Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageBundleLuaLibrary.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageBundleTranslation;
5
6use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LibraryBase;
7use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError;
9use MediaWiki\MediaWikiServices;
10
11class MessageBundleLuaLibrary extends LibraryBase {
12 public function register() {
13 $extensionLuaPath = __DIR__ . '/lua/MessageBundleLibrary.lua';
14 $lib = [
15 'validate' => [ $this, 'validate' ],
16 'getMessageBundleTranslations' => [ $this, 'getMessageBundleTranslations' ]
17 ];
18 $opts = [
19 'pageLanguageCode' => $this->getTitle()->getPageLanguage()->getCode()
20 ];
21
22 return $this->getEngine()->registerInterface( $extensionLuaPath, $lib, $opts );
23 }
24
25 public function validate( string $messageBundleTitle ): void {
26 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
27 $mbTitle = $titleFactory->newFromText( $messageBundleTitle );
28 if ( !MessageBundle::isSourcePage( $mbTitle ) ) {
29 throw new LuaError( "$messageBundleTitle is not a valid message bundle." );
30 }
31 }
32
33 public function getMessageBundleTranslations(
34 string $messageBundleTitle,
35 string $languageCode,
36 bool $skipFallbacks
37 ): array {
38 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
39 $messageBundle = new MessageBundle( $titleFactory->newFromText( $messageBundleTitle ) );
40 $messageBundleTranslationLoader = Services::getInstance()->getMessageBundleTranslationLoader();
41 if ( !MessageBundle::isSourcePage( $messageBundle->getTitle() ) ) {
42 throw new LuaError( "Message bundle with title $messageBundleTitle not found" );
43 }
44
45 return [ $messageBundleTranslationLoader->get( $messageBundle, $languageCode, $skipFallbacks ) ];
46 }
47}
Minimal service container.
Definition Services.php:58