| 1 | <?php |
| 2 | |
| 3 | use MediaWiki\Message\Message; |
| 4 | use Wikimedia\Message\MessageSpecifier; |
| 5 | |
| 6 | /** |
| 7 | * A simple {@link MessageLocalizer} implementation for use in tests. |
| 8 | * By default, it sets the message language to 'qqx', |
| 9 | * to make the tests independent of the wiki configuration. |
| 10 | * |
| 11 | * @author Lucas Werkmeister |
| 12 | * @license GPL-2.0-or-later |
| 13 | */ |
| 14 | class MockMessageLocalizer implements MessageLocalizer { |
| 15 | |
| 16 | /** |
| 17 | * @var string|null |
| 18 | */ |
| 19 | private $languageCode; |
| 20 | |
| 21 | /** |
| 22 | * @param string|null $languageCode The language code to use for messages by default. |
| 23 | * You can specify null to use the user language, |
| 24 | * but this is not recommended as it may make your tests depend on the wiki configuration. |
| 25 | */ |
| 26 | public function __construct( $languageCode = 'qqx' ) { |
| 27 | $this->languageCode = $languageCode; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Get a Message object. |
| 32 | * Parameters are the same as {@link wfMessage()}. |
| 33 | * |
| 34 | * @param string|string[]|MessageSpecifier $key Message key, or array of keys, |
| 35 | * or a MessageSpecifier. |
| 36 | * @param mixed ...$params |
| 37 | * @return Message |
| 38 | */ |
| 39 | public function msg( $key, ...$params ) { |
| 40 | $message = wfMessage( $key, ...$params ); |
| 41 | |
| 42 | if ( $this->languageCode !== null ) { |
| 43 | $message->inLanguage( $this->languageCode ); |
| 44 | } |
| 45 | |
| 46 | return $message; |
| 47 | } |
| 48 | |
| 49 | } |