MediaWiki  master
LanguageCrh.php
Go to the documentation of this file.
1 <?php
31 class CrhConverter extends LanguageConverter {
32  // Defines working character ranges
33 
34  // Cyrillic
35  const C_UC = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'; # Crimean Tatar Cyrillic uppercase
36  const C_LC = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'; # Crimean Tatar Cyrillic lowercase
37  const C_CONS_UC = 'БВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ'; # Crimean Tatar Cyrillic + CÑ uppercase consonants
38  const C_CONS_LC = 'бвгджзйклмнпрстфхцчшщcñ'; # Crimean Tatar Cyrillic + CÑ lowercase consonants
39  const C_M_CONS = 'бгкмшcБГКМШC'; # Crimean Tatar Cyrillic M-type consonants
40 
41  // Crimean Tatar Cyrillic + CÑ consonants
42  const C_CONS = 'бвгджзйклмнпрстфхцчшщcñБВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ';
43 
44  // Latin
45  const L_UC = 'AÂBCÇDEFGĞHIİJKLMNÑOÖPQRSŞTUÜVYZ'; # Crimean Tatar Latin uppercase
46  const L_LC = 'aâbcçdefgğhıijklmnñoöpqrsştuüvyz'; # Crimean Tatar Latin lowercase
47  const L_N_CONS_UC = 'ÇNRSTZ'; # Crimean Tatar Latin N-type upper case consonants
48  const L_N_CONS_LC = 'çnrstz'; # Crimean Tatar Latin N-type lower case consonants
49  const L_N_CONS = 'çnrstzÇNRSTZ'; # Crimean Tatar Latin N-type consonants
50  const L_M_CONS = 'bcgkmpşBCGKMPŞ'; # Crimean Tatar Latin M-type consonants
51  const L_CONS_UC = 'BCÇDFGĞHJKLMNÑPQRSŞTVZ'; # Crimean Tatar Latin uppercase consonants
52  const L_CONS_LC = 'bcçdfgğhjklmnñpqrsştvz'; # Crimean Tatar Latin lowercase consonants
53  const L_CONS = 'bcçdfgğhjklmnñpqrsştvzBCÇDFGĞHJKLMNÑPQRSŞTVZ'; # Crimean Tatar Latin consonants
54  const L_VOW_UC = 'AÂEIİOÖUÜ'; # Crimean Tatar Latin uppercase vowels
55  const L_VOW = 'aâeıioöuüAÂEIİOÖUÜ'; # Crimean Tatar Latin vowels
56  const L_F_UC = 'EİÖÜ'; # Crimean Tatar Latin uppercase front vowels
57  const L_F = 'eiöüEİÖÜ'; # Crimean Tatar Latin front vowels
58 
66  public function __construct( Language $langobj, $maincode,
67  $variants = [],
68  $variantfallbacks = [],
69  $flags = []
70  ) {
71  parent::__construct( $langobj, $maincode,
72  $variants, $variantfallbacks, $flags );
73 
74  // No point delaying this since they're in code.
75  // Waiting until loadDefaultTables() means they never get loaded
76  // when the tables themselves are loaded from cache.
77  $this->loadExceptions();
78  }
79 
80  public $mCyrillicToLatin = [
81 
82  ## these are independent of location in the word, but have
83  ## to go first so other transforms don't bleed them
84  'гъ' => 'ğ', 'Гъ' => 'Ğ', 'ГЪ' => 'Ğ',
85  'къ' => 'q', 'Къ' => 'Q', 'КЪ' => 'Q',
86  'нъ' => 'ñ', 'Нъ' => 'Ñ', 'НЪ' => 'Ñ',
87  'дж' => 'c', 'Дж' => 'C', 'ДЖ' => 'C',
88 
89  'А' => 'A', 'а' => 'a', 'Б' => 'B', 'б' => 'b',
90  'В' => 'V', 'в' => 'v', 'Г' => 'G', 'г' => 'g',
91  'Д' => 'D', 'д' => 'd', 'Ж' => 'J', 'ж' => 'j',
92  'З' => 'Z', 'з' => 'z', 'И' => 'İ', 'и' => 'i',
93  'Й' => 'Y', 'й' => 'y', 'К' => 'K', 'к' => 'k',
94  'Л' => 'L', 'л' => 'l', 'М' => 'M', 'м' => 'm',
95  'Н' => 'N', 'н' => 'n', 'П' => 'P', 'п' => 'p',
96  'Р' => 'R', 'р' => 'r', 'С' => 'S', 'с' => 's',
97  'Т' => 'T', 'т' => 't', 'Ф' => 'F', 'ф' => 'f',
98  'Х' => 'H', 'х' => 'h', 'Ч' => 'Ç', 'ч' => 'ç',
99  'Ш' => 'Ş', 'ш' => 'ş', 'Ы' => 'I', 'ы' => 'ı',
100  'Э' => 'E', 'э' => 'e', 'Е' => 'E', 'е' => 'e',
101  'Я' => 'Â', 'я' => 'â', 'У' => 'U', 'у' => 'u',
102  'О' => 'O', 'о' => 'o',
103 
104  'Ё' => 'Yo', 'ё' => 'yo', 'Ю' => 'Yu', 'ю' => 'yu',
105  'Ц' => 'Ts', 'ц' => 'ts', 'Щ' => 'Şç', 'щ' => 'şç',
106  'Ь' => '', 'ь' => '', 'Ъ' => '', 'ъ' => '',
107 
108  ];
109 
110  public $mLatinToCyrillic = [
111  'Â' => 'Я', 'â' => 'я', 'B' => 'Б', 'b' => 'б',
112  'Ç' => 'Ч', 'ç' => 'ч', 'D' => 'Д', 'd' => 'д',
113  'F' => 'Ф', 'f' => 'ф', 'G' => 'Г', 'g' => 'г',
114  'H' => 'Х', 'h' => 'х', 'I' => 'Ы', 'ı' => 'ы',
115  'İ' => 'И', 'i' => 'и', 'J' => 'Ж', 'j' => 'ж',
116  'K' => 'К', 'k' => 'к', 'L' => 'Л', 'l' => 'л',
117  'M' => 'М', 'm' => 'м', 'N' => 'Н', 'n' => 'н',
118  'O' => 'О', 'o' => 'о', 'P' => 'П', 'p' => 'п',
119  'R' => 'Р', 'r' => 'р', 'S' => 'С', 's' => 'с',
120  'Ş' => 'Ш', 'ş' => 'ш', 'T' => 'Т', 't' => 'т',
121  'V' => 'В', 'v' => 'в', 'Z' => 'З', 'z' => 'з',
122 
123  'ya' => 'я', 'Ya' => 'Я', 'YA' => 'Я',
124  'ye' => 'е', 'YE' => 'Е', 'Ye' => 'Е',
125 
126  // hack, hack, hack
127  'A' => 'А', 'a' => 'а', 'E' => 'Е', 'e' => 'е',
128  'Ö' => 'Ё', 'ö' => 'ё', 'U' => 'У', 'u' => 'у',
129  'Ü' => 'Ю', 'ü' => 'ю', 'Y' => 'Й', 'y' => 'й',
130  'C' => 'Дж', 'c' => 'дж', 'Ğ' => 'Гъ', 'ğ' => 'гъ',
131  'Ñ' => 'Нъ', 'ñ' => 'нъ', 'Q' => 'Къ', 'q' => 'къ',
132 
133  ];
134 
137 
138  public $mCyrl2LatnPatterns = [];
139  public $mLatn2CyrlPatterns = [];
140 
141  public $mCyrlCleanUpRegexes = [];
142 
143  public $mExceptionsLoaded = false;
144 
145  protected function loadDefaultTables() {
146  $this->mTables = [
147  'crh-latn' => new ReplacementArray( $this->mCyrillicToLatin ),
148  'crh-cyrl' => new ReplacementArray( $this->mLatinToCyrillic ),
149  'crh' => new ReplacementArray()
150  ];
151  }
152 
153  private function loadExceptions() {
154  if ( $this->mExceptionsLoaded ) {
155  return;
156  }
157 
158  $this->mExceptionsLoaded = true;
159  $crhExceptions = new MediaWiki\Languages\Data\CrhExceptions();
160  list( $this->mCyrl2LatnExceptions, $this->mLatn2CyrlExceptions,
161  $this->mCyrl2LatnPatterns, $this->mLatn2CyrlPatterns, $this->mCyrlCleanUpRegexes ) =
162  $crhExceptions->loadExceptions( self::L_LC . self::C_LC, self::L_UC . self::C_UC );
163  }
164 
175  public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
176  // check for user namespace
177  if ( is_object( $nt ) ) {
178  $ns = $nt->getNamespace();
179  if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
180  return;
181  }
182  }
183 
184  $oldlink = $link;
185  parent::findVariantLink( $link, $nt, $ignoreOtherCond );
186  if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
187  $link = $oldlink;
188  }
189  }
190 
201  public function translate( $text, $toVariant ) {
202  switch ( $toVariant ) {
203  case 'crh-cyrl':
204  case 'crh-latn':
205  break;
206  default:
207  return $text;
208  }
209 
210  if ( !$this->mTablesLoaded ) {
211  $this->loadTables();
212  }
213 
214  if ( !isset( $this->mTables[$toVariant] ) ) {
215  throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
216  }
217 
218  switch ( $toVariant ) {
219  case 'crh-cyrl':
220  /* Check for roman numbers like VII, XIX...
221  * Only need to split on Roman numerals when converting to Cyrillic
222  * Lookahead assertion ensures $roman doesn't match the empty string, and
223  * non-period after first "Roman" character allows initials to be converted
224  */
225  $roman = '(?=[MDCLXVI]([^.]|$))M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})';
226 
227  $breaks = '([^\w\x80-\xff])';
228 
229  // allow for multiple Roman numerals in a row; rare but it happens
230  $romanRegex = '/^' . $roman . '$|^(' . $roman . $breaks . ')+|(' . $breaks . $roman . ')+$|' .
231  $breaks . '(' . $roman . $breaks . ')+/';
232 
233  $matches = preg_split( $romanRegex, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
234  $mstart = 0;
235  $ret = '';
236  foreach ( $matches as $m ) {
237  // copy over Roman numerals
238  $ret .= substr( $text, $mstart, $m[1] - $mstart );
239 
240  // process everything else
241  if ( $m[0] !== '' ) {
242  $ret .= $this->regsConverter( $m[0], $toVariant );
243  }
244 
245  $mstart = $m[1] + strlen( $m[0] );
246  }
247 
248  return $ret;
249  default:
250  // Just process the whole string in one go
251  return $this->regsConverter( $text, $toVariant );
252  }
253  }
254 
255  private function regsConverter( $text, $toVariant ) {
256  if ( $text == '' ) return $text;
257 
258  $pat = [];
259  $rep = [];
260  switch ( $toVariant ) {
261  case 'crh-latn':
262  $text = strtr( $text, $this->mCyrl2LatnExceptions );
263  foreach ( $this->mCyrl2LatnPatterns as $pat => $rep ) {
264  $text = preg_replace( $pat, $rep, $text );
265  }
266  $text = parent::translate( $text, $toVariant );
267  $text = strtr( $text, [ '«' => '"', '»' => '"', ] );
268  return $text;
269  case 'crh-cyrl':
270  $text = strtr( $text, $this->mLatn2CyrlExceptions );
271  foreach ( $this->mLatn2CyrlPatterns as $pat => $rep ) {
272  $text = preg_replace( $pat, $rep, $text );
273  }
274  $text = parent::translate( $text, $toVariant );
275  $text = strtr( $text, [ '“' => '«', '”' => '»', ] );
276  foreach ( $this->mCyrlCleanUpRegexes as $pat => $rep ) {
277  $text = preg_replace( $pat, $rep, $text );
278  }
279  return $text;
280  default:
281  return $text;
282  }
283  }
284 
285 }
286 
292 class LanguageCrh extends Language {
293  protected function newConverter() : CrhConverter {
294  $variants = [ 'crh', 'crh-cyrl', 'crh-latn' ];
295  $variantfallbacks = [
296  'crh' => 'crh-latn',
297  'crh-cyrl' => 'crh-latn',
298  'crh-latn' => 'crh-cyrl',
299  ];
300 
301  return new CrhConverter( $this, 'crh', $variants, $variantfallbacks );
302  }
303 }
CrhConverter\$mCyrl2LatnExceptions
$mCyrl2LatnExceptions
Definition: LanguageCrh.php:135
MediaWiki\Languages\Data\CrhExceptions
Definition: CrhExceptions.php:14
CrhConverter\L_N_CONS
const L_N_CONS
Definition: LanguageCrh.php:49
LanguageCrh
Crimean Tatar (Qırımtatarca)
Definition: LanguageCrh.php:292
CrhConverter\loadExceptions
loadExceptions()
Definition: LanguageCrh.php:153
CrhConverter\C_M_CONS
const C_M_CONS
Definition: LanguageCrh.php:39
CrhConverter\L_CONS_LC
const L_CONS_LC
Definition: LanguageCrh.php:52
CrhConverter\$mLatinToCyrillic
$mLatinToCyrillic
Definition: LanguageCrh.php:110
CrhConverter\loadDefaultTables
loadDefaultTables()
Definition: LanguageCrh.php:145
CrhConverter\L_N_CONS_UC
const L_N_CONS_UC
Definition: LanguageCrh.php:47
MWException
MediaWiki exception.
Definition: MWException.php:26
CrhConverter\L_CONS_UC
const L_CONS_UC
Definition: LanguageCrh.php:51
CrhConverter\__construct
__construct(Language $langobj, $maincode, $variants=[], $variantfallbacks=[], $flags=[])
Definition: LanguageCrh.php:66
CrhConverter\L_CONS
const L_CONS
Definition: LanguageCrh.php:53
$matches
$matches
Definition: NoLocalSettings.php:24
CrhConverter\L_F_UC
const L_F_UC
Definition: LanguageCrh.php:56
CrhConverter\L_VOW_UC
const L_VOW_UC
Definition: LanguageCrh.php:54
CrhConverter\findVariantLink
findVariantLink(&$link, &$nt, $ignoreOtherCond=false)
A function wrapper:
Definition: LanguageCrh.php:175
CrhConverter\regsConverter
regsConverter( $text, $toVariant)
Definition: LanguageCrh.php:255
CrhConverter\C_CONS_UC
const C_CONS_UC
Definition: LanguageCrh.php:37
CrhConverter\$mCyrillicToLatin
$mCyrillicToLatin
Definition: LanguageCrh.php:80
CrhConverter\C_LC
const C_LC
Definition: LanguageCrh.php:36
CrhConverter\$mCyrl2LatnPatterns
$mCyrl2LatnPatterns
Definition: LanguageCrh.php:138
CrhConverter\$mLatn2CyrlExceptions
$mLatn2CyrlExceptions
Definition: LanguageCrh.php:136
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:63
ReplacementArray
Wrapper around strtr() that holds replacements.
Definition: ReplacementArray.php:24
CrhConverter\L_VOW
const L_VOW
Definition: LanguageCrh.php:55
CrhConverter\C_CONS_LC
const C_CONS_LC
Definition: LanguageCrh.php:38
CrhConverter\C_CONS
const C_CONS
Definition: LanguageCrh.php:42
CrhConverter\L_N_CONS_LC
const L_N_CONS_LC
Definition: LanguageCrh.php:48
CrhConverter
Crimean Tatar (Qırımtatarca) converter routines.
Definition: LanguageCrh.php:31
LanguageCrh\newConverter
newConverter()
Construct a new LanguageConverter suitable for this language.
Definition: LanguageCrh.php:293
CrhConverter\L_LC
const L_LC
Definition: LanguageCrh.php:46
CrhConverter\$mExceptionsLoaded
$mExceptionsLoaded
Definition: LanguageCrh.php:143
CrhConverter\L_M_CONS
const L_M_CONS
Definition: LanguageCrh.php:50
CrhConverter\translate
translate( $text, $toVariant)
It translates text into variant, specials:
Definition: LanguageCrh.php:201
CrhConverter\$mCyrlCleanUpRegexes
$mCyrlCleanUpRegexes
Definition: LanguageCrh.php:141
CrhConverter\$mLatn2CyrlPatterns
$mLatn2CyrlPatterns
Definition: LanguageCrh.php:139
NS_USER
const NS_USER
Definition: Defines.php:62
CrhConverter\L_UC
const L_UC
Definition: LanguageCrh.php:45
Language
Internationalisation code.
Definition: Language.php:39
CrhConverter\C_UC
const C_UC
Definition: LanguageCrh.php:35
CrhConverter\L_F
const L_F
Definition: LanguageCrh.php:57