http://www.mediawiki.org/wiki/Special:Code/MediaWiki/97793
Revision: 97793
Author: santhosh
Date: 2011-09-22 05:01:19 +0000 (Thu, 22 Sep 2011)
Log Message:
-----------
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
Modified Paths:
--------------
trunk/phase3/includes/LocalisationCache.php
trunk/phase3/languages/Language.php
trunk/phase3/languages/messages/MessagesHi.php
trunk/phase3/languages/messages/MessagesMl.php
Added Paths:
-----------
trunk/phase3/tests/phpunit/languages/LanguageMlTest.php
trunk/phase3/tests/phpunit/languages/LanguageNlTest.php
Modified: trunk/phase3/includes/LocalisationCache.php
===================================================================
--- trunk/phase3/includes/LocalisationCache.php 2011-09-22 04:54:12 UTC (rev
97792)
+++ trunk/phase3/includes/LocalisationCache.php 2011-09-22 05:01:19 UTC (rev
97793)
@@ -88,6 +88,7 @@
'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
'defaultDateFormat', 'extraUserToggles', 'specialPageAliases',
'imageFiles', 'preloadedMessages', 'namespaceGenderAliases',
+ 'digitGroupingPattern'
);
/**
Modified: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php 2011-09-22 04:54:12 UTC (rev 97792)
+++ trunk/phase3/languages/Language.php 2011-09-22 05:01:19 UTC (rev 97793)
@@ -2606,13 +2606,57 @@
/**
* 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' );
+ }
/**
* @return array
Modified: trunk/phase3/languages/messages/MessagesHi.php
===================================================================
--- trunk/phase3/languages/messages/MessagesHi.php 2011-09-22 04:54:12 UTC
(rev 97792)
+++ trunk/phase3/languages/messages/MessagesHi.php 2011-09-22 05:01:19 UTC
(rev 97793)
@@ -68,6 +68,8 @@
);
$linkTrail = "/^([a-z]+)(.*)$/sD";
+$digitGroupingPattern = "##,##,###";
+
$messages = array(
# User preference toggles
'tog-underline' => 'कड़ियाँ अधोरेखन:',
Modified: trunk/phase3/languages/messages/MessagesMl.php
===================================================================
--- trunk/phase3/languages/messages/MessagesMl.php 2011-09-22 04:54:12 UTC
(rev 97792)
+++ trunk/phase3/languages/messages/MessagesMl.php 2011-09-22 05:01:19 UTC
(rev 97793)
@@ -315,6 +315,8 @@
'url_query' => array( '0', 'ക്വറി', 'QUERY' ),
);
+$digitGroupingPattern = "##,##,###";
+
$messages = array(
# User preference toggles
'tog-underline' => 'കണ്ണികൾക്ക് അടിവരയിടുക:',
Added: trunk/phase3/tests/phpunit/languages/LanguageMlTest.php
===================================================================
--- trunk/phase3/tests/phpunit/languages/LanguageMlTest.php
(rev 0)
+++ trunk/phase3/tests/phpunit/languages/LanguageMlTest.php 2011-09-22
05:01:19 UTC (rev 97793)
@@ -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'
) );
+ }
+}
Property changes on: trunk/phase3/tests/phpunit/languages/LanguageMlTest.php
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/phase3/tests/phpunit/languages/LanguageNlTest.php
===================================================================
--- trunk/phase3/tests/phpunit/languages/LanguageNlTest.php
(rev 0)
+++ trunk/phase3/tests/phpunit/languages/LanguageNlTest.php 2011-09-22
05:01:19 UTC (rev 97793)
@@ -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' ) );
+ }
+}
Property changes on: trunk/phase3/tests/phpunit/languages/LanguageNlTest.php
___________________________________________________________________
Added: svn:eol-style
+ native
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs