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
This commit is contained in:
parent
2c26f5fe4e
commit
88bdda41b1
6 changed files with 109 additions and 2 deletions
|
|
@ -88,6 +88,7 @@ class LocalisationCache {
|
|||
'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
|
||||
'defaultDateFormat', 'extraUserToggles', 'specialPageAliases',
|
||||
'imageFiles', 'preloadedMessages', 'namespaceGenderAliases',
|
||||
'digitGroupingPattern'
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -68,6 +68,8 @@ $digitTransformTable = array(
|
|||
);
|
||||
$linkTrail = "/^([a-z]+)(.*)$/sD";
|
||||
|
||||
$digitGroupingPattern = "##,##,###";
|
||||
|
||||
$messages = array(
|
||||
# User preference toggles
|
||||
'tog-underline' => 'कड़ियाँ अधोरेखन:',
|
||||
|
|
|
|||
|
|
@ -315,6 +315,8 @@ $magicWords = array(
|
|||
'url_query' => array( '0', 'ക്വറി', 'QUERY' ),
|
||||
);
|
||||
|
||||
$digitGroupingPattern = "##,##,###";
|
||||
|
||||
$messages = array(
|
||||
# User preference toggles
|
||||
'tog-underline' => 'കണ്ണികൾക്ക് അടിവരയിടുക:',
|
||||
|
|
|
|||
30
tests/phpunit/languages/LanguageMlTest.php
Normal file
30
tests/phpunit/languages/LanguageMlTest.php
Normal file
|
|
@ -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' ) );
|
||||
}
|
||||
}
|
||||
28
tests/phpunit/languages/LanguageNlTest.php
Normal file
28
tests/phpunit/languages/LanguageNlTest.php
Normal file
|
|
@ -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' ) );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue