From 88bdda41b1d31b76543f813f6f2e98fd8b40749f Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Thu, 22 Sep 2011 05:01:19 +0000 Subject: [PATCH] Add support for Number grouping(commafy) based on CLDR number grouping patterns like ##,##,###. Testcases for Malayalam and Dutch Number grouping Patterns added to Ml and Hi Message classes. Reference: Bug 29495 --- includes/LocalisationCache.php | 1 + languages/Language.php | 48 +++++++++++++++++++++- languages/messages/MessagesHi.php | 2 + languages/messages/MessagesMl.php | 2 + tests/phpunit/languages/LanguageMlTest.php | 30 ++++++++++++++ tests/phpunit/languages/LanguageNlTest.php | 28 +++++++++++++ 6 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/languages/LanguageMlTest.php create mode 100644 tests/phpunit/languages/LanguageNlTest.php diff --git a/includes/LocalisationCache.php b/includes/LocalisationCache.php index d451dc2f5d..361cfa7a40 100644 --- a/includes/LocalisationCache.php +++ b/includes/LocalisationCache.php @@ -88,6 +88,7 @@ class LocalisationCache { 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap', 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases', 'imageFiles', 'preloadedMessages', 'namespaceGenderAliases', + 'digitGroupingPattern' ); /** diff --git a/languages/Language.php b/languages/Language.php index 8c801c31bd..75f3288d0f 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -2606,12 +2606,56 @@ class Language { /** * Adds commas to a given number - * + * @since 1.19 * @param $_ mixed * @return string */ function commafy( $_ ) { - return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) ); + $digitGroupingPattern = $this->digitGroupingPattern(); + + if ( !$digitGroupingPattern || $digitGroupingPattern === "###,###,###" ) { + //default grouping is at thousands, use the same for ###,###,### pattern too. + return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) ); + } + else { + // Ref: http://cldr.unicode.org/translation/number-patterns + $numberpart = array(); + $decimalpart = array(); + $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches ); + preg_match( "/\d+/", $_, $numberpart ); + preg_match( "/\.\d*/", $_, $decimalpart ); + $groupedNumber = ( count( $decimalpart ) > 0 ) ? $decimalpart[0]:""; + if ( $groupedNumber === $_){ + //the string does not have any number part. Eg: .12345 + return $groupedNumber; + } + $start = $end = strlen( $numberpart[0] ); + while ( $start > 0 ) + { + $match = $matches[0][$numMatches -1] ; + $matchLen = strlen( $match ); + $start = $end - $matchLen; + if ( $start < 0 ) { + $start = 0; + } + $groupedNumber = substr( $_ , $start, $end -$start ) . $groupedNumber ; + $end = $start; + if ( $numMatches > 1 ) { + // use the last pattern for the rest of the number + $numMatches--; + } + if ( $start > 0 ) { + $groupedNumber = "," . $groupedNumber; + } + } + return $groupedNumber; + } + } + /** + * @return String + */ + function digitGroupingPattern() { + return self::$dataCache->getItem( $this->mCode, 'digitGroupingPattern' ); } /** diff --git a/languages/messages/MessagesHi.php b/languages/messages/MessagesHi.php index 2668c0e0a2..3527e066f9 100644 --- a/languages/messages/MessagesHi.php +++ b/languages/messages/MessagesHi.php @@ -68,6 +68,8 @@ $digitTransformTable = array( ); $linkTrail = "/^([a-z]+)(.*)$/sD"; +$digitGroupingPattern = "##,##,###"; + $messages = array( # User preference toggles 'tog-underline' => 'कड़ियाँ अधोरेखन:', diff --git a/languages/messages/MessagesMl.php b/languages/messages/MessagesMl.php index a911deb270..8b91aae4ff 100644 --- a/languages/messages/MessagesMl.php +++ b/languages/messages/MessagesMl.php @@ -315,6 +315,8 @@ $magicWords = array( 'url_query' => array( '0', 'ക്വറി', 'QUERY' ), ); +$digitGroupingPattern = "##,##,###"; + $messages = array( # User preference toggles 'tog-underline' => 'കണ്ണികൾക്ക് അടിവരയിടുക:', diff --git a/tests/phpunit/languages/LanguageMlTest.php b/tests/phpunit/languages/LanguageMlTest.php new file mode 100644 index 0000000000..a947e788b8 --- /dev/null +++ b/tests/phpunit/languages/LanguageMlTest.php @@ -0,0 +1,30 @@ +lang = Language::factory( 'Ml' ); + } + function tearDown() { + unset( $this->lang ); + } + + /** see bug 29495 */ + function testFormatNum() { + $this->assertEquals( '12,34,567', $this->lang->formatNum( '1234567' ) ); + $this->assertEquals( '12,345', $this->lang->formatNum( '12345' ) ); + $this->assertEquals( '1', $this->lang->formatNum( '1' ) ); + $this->assertEquals( '123', $this->lang->formatNum( '123' ) ); + $this->assertEquals( '1,234', $this->lang->formatNum( '1234' ) ); + $this->assertEquals( '12,345.56', $this->lang->formatNum( '12345.56' ) ); + $this->assertEquals( '12,34,56,79,81,23,45,678', $this->lang->formatNum( '12345679812345678' ) ); + $this->assertEquals( '.12345', $this->lang->formatNum( '.12345' ) ); + } +} diff --git a/tests/phpunit/languages/LanguageNlTest.php b/tests/phpunit/languages/LanguageNlTest.php new file mode 100644 index 0000000000..cf979cd24c --- /dev/null +++ b/tests/phpunit/languages/LanguageNlTest.php @@ -0,0 +1,28 @@ +lang = Language::factory( 'Nl' ); + } + function tearDown() { + unset( $this->lang ); + } + + function testFormatNum() { + $this->assertEquals( '1.234.567', $this->lang->formatNum( '1234567' ) ); + $this->assertEquals( '12.345', $this->lang->formatNum( '12345' ) ); + $this->assertEquals( '1', $this->lang->formatNum( '1' ) ); + $this->assertEquals( '123', $this->lang->formatNum( '123' ) ); + $this->assertEquals( '1.234', $this->lang->formatNum( '1234' ) ); + $this->assertEquals( '12.345,56', $this->lang->formatNum( '12345.56' ) ); + $this->assertEquals( ',1234556', $this->lang->formatNum( '.1234556' ) ); + } +} -- 2.20.1