Stop using Language::formatNum to localize separators

This is done to make the discussion in If3dcfd7 easier.

When we introduced this code we actually used it to format
entire numbers. We had to change this later to *not* localize
digits, but only separators. Language::formatNum is and always
was able to do this, so we just continued to use it.

This is discussed now.

It turns out there is only a single place left where we use
formatNum, and it does nothing but localizing the decimal
point. There is another way to do the same.

Bug: T237467
Change-Id: I89b17a9e11b3afc6c653ba7ccc6ff84c37863b66
This commit is contained in:
Thiemo Kreuz 2020-09-02 09:40:33 +02:00
parent bc919e6d78
commit 7ce27b432f
5 changed files with 11 additions and 12 deletions

View file

@ -25,14 +25,13 @@ class ReferenceMessageLocalizer implements MessageLocalizer {
}
/**
* Transliterate numerals, add thousands separators and localize the decimal point.
*
* @param string $number
*
* @return string
*/
public function formatNum( string $number ) : string {
return $this->language->formatNum( $number );
public function localizeSeparators( string $number ) : string {
// Filter to make sure characters are never removed
return strtr( $number, array_filter( $this->language->separatorTransformTable() ?: [] ) );
}
/**

View file

@ -265,7 +265,7 @@ class ReferencesFormatter {
int $max
) : string {
return $this->messageLocalizer->localizeDigits( $base ) .
$this->messageLocalizer->formatNum( '.' ) .
$this->messageLocalizer->localizeSeparators( '.' ) .
$this->messageLocalizer->localizeDigits(
str_pad( (string)$offset, strlen( (string)$max ), '0', STR_PAD_LEFT )
);

View file

@ -37,7 +37,7 @@ class FootnoteMarkFormatterTest extends \MediaWikiUnitTestCase {
}
);
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'formatNum' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'localizeSeparators' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) use ( $group, $fooLabels ) {

View file

@ -11,15 +11,15 @@ use Language;
class ReferenceMessageLocalizerUnitTest extends \MediaWikiUnitTestCase {
/**
* @covers ::formatNum
* @covers ::localizeSeparators
* @covers ::__construct
*/
public function testFormatNum() {
public function testLocalizeSeparators() {
$mockLanguage = $this->createMock( Language::class );
$mockLanguage->method( 'formatNum' )->willReturn( '10,0' );
$mockLanguage->method( 'separatorTransformTable' )->willReturn( [ '.' => ',', '0' => '' ] );
/** @var Language $mockLanguage */
$messageLocalizer = new ReferenceMessageLocalizer( $mockLanguage );
$this->assertSame( '10,0', $messageLocalizer->formatNum( '10.0' ) );
$this->assertSame( '10,0', $messageLocalizer->localizeSeparators( '10.0' ) );
}
/**

View file

@ -214,7 +214,7 @@ class ReferencesFormatterTest extends \MediaWikiUnitTestCase {
);
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'formatNum' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'localizeSeparators' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) {
@ -414,7 +414,7 @@ class ReferencesFormatterTest extends \MediaWikiUnitTestCase {
string $expectedLabel, int $base, int $offset, int $max
) {
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'formatNum' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'localizeSeparators' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */