Introduce ReferenceMessageLocalizer

Encapsulate the language interfaces, this will be used to replace
global wfMessage calls in future patches.

Change-Id: I7857f3e5154626e0b29977610b81103d91615f65
This commit is contained in:
Adam Wight 2019-12-04 00:39:28 +01:00
parent 0f146cd56e
commit bccb92335f
2 changed files with 67 additions and 0 deletions

View file

@ -0,0 +1,46 @@
<?php
namespace Cite;
use Language;
use Message;
use MessageLocalizer;
use MessageSpecifier;
class ReferenceMessageLocalizer implements MessageLocalizer {
/**
* @var Language
*/
private $language;
/**
* @param Language $language
*/
public function __construct( Language $language ) {
$this->language = $language;
}
/**
* @return Language
*/
public function getLanguage(): Language {
return $this->language;
}
/**
* This is the method for getting translated interface messages.
*
* @see https://www.mediawiki.org/wiki/Manual:Messages_API
* @see Message::__construct
*
* @param string|string[]|MessageSpecifier $key Message key, or array of keys,
* or a MessageSpecifier.
* @param mixed ...$params Normal message parameters
* @return Message
*/
public function msg( $key, ...$params ): Message {
return wfMessage( $key, ...$params )->inLanguage( $this->language );
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Cite;
use LanguageQqx;
use MediaWikiIntegrationTestCase;
/**
* @coversDefaultClass \Cite\ReferenceMessageLocalizer
*/
class ReferenceMessageLocalizerTest extends MediaWikiIntegrationTestCase {
/**
* @covers ::msg
*/
public function testMsg() {
$localizer = new ReferenceMessageLocalizer( new LanguageQqx() );
$this->assertSame(
'(cite_reference_link_prefix)',
$localizer->msg( 'cite_reference_link_prefix' )->plain() );
}
}