| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * A class that extends LanguageConverterSpecific for converts that use |
| 23 | * ICU rule-based transliterators. |
| 24 | * |
| 25 | * @ingroup Language |
| 26 | */ |
| 27 | abstract class LanguageConverterIcu extends LanguageConverterSpecific { |
| 28 | |
| 29 | /** |
| 30 | * @var Transliterator[] |
| 31 | */ |
| 32 | protected $mTransliterators; |
| 33 | |
| 34 | /** |
| 35 | * Creates empty tables. mTransliterators will be used instead. |
| 36 | * |
| 37 | * @return array |
| 38 | */ |
| 39 | protected function loadDefaultTables(): array { |
| 40 | $tables = []; |
| 41 | foreach ( $this->getVariants() as $variant ) { |
| 42 | $tables[$variant] = new ReplacementArray(); |
| 43 | } |
| 44 | return $tables; |
| 45 | } |
| 46 | |
| 47 | public function translate( $text, $variant ) { |
| 48 | $text = parent::translate( $text, $variant ); |
| 49 | if ( trim( $text ) ) { |
| 50 | $text = $this->icuTranslate( $text, $variant ); |
| 51 | } |
| 52 | return $text; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Translate a string to a variant using ICU transliterator. |
| 57 | * |
| 58 | * @param string $text Text to convert |
| 59 | * @param string $variant Variant language code |
| 60 | * @return string Translated text |
| 61 | */ |
| 62 | public function icuTranslate( $text, $variant ) { |
| 63 | return $this->getTransliterators()[$variant]->transliterate( $text ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the array mapping variants to ICU transliteration rules. |
| 68 | * Subclasses must implement this. |
| 69 | * |
| 70 | * @return string[] |
| 71 | */ |
| 72 | abstract protected function getIcuRules(); |
| 73 | |
| 74 | /** |
| 75 | * Get the array mapping variants to ICU transliterators. |
| 76 | * |
| 77 | * @return Transliterator[] |
| 78 | */ |
| 79 | protected function getTransliterators() { |
| 80 | if ( $this->mTransliterators === null ) { |
| 81 | $this->mTransliterators = []; |
| 82 | foreach ( $this->getIcuRules() as $variant => $rule ) { |
| 83 | $this->mTransliterators[$variant] = Transliterator::createFromRules( $rule ); |
| 84 | } |
| 85 | foreach ( $this->getTransliteratorAliases() as $alias => $variant ) { |
| 86 | $this->mTransliterators[$alias] = $this->mTransliterators[$variant]; |
| 87 | } |
| 88 | } |
| 89 | return $this->mTransliterators; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the array mapping variant aliases to the main variant. |
| 94 | * |
| 95 | * @return string[] |
| 96 | */ |
| 97 | protected function getTransliteratorAliases() { |
| 98 | return []; |
| 99 | } |
| 100 | } |