MediaWiki  master
LanguageConverterIcu.php
Go to the documentation of this file.
1 <?php
28 
32  protected $mTransliterators;
33 
37  protected function loadDefaultTables() {
38  $this->mTables = [];
39  foreach ( $this->getVariants() as $variant ) {
40  $this->mTables[$variant] = new ReplacementArray();
41  }
42  }
43 
44  public function translate( $text, $variant ) {
45  $text = parent::translate( $text, $variant );
46  if ( trim( $text ) ) {
47  $text = $this->icuTranslate( $text, $variant );
48  }
49  return $text;
50  }
51 
59  public function icuTranslate( $text, $variant ) {
60  return $this->getTransliterators()[$variant]->transliterate( $text );
61  }
62 
69  abstract protected function getIcuRules();
70 
76  protected function getTransliterators() {
77  if ( $this->mTransliterators === null ) {
78  $this->mTransliterators = [];
79  foreach ( $this->getIcuRules() as $variant => $rule ) {
80  $this->mTransliterators[$variant] = Transliterator::createFromRules( $rule );
81  }
82  foreach ( $this->getTransliteratorAliases() as $alias => $variant ) {
83  $this->mTransliterators[$alias] = $this->mTransliterators[$variant];
84  }
85  }
87  }
88 
94  protected function getTransliteratorAliases() {
95  return [];
96  }
97 }
LanguageConverterIcu\loadDefaultTables
loadDefaultTables()
Creates empty tables.
Definition: LanguageConverterIcu.php:37
LanguageConverterIcu\getTransliteratorAliases
getTransliteratorAliases()
Get the array mapping variant aliases to main variant.
Definition: LanguageConverterIcu.php:94
LanguageConverter\getVariants
getVariants()
Get all valid variants for current Coverter.
Definition: LanguageConverter.php:256
LanguageConverterIcu
A class that extends LanguageConverterSpecific for converts that use ICU rule-based transliterators.
Definition: LanguageConverterIcu.php:27
LanguageConverterIcu\getIcuRules
getIcuRules()
Get the array mapping variants to ICU transliteration rules.
ReplacementArray
Wrapper around strtr() that holds replacements.
Definition: ReplacementArray.php:24
LanguageConverterIcu\translate
translate( $text, $variant)
Translate a string to a variant.
Definition: LanguageConverterIcu.php:44
LanguageConverterIcu\getTransliterators
getTransliterators()
Get the array mapping variants to ICU transliterators.
Definition: LanguageConverterIcu.php:76
LanguageConverterSpecific
A class that extends LanguageConverter with specific behaviour.
Definition: LanguageConverterSpecific.php:26
LanguageConverterIcu\$mTransliterators
Transliterator[] $mTransliterators
Definition: LanguageConverterIcu.php:32
LanguageConverterIcu\icuTranslate
icuTranslate( $text, $variant)
Translate a string to a variant using ICU transliterator.
Definition: LanguageConverterIcu.php:59