Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
authorSanthosh Thottingal <santhosh@users.mediawiki.org>
Thu, 22 Sep 2011 05:01:19 +0000 (05:01 +0000)
committerSanthosh Thottingal <santhosh@users.mediawiki.org>
Thu, 22 Sep 2011 05:01:19 +0000 (05:01 +0000)
Testcases for Malayalam and Dutch
Number grouping Patterns added to Ml and Hi Message classes.
Reference: Bug 29495

includes/LocalisationCache.php
languages/Language.php
languages/messages/MessagesHi.php
languages/messages/MessagesMl.php
tests/phpunit/languages/LanguageMlTest.php [new file with mode: 0644]
tests/phpunit/languages/LanguageNlTest.php [new file with mode: 0644]

index d451dc2..361cfa7 100644 (file)
@@ -88,6 +88,7 @@ class LocalisationCache {
                'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
                'defaultDateFormat', 'extraUserToggles', 'specialPageAliases',
                'imageFiles', 'preloadedMessages', 'namespaceGenderAliases',
+               'digitGroupingPattern'
        );
 
        /**
index 8c801c3..75f3288 100644 (file)
@@ -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' );
        }
 
        /**
index 2668c0e..3527e06 100644 (file)
@@ -68,6 +68,8 @@ $digitTransformTable = array(
 );
 $linkTrail = "/^([a-z]+)(.*)$/sD";
 
+$digitGroupingPattern = "##,##,###";
+
 $messages = array(
 # User preference toggles
 'tog-underline'               => 'कड़ियाँ अधोरेखन:',
index a911deb..8b91aae 100644 (file)
@@ -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 (file)
index 0000000..a947e78
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @author Santhosh Thottingal
+ * @copyright Copyright © 2011, Santhosh Thottingal
+ * @file
+ */
+
+/** Tests for MediaWiki languages/LanguageMl.php */
+class LanguageMlTest extends MediaWikiTestCase {
+       private $lang;
+
+       function setUp() {
+               $this->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 (file)
index 0000000..cf979cd
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * @author Santhosh Thottingal
+ * @copyright Copyright © 2011, Santhosh Thottingal
+ * @file
+ */
+
+/** Tests for MediaWiki languages/LanguageNl.php */
+class LanguageNlTest extends MediaWikiTestCase {
+       private $lang;
+
+       function setUp() {
+               $this->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' ) );
+       }
+}