MediaWiki 1.42.1
LanguageConverterFactory.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Languages;
22
23use BanConverter;
24use CrhConverter;
25use EnConverter;
26use GanConverter;
28use IuConverter;
29use KuConverter;
30use Language;
34use ShConverter;
35use ShiConverter;
36use SrConverter;
37use TgConverter;
38use TlyConverter;
40use UzConverter;
41use Wikimedia\ObjectFactory\ObjectFactory;
42use WuuConverter;
43use ZhConverter;
44
52
53 private $cache = [];
57 private $converterList = [
58 'ban' => [
59 'class' => BanConverter::class,
60 ],
61 'crh' => [
62 'class' => CrhConverter::class,
63 ],
64 'gan' => [
65 'class' => GanConverter::class,
66 ],
67 'iu' => [
68 'class' => IuConverter::class,
69 ],
70 'ku' => [
71 'class' => KuConverter::class,
72 ],
73 'shi' => [
74 'class' => ShiConverter::class,
75 ],
76 'sh' => [
77 'class' => ShConverter::class,
78 ],
79 'sr' => [
80 'class' => SrConverter::class,
81 ],
82 'tg' => [
83 'class' => TgConverter::class,
84 ],
85 'tly' => [
86 'class' => TlyConverter::class,
87 ],
88 'uz' => [
89 'class' => UzConverter::class,
90 ],
91 'wuu' => [
92 'class' => WuuConverter::class,
93 ],
94 'zh' => [
95 'class' => ZhConverter::class,
96 ],
97 ];
98
99 private const DEFAULT_CONVERTER = [
100 'class' => TrivialLanguageConverter::class,
101 'services' => [
102 'TitleFormatter',
103 ]
104 ];
105
106 private const EN_CONVERTER = [
107 'class' => EnConverter::class,
108 ];
109
113 public const CONSTRUCTOR_OPTIONS = [
117 ];
118
119 private ServiceOptions $options;
120 private ObjectFactory $objectFactory;
121
125 private $defaultLanguage;
126
135 public function __construct(
136 ServiceOptions $options,
137 ObjectFactory $objectFactory,
138 callable $defaultLanguage
139 ) {
140 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
141 $this->options = $options;
142 $this->objectFactory = $objectFactory;
143 if ( $options->get( MainConfigNames::UsePigLatinVariant ) ) {
144 $this->converterList['en'] = self::EN_CONVERTER;
145 }
146 $this->defaultLanguage = $defaultLanguage;
147 }
148
155 private function instantiateConverter( $lang ): ILanguageConverter {
156 $code = mb_strtolower( $lang->getCode() );
157 $spec = $this->converterList[$code] ?? self::DEFAULT_CONVERTER;
158 // ObjectFactory::createObject accepts an array, not just a callable (phan bug)
159 // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey, PhanTypeInvalidCallableArraySize
160 return $this->objectFactory->createObject(
161 $spec,
162 [
163 'assertClass' => ILanguageConverter::class,
164 'extraArgs' => [ $lang ],
165 ]
166 );
167 }
168
178 public function getLanguageConverter( $language = null ): ILanguageConverter {
179 $lang = $language ?? ( $this->defaultLanguage )();
180 if ( isset( $this->cache[$lang->getCode()] ) ) {
181 return $this->cache[$lang->getCode()];
182 }
183 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
184 $converter = $this->instantiateConverter( $lang );
185 $this->cache[$lang->getCode()] = $converter;
186 return $converter;
187 }
188
194 public function isConversionDisabled() {
195 return $this->options->get( MainConfigNames::DisableLangConversion );
196 }
197
203 public function isLinkConversionDisabled() {
204 return $this->options->get( MainConfigNames::DisableLangConversion ) ||
205 // Note that this configuration option is misnamed.
206 $this->options->get( MainConfigNames::DisableTitleConversion );
207 }
208}
Balinese specific code.
Crimean Tatar (Qırımtatarca) converter routines.
English specific converter routines.
Gan Chinese specific code.
Inuktitut specific code.
Kurdish converter routines.
Base class for language-specific code.
Definition Language.php:63
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
An interface for creating language converters.
isLinkConversionDisabled()
Whether to disable language variant conversion for links.
isConversionDisabled()
Whether to disable language variant conversion.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
__construct(ServiceOptions $options, ObjectFactory $objectFactory, callable $defaultLanguage)
A class containing constants representing the names of configuration variables.
const UsePigLatinVariant
Name constant for the UsePigLatinVariant setting, for use with Config::get()
const DisableLangConversion
Name constant for the DisableLangConversion setting, for use with Config::get()
const DisableTitleConversion
Name constant for the DisableTitleConversion setting, for use with Config::get()
Stub object for the user language.
Converts Serbo-Croatian from Latin script to Cyrillic script.
Shilha specific code.
Serbian (Српски / Srpski) specific code.
Tajik (Тоҷикӣ) specific code, converting Tajiki to Latin orthography.
Talysh specific code.
A trivial language converter.
Uzbek specific code.
Wu language specific code.
Chinese converter routine.
The shared interface for all language converters.