mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-09-23 18:28:58 +00:00
feat(core): improve handling of number formatter for site stats
* The lack of PECL intl would not trigger an excpetion anymore, instead it will default back to the regular PHP number format * NumberFormatter will be based on user locale now instead of en_US
This commit is contained in:
parent
1b7217457a
commit
c0cae44d41
|
@ -168,11 +168,15 @@ final class Drawer extends Partial {
|
|||
if ( $this->getConfigValue( 'CitizenEnableDrawerSiteStats' ) ) {
|
||||
$stats = [ 'articles', 'images', 'users', 'edits' ];
|
||||
$items = [];
|
||||
$fmt = null;
|
||||
|
||||
// Get NumberFormatter here so that we don't have to call it for every stat
|
||||
$fmt = new \NumberFormatter( 'en_US', \NumberFormatter::PADDING_POSITION );
|
||||
$fmt->setAttribute( \NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_DOWN );
|
||||
$fmt->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, 1 );
|
||||
// Get NumberFormatter here so that we don't have to call it for every stats
|
||||
if ( class_exists( \NumberFormatter::class ) ) {
|
||||
$locale = $this->skin->getLanguage()->getHtmlCode() ?? 'en_US';
|
||||
$fmt = new \NumberFormatter( $locale, \NumberFormatter::PADDING_POSITION );
|
||||
$fmt->setAttribute( \NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_DOWN );
|
||||
$fmt->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, 1 );
|
||||
}
|
||||
|
||||
foreach ( $stats as &$stat ) {
|
||||
$items[] = [
|
||||
|
@ -190,21 +194,17 @@ final class Drawer extends Partial {
|
|||
/**
|
||||
* Get and format sitestat value
|
||||
*
|
||||
* TODO: Formatting should be based on user locale
|
||||
*
|
||||
* @param string $key
|
||||
* @param NumberFormatter $fmt
|
||||
* @param NumberFormatter|null $fmt
|
||||
* @return string
|
||||
*/
|
||||
private function getSiteStatValue( $key, $fmt ): string {
|
||||
$value = call_user_func( 'SiteStats::' . $key ) ?? '';
|
||||
|
||||
if ( $value >= 10000 ) {
|
||||
$value = $fmt->format( $value );
|
||||
if ( $fmt ) {
|
||||
return $fmt->format( $value );
|
||||
} else {
|
||||
$value = number_format( $value );
|
||||
return number_format( $value );
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue