1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
use MediaWiki\Language\Language;
use MediaWiki\Language\ReplacementArray;
use MediaWiki\Languages\Data\CrhExceptions;
use MediaWiki\StubObject\StubUserLang;
/**
* Crimean Tatar (Qırımtatarca) converter routines.
*
* Adapted from https://crh.wikipedia.org/wiki/Qullan%C4%B1c%C4%B1:Don_Alessandro/Translit
*
* @ingroup Languages
*/
class CrhConverter extends LanguageConverterSpecific {
// Defines working character ranges
// Cyrillic
# Crimean Tatar Cyrillic uppercase
public const C_UC = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ';
# Crimean Tatar Cyrillic lowercase
public const C_LC = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
# Crimean Tatar Cyrillic + CÑ uppercase consonants
public const C_CONS_UC = 'БВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ';
# Crimean Tatar Cyrillic + CÑ lowercase consonants
public const C_CONS_LC = 'бвгджзйклмнпрстфхцчшщcñ';
# Crimean Tatar Cyrillic M-type consonants
public const C_M_CONS = 'бгкмшcБГКМШC';
// Crimean Tatar Cyrillic + CÑ consonants
public const C_CONS = 'бвгджзйклмнпрстфхцчшщcñБВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ';
// Latin
# Crimean Tatar Latin uppercase
public const L_UC = 'AÂBCÇDEFGĞHIİJKLMNÑOÖPQRSŞTUÜVYZ';
# Crimean Tatar Latin lowercase
public const L_LC = 'aâbcçdefgğhıijklmnñoöpqrsştuüvyz';
# Crimean Tatar Latin N-type upper case consonants
public const L_N_CONS_UC = 'ÇNRSTZ';
# Crimean Tatar Latin N-type lower case consonants
public const L_N_CONS_LC = 'çnrstz';
# Crimean Tatar Latin N-type consonants
public const L_N_CONS = 'çnrstzÇNRSTZ';
# Crimean Tatar Latin M-type consonants
public const L_M_CONS = 'bcgkmpşBCGKMPŞ';
# Crimean Tatar Latin uppercase consonants
public const L_CONS_UC = 'BCÇDFGĞHJKLMNÑPQRSŞTVZ';
# Crimean Tatar Latin lowercase consonants
public const L_CONS_LC = 'bcçdfgğhjklmnñpqrsştvz';
# Crimean Tatar Latin consonants
public const L_CONS = 'bcçdfgğhjklmnñpqrsştvzBCÇDFGĞHJKLMNÑPQRSŞTVZ';
# Crimean Tatar Latin uppercase vowels
public const L_VOW_UC = 'AÂEIİOÖUÜ';
# Crimean Tatar Latin vowels
public const L_VOW = 'aâeıioöuüAÂEIİOÖUÜ';
# Crimean Tatar Latin uppercase front vowels
public const L_F_UC = 'EİÖÜ';
# Crimean Tatar Latin front vowels
public const L_F = 'eiöüEİÖÜ';
public function getMainCode(): string {
return 'crh';
}
public function getLanguageVariants(): array {
return [ 'crh', 'crh-cyrl', 'crh-latn' ];
}
public function getVariantsFallbacks(): array {
return [
'crh' => 'crh-latn',
'crh-cyrl' => 'crh-latn',
'crh-latn' => 'crh-cyrl',
];
}
/**
* @param Language|StubUserLang $langobj
*/
public function __construct( $langobj ) {
parent::__construct( $langobj );
// No point delaying this since they're in code.
// Waiting until loadDefaultTables() means they never get loaded
// when the tables themselves are loaded from the cache.
$this->loadExceptions();
}
public const CYRILLIC_TO_LATIN = [
## these are independent of location in the word, but have
## to go first so other transforms don't bleed them
'гъ' => 'ğ', 'Гъ' => 'Ğ', 'ГЪ' => 'Ğ',
'къ' => 'q', 'Къ' => 'Q', 'КЪ' => 'Q',
'нъ' => 'ñ', 'Нъ' => 'Ñ', 'НЪ' => 'Ñ',
'дж' => 'c', 'Дж' => 'C', 'ДЖ' => 'C',
'А' => 'A', 'а' => 'a', 'Б' => 'B', 'б' => 'b',
'В' => 'V', 'в' => 'v', 'Г' => 'G', 'г' => 'g',
'Д' => 'D', 'д' => 'd', 'Ж' => 'J', 'ж' => 'j',
'З' => 'Z', 'з' => 'z', 'И' => 'İ', 'и' => 'i',
'Й' => 'Y', 'й' => 'y', 'К' => 'K', 'к' => 'k',
'Л' => 'L', 'л' => 'l', 'М' => 'M', 'м' => 'm',
'Н' => 'N', 'н' => 'n', 'П' => 'P', 'п' => 'p',
'Р' => 'R', 'р' => 'r', 'С' => 'S', 'с' => 's',
'Т' => 'T', 'т' => 't', 'Ф' => 'F', 'ф' => 'f',
'Х' => 'H', 'х' => 'h', 'Ч' => 'Ç', 'ч' => 'ç',
'Ш' => 'Ş', 'ш' => 'ş', 'Ы' => 'I', 'ы' => 'ı',
'Э' => 'E', 'э' => 'e', 'Е' => 'E', 'е' => 'e',
'Я' => 'Â', 'я' => 'â', 'У' => 'U', 'у' => 'u',
'О' => 'O', 'о' => 'o',
'Ё' => 'Yo', 'ё' => 'yo', 'Ю' => 'Yu', 'ю' => 'yu',
'Ц' => 'Ts', 'ц' => 'ts', 'Щ' => 'Şç', 'щ' => 'şç',
'Ь' => '', 'ь' => '', 'Ъ' => '', 'ъ' => '',
];
public const LATIN_TO_CYRILLIC = [
'Â' => 'Я', 'â' => 'я', 'B' => 'Б', 'b' => 'б',
'Ç' => 'Ч', 'ç' => 'ч', 'D' => 'Д', 'd' => 'д',
'F' => 'Ф', 'f' => 'ф', 'G' => 'Г', 'g' => 'г',
'H' => 'Х', 'h' => 'х', 'I' => 'Ы', 'ı' => 'ы',
'İ' => 'И', 'i' => 'и', 'J' => 'Ж', 'j' => 'ж',
'K' => 'К', 'k' => 'к', 'L' => 'Л', 'l' => 'л',
'M' => 'М', 'm' => 'м', 'N' => 'Н', 'n' => 'н',
'O' => 'О', 'o' => 'о', 'P' => 'П', 'p' => 'п',
'R' => 'Р', 'r' => 'р', 'S' => 'С', 's' => 'с',
'Ş' => 'Ш', 'ş' => 'ш', 'T' => 'Т', 't' => 'т',
'V' => 'В', 'v' => 'в', 'Z' => 'З', 'z' => 'з',
'ya' => 'я', 'Ya' => 'Я', 'YA' => 'Я',
'ye' => 'е', 'YE' => 'Е', 'Ye' => 'Е',
// hack, hack, hack
'A' => 'А', 'a' => 'а', 'E' => 'Е', 'e' => 'е',
'Ö' => 'Ё', 'ö' => 'ё', 'U' => 'У', 'u' => 'у',
'Ü' => 'Ю', 'ü' => 'ю', 'Y' => 'Й', 'y' => 'й',
'C' => 'Дж', 'c' => 'дж', 'Ğ' => 'Гъ', 'ğ' => 'гъ',
'Ñ' => 'Нъ', 'ñ' => 'нъ', 'Q' => 'Къ', 'q' => 'къ',
];
/** @var string[] */
private array $mCyrl2LatnExceptions = [];
/** @var string[] */
private array $mLatn2CyrlExceptions = [];
/** @var string[] */
private array $mCyrl2LatnPatterns = [];
/** @var string[] */
private array $mLatn2CyrlPatterns = [];
/** @var string[] */
private array $mCyrlCleanUpRegexes = [];
private bool $mExceptionsLoaded = false;
/**
* @inheritDoc
*/
protected function loadDefaultTables(): array {
return [
'crh-latn' => new ReplacementArray( self::CYRILLIC_TO_LATIN ),
'crh-cyrl' => new ReplacementArray( self::LATIN_TO_CYRILLIC ),
'crh' => new ReplacementArray()
];
}
private function loadExceptions() {
if ( $this->mExceptionsLoaded ) {
return;
}
$this->mExceptionsLoaded = true;
$crhExceptions = new CrhExceptions();
[ $this->mCyrl2LatnExceptions, $this->mLatn2CyrlExceptions,
$this->mCyrl2LatnPatterns, $this->mLatn2CyrlPatterns, $this->mCyrlCleanUpRegexes ] =
$crhExceptions->loadExceptions( self::L_LC . self::C_LC, self::L_UC . self::C_UC );
}
/**
* It translates text into variant, specials:
* - omitting roman numbers
*
* @param string $text
* @param string $toVariant
* @return string
*/
public function translate( $text, $toVariant ) {
switch ( $toVariant ) {
case 'crh-cyrl':
case 'crh-latn':
break;
default:
return $text;
}
$this->loadTables();
if ( !isset( $this->mTables[$toVariant] ) ) {
throw new LogicException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
}
switch ( $toVariant ) {
case 'crh-cyrl':
/* Check for roman numbers like VII, XIX...
* Only need to split on Roman numerals when converting to Cyrillic
* Lookahead assertion ensures $roman doesn't match the empty string, and
* non-period after the first "Roman" character allows initials to be converted
*/
$roman = '(?=[MDCLXVI]([^.]|$))M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})';
$breaks = '([^\w\x80-\xff])';
// allow for multiple Roman numerals in a row; rare but it happens
$romanRegex = '/^' . $roman . '$|^(' . $roman . $breaks . ')+|(' . $breaks . $roman . ')+$|' .
$breaks . '(' . $roman . $breaks . ')+/';
$matches = preg_split( $romanRegex, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
$mstart = 0;
$ret = '';
foreach ( $matches as $m ) {
// copy over Roman numerals
$ret .= substr( $text, $mstart, (int)$m[1] - $mstart );
// process everything else
if ( $m[0] !== '' ) {
$ret .= $this->regsConverter( $m[0], $toVariant );
}
$mstart = (int)$m[1] + strlen( $m[0] );
}
return $ret;
default:
// Just process the whole string in one go
return $this->regsConverter( $text, $toVariant );
}
}
private function regsConverter( string $text, string $toVariant ): string {
if ( $text == '' ) {
return $text;
}
switch ( $toVariant ) {
case 'crh-latn':
$text = strtr( $text, $this->mCyrl2LatnExceptions );
foreach ( $this->mCyrl2LatnPatterns as $pat => $rep ) {
$text = preg_replace( $pat, $rep, $text );
}
$text = parent::translate( $text, $toVariant );
return strtr( $text, [ '«' => '"', '»' => '"', ] );
case 'crh-cyrl':
$text = strtr( $text, $this->mLatn2CyrlExceptions );
foreach ( $this->mLatn2CyrlPatterns as $pat => $rep ) {
$text = preg_replace( $pat, $rep, $text );
}
$text = parent::translate( $text, $toVariant );
$text = strtr( $text, [ '“' => '«', '”' => '»', ] );
foreach ( $this->mCyrlCleanUpRegexes as $pat => $rep ) {
$text = preg_replace( $pat, $rep, $text );
}
return $text;
default:
return $text;
}
}
}
|