MediaWiki  master
LanguageConverterFactory.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Languages;
22 
23 use BanConverter;
24 use CrhConverter;
25 use EnConverter;
26 use GanConverter;
28 use IuConverter;
29 use KkConverter;
30 use KuConverter;
31 use Language;
32 use ShiConverter;
33 use SrConverter;
34 use StubUserLang;
35 use TgConverter;
36 use TlyConverter;
38 use UzConverter;
39 use Wikimedia\ObjectFactory\ObjectFactory;
40 use ZhConverter;
41 
49 
50  private $cache = [];
54  private $converterList = [
55  'ban' => [
56  'class' => BanConverter::class,
57  ],
58  'crh' => [
59  'class' => CrhConverter::class,
60  ],
61  'gan' => [
62  'class' => GanConverter::class,
63  ],
64  'iu' => [
65  'class' => IuConverter::class,
66  ],
67  'kk' => [
68  'class' => KkConverter::class,
69  ],
70  'ku' => [
71  'class' => KuConverter::class,
72  ],
73  'shi' => [
74  'class' => ShiConverter::class,
75  ],
76  'sr' => [
77  'class' => SrConverter::class,
78  ],
79  'tg' => [
80  'class' => TgConverter::class,
81  ],
82  'tly' => [
83  'class' => TlyConverter::class,
84  ],
85  'uz' => [
86  'class' => UzConverter::class,
87  ],
88  'zh' => [
89  'class' => ZhConverter::class,
90  ],
91  ];
92 
93  private const DEFAULT_CONVERTER = [
94  'class' => TrivialLanguageConverter::class,
95  'services' => [
96  'TitleFormatter',
97  ]
98  ];
99 
100  private const EN_CONVERTER = [
101  'class' => EnConverter::class,
102  ];
103 
105  private $objectFactory;
106 
111 
116 
121 
132  public function __construct(
133  ObjectFactory $objectFactory,
135  callable $defaultLanguage
136  ) {
137  $this->objectFactory = $objectFactory;
138  if ( $usePigLatinVariant ) {
139  $this->converterList['en'] = self::EN_CONVERTER;
140  }
143  $this->defaultLanguage = $defaultLanguage;
144  }
145 
153  $code = mb_strtolower( $lang->getCode() );
154  $spec = $this->converterList[$code] ?? self::DEFAULT_CONVERTER;
155  // ObjectFactory::createObject accepts an array, not just a callable (phan bug)
156  // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey,PhanTypeInvalidCallableArraySize
157  return $this->objectFactory->createObject(
158  $spec,
159  [
160  'assertClass' => ILanguageConverter::class,
161  'extraArgs' => [ $lang ],
162  ]
163  );
164  }
165 
175  public function getLanguageConverter( $language = null ): ILanguageConverter {
176  $lang = $language ?? ( $this->defaultLanguage )();
177  if ( isset( $this->cache[$lang->getCode()] ) ) {
178  return $this->cache[$lang->getCode()];
179  }
180  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
181  $converter = $this->instantiateConverter( $lang );
182  $this->cache[$lang->getCode()] = $converter;
183  return $converter;
184  }
185 
190  public function isConversionDisabled() {
191  return $this->isConversionDisabled;
192  }
193 
199  public function isTitleConversionDisabled() {
200  return $this->isTitleConversionDisabled;
201  }
202 
207  public function isLinkConversionDisabled() {
208  return $this->isConversionDisabled || $this->isTitleConversionDisabled;
209  }
210 }
Crimean Tatar (Qırımtatarca) converter routines.
Conversion script between Latin and Syllabics for Inuktitut.
Definition: IuConverter.php:36
Kazakh (Қазақша) converter routines.
Definition: KkConverter.php:37
Kurdish converter routines.
Definition: KuConverter.php:29
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:45
An interface for creating language converters.
instantiateConverter( $lang)
Returns Converter instance for given language object.
bool $isTitleConversionDisabled
Whether to disable language variant conversion for links.
isTitleConversionDisabled()
Whether to disable language variant conversion for titles.
__construct(ObjectFactory $objectFactory, $usePigLatinVariant, $isConversionDisabled, $isTitleConversionDisabled, callable $defaultLanguage)
isLinkConversionDisabled()
Whether to disable language variant conversion for links.
callable $defaultLanguage
callback of () : Language
isConversionDisabled()
Whether to disable language variant conversion.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
bool $isConversionDisabled
Whether to disable language variant conversion.
Conversion script between Latin and Tifinagh for Tachelhit.
There are two levels of conversion for Serbian: the script level (Cyrillics <-> Latin),...
Definition: SrConverter.php:33
Stub object for the user language.
Converts Tajiki to Latin orthography.
Definition: TgConverter.php:29
A trivial language converter.
The shared interface for all language converters.
if(!isset( $args[0])) $lang