Narrow message localizer interface

We never access Language directly, so proxy its method instead of
returning the full object.

I believe I've found a bug, but not fixing here: the footnote body
numeric backlinks like "2.1" behave as if they were decimals rather
than two numbers stuck together with a dot.  So they are localized
to "2,1".

Bug: T239725
Change-Id: If386bf96d48cb95c0a287a02bedfe984941efe30
This commit is contained in:
Adam Wight 2019-12-06 10:12:25 +01:00
parent f745a6b2f2
commit 3d80501829
6 changed files with 61 additions and 28 deletions

View file

@ -253,7 +253,7 @@ class FootnoteBodyFormatter {
int $offset,
int $max
) : string {
return $this->messageLocalizer->getLanguage()->formatNum(
return $this->messageLocalizer->formatNum(
$base .
'.' .
str_pad( $offset, strlen( $max ), '0', STR_PAD_LEFT )

View file

@ -64,17 +64,15 @@ class FootnoteMarkFormatter {
* @return string
*/
public function linkRef( string $group, array $ref ) : string {
$language = $this->messageLocalizer->getLanguage();
$label = $this->getLinkLabel( $group, $ref['number'] );
if ( $label === null ) {
$label = $language->formatNum( $ref['number'] );
$label = $this->messageLocalizer->formatNum( $ref['number'] );
if ( $group !== Cite::DEFAULT_GROUP ) {
$label = "$group $label";
}
}
if ( isset( $ref['extendsIndex'] ) ) {
$label .= '.' . $language->formatNum( $ref['extendsIndex'], true );
$label .= '.' . $this->messageLocalizer->formatNumNoSeparators( $ref['extendsIndex'] );
}
$key = $ref['name'] ?? $ref['key'];

View file

@ -24,10 +24,23 @@ class ReferenceMessageLocalizer implements MessageLocalizer {
}
/**
* @return Language
* Transliterate numerals, add thousands separators and localize the decimal point.
*
* @param string $number
* @return string
*/
public function getLanguage(): Language {
return $this->language;
public function formatNum( string $number ): string {
return $this->language->formatNum( $number );
}
/**
* Transliterate numerals, without adding or changing separators.
*
* @param string $number
* @return string
*/
public function formatNumNoSeparators( string $number ): string {
return $this->language->formatNumNoSeparators( $number );
}
/**
@ -47,4 +60,5 @@ class ReferenceMessageLocalizer implements MessageLocalizer {
public function msg( $key, ...$params ): Message {
return wfMessage( $key, ...$params )->inLanguage( $this->language );
}
}

View file

@ -6,7 +6,6 @@ use Cite\CiteErrorReporter;
use Cite\CiteKeyFormatter;
use Cite\FootnoteBodyFormatter;
use Cite\ReferenceMessageLocalizer;
use Language;
use MediaWikiUnitTestCase;
use Message;
use Parser;
@ -213,13 +212,7 @@ class FootnoteBodyFormatterTest extends MediaWikiUnitTestCase {
);
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'getLanguage' )->willReturnCallback(
function () {
$mockLanguage = $this->createMock( Language::class );
$mockLanguage->method( 'formatNum' )->willReturnArgument( 0 );
return $mockLanguage;
}
);
$mockMessageLocalizer->method( 'formatNum' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) {
$mockMessage = $this->createMock( Message::class );
@ -415,12 +408,9 @@ class FootnoteBodyFormatterTest extends MediaWikiUnitTestCase {
public function testReferencesFormatEntryNumericBacklinkLabel(
string $expectedLabel, int $base, int $offset, int $max
) {
$mockLanguage = $this->createMock( Language::class );
$mockLanguage->method( 'formatNum' )->with( $expectedLabel )
->willReturnArgument( 0 );
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'getLanguage' )->willReturn( $mockLanguage );
$mockMessageLocalizer->method( 'formatNum' )->willReturnArgument( 0 );
$formatter = TestingAccessWrapper::newFromObject( new FootnoteBodyFormatter(
$this->createMock( Parser::class ),
$this->createMock( CiteErrorReporter::class ),

View file

@ -2,7 +2,6 @@
namespace Cite;
use Language;
use MediaWikiUnitTestCase;
use Message;
use Parser;
@ -34,13 +33,8 @@ class FootnoteMarkFormatterTest extends MediaWikiUnitTestCase {
}
);
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'getLanguage' )->willReturnCallback(
function () {
$mockLanguage = $this->createMock( Language::class );
$mockLanguage->method( 'formatNum' )->willReturnArgument( 0 );
return $mockLanguage;
}
);
$mockMessageLocalizer->method( 'formatNum' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'formatNumNoSeparators' )->willReturnArgument( 0 );
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) use ( $group, $fooLabels ) {
$mockMessage = $this->createMock( Message::class );

View file

@ -0,0 +1,37 @@
<?php
namespace Cite\Tests\Unit;
use Cite\ReferenceMessageLocalizer;
use Language;
use MediaWikiUnitTestCase;
/**
* @coversDefaultClass \Cite\ReferenceMessageLocalizer
*/
class ReferenceMessageLocalizerUnitTest extends MediaWikiUnitTestCase {
/**
* @covers ::formatNum
* @covers ::__construct
*/
public function testFormatNum() {
$mockLanguage = $this->createMock( Language::class );
$mockLanguage->method( 'formatNum' )->willReturn( '10,0' );
/** @var Language $mockLanguage */
$messageLocalizer = new ReferenceMessageLocalizer( $mockLanguage );
$this->assertSame( '10,0', $messageLocalizer->formatNum( '10.0' ) );
}
/**
* @covers ::formatNumNoSeparators
*/
public function testFormatNumNoSeparators() {
$mockLanguage = $this->createMock( Language::class );
$mockLanguage->method( 'formatNumNoSeparators' )->willReturn( 'ה' );
/** @var Language $mockLanguage */
$messageLocalizer = new ReferenceMessageLocalizer( $mockLanguage );
$this->assertSame( 'ה', $messageLocalizer->formatNumNoSeparators( '5' ) );
}
}