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:
alistair3149 2022-05-19 14:46:35 -04:00
parent 1b7217457a
commit c0cae44d41
No known key found for this signature in database
GPG key ID: 94D081060FD3DD9C

View file

@ -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;
}
}