3 * Crimean Tatar (Qırımtatarca) specific code.
5 * Adapted from https://crh.wikipedia.org/wiki/Qullan%C4%B1c%C4%B1:Don_Alessandro/Translit
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
27 * Crimean Tatar (Qırımtatarca) converter routines
31 class CrhConverter
extends LanguageConverter
{
32 // Defines working character ranges
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
41 // Crimean Tatar Cyrillic + CÑ consonants
42 const C_CONS
= 'бвгджзйклмнпрстфхцчшщcñБВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ';
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
60 * @param Language $langobj
61 * @param string $maincode
62 * @param array $variants
63 * @param array $variantfallbacks
66 function __construct( $langobj, $maincode,
68 $variantfallbacks = [],
70 parent
::__construct( $langobj, $maincode,
71 $variants, $variantfallbacks, $flags );
73 // No point delaying this since they're in code.
74 // Waiting until loadDefaultTables() means they never get loaded
75 // when the tables themselves are loaded from cache.
76 $this->loadExceptions();
79 public $mCyrillicToLatin = [
81 ## these are independent of location in the word, but have
82 ## to go first so other transforms don't bleed them
83 'гъ' => 'ğ', 'Гъ' => 'Ğ', 'ГЪ' => 'Ğ',
84 'къ' => 'q', 'Къ' => 'Q', 'КЪ' => 'Q',
85 'нъ' => 'ñ', 'Нъ' => 'Ñ', 'НЪ' => 'Ñ',
86 'дж' => 'c', 'Дж' => 'C', 'ДЖ' => 'C',
88 'А' => 'A', 'а' => 'a', 'Б' => 'B', 'б' => 'b',
89 'В' => 'V', 'в' => 'v', 'Г' => 'G', 'г' => 'g',
90 'Д' => 'D', 'д' => 'd', 'Ж' => 'J', 'ж' => 'j',
91 'З' => 'Z', 'з' => 'z', 'И' => 'İ', 'и' => 'i',
92 'Й' => 'Y', 'й' => 'y', 'К' => 'K', 'к' => 'k',
93 'Л' => 'L', 'л' => 'l', 'М' => 'M', 'м' => 'm',
94 'Н' => 'N', 'н' => 'n', 'П' => 'P', 'п' => 'p',
95 'Р' => 'R', 'р' => 'r', 'С' => 'S', 'с' => 's',
96 'Т' => 'T', 'т' => 't', 'Ф' => 'F', 'ф' => 'f',
97 'Х' => 'H', 'х' => 'h', 'Ч' => 'Ç', 'ч' => 'ç',
98 'Ш' => 'Ş', 'ш' => 'ş', 'Ы' => 'I', 'ы' => 'ı',
99 'Э' => 'E', 'э' => 'e', 'Е' => 'E', 'е' => 'e',
100 'Я' => 'Â', 'я' => 'â', 'У' => 'U', 'у' => 'u',
101 'О' => 'O', 'о' => 'o',
103 'Ё' => 'Yo', 'ё' => 'yo', 'Ю' => 'Yu', 'ю' => 'yu',
104 'Ц' => 'Ts', 'ц' => 'ts', 'Щ' => 'Şç', 'щ' => 'şç',
105 'Ь' => '', 'ь' => '', 'Ъ' => '', 'ъ' => '',
109 public $mLatinToCyrillic = [
110 'Â' => 'Я', 'â' => 'я', 'B' => 'Б', 'b' => 'б',
111 'Ç' => 'Ч', 'ç' => 'ч', 'D' => 'Д', 'd' => 'д',
112 'F' => 'Ф', 'f' => 'ф', 'G' => 'Г', 'g' => 'г',
113 'H' => 'Х', 'h' => 'х', 'I' => 'Ы', 'ı' => 'ы',
114 'İ' => 'И', 'i' => 'и', 'J' => 'Ж', 'j' => 'ж',
115 'K' => 'К', 'k' => 'к', 'L' => 'Л', 'l' => 'л',
116 'M' => 'М', 'm' => 'м', 'N' => 'Н', 'n' => 'н',
117 'O' => 'О', 'o' => 'о', 'P' => 'П', 'p' => 'п',
118 'R' => 'Р', 'r' => 'р', 'S' => 'С', 's' => 'с',
119 'Ş' => 'Ш', 'ş' => 'ш', 'T' => 'Т', 't' => 'т',
120 'V' => 'В', 'v' => 'в', 'Z' => 'З', 'z' => 'з',
122 'ya' => 'я', 'Ya' => 'Я', 'YA' => 'Я',
123 'ye' => 'е', 'YE' => 'Е', 'Ye' => 'Е',
126 'A' => 'А', 'a' => 'а', 'E' => 'Е', 'e' => 'е',
127 'Ö' => 'Ё', 'ö' => 'ё', 'U' => 'У', 'u' => 'у',
128 'Ü' => 'Ю', 'ü' => 'ю', 'Y' => 'Й', 'y' => 'й',
129 'C' => 'Дж', 'c' => 'дж', 'Ğ' => 'Гъ', 'ğ' => 'гъ',
130 'Ñ' => 'Нъ', 'ñ' => 'нъ', 'Q' => 'Къ', 'q' => 'къ',
134 public $mCyrl2LatnExceptions = [];
135 public $mLatn2CyrlExceptions = [];
137 public $mCyrl2LatnPatterns = [];
138 public $mLatn2CyrlPatterns = [];
140 public $mCyrlCleanUpRegexes = [];
142 public $mExceptionsLoaded = false;
144 function loadDefaultTables() {
146 'crh-latn' => new ReplacementArray( $this->mCyrillicToLatin
),
147 'crh-cyrl' => new ReplacementArray( $this->mLatinToCyrillic
),
148 'crh' => new ReplacementArray()
152 function loadExceptions() {
153 if ( $this->mExceptionsLoaded
) {
157 $this->mExceptionsLoaded
= true;
158 $crhExceptions = new MediaWiki\Languages\Data\
CrhExceptions();
159 list( $this->mCyrl2LatnExceptions
, $this->mLatn2CyrlExceptions
,
160 $this->mCyrl2LatnPatterns
, $this->mLatn2CyrlPatterns
, $this->mCyrlCleanUpRegexes
) =
161 $crhExceptions->loadExceptions( self
::L_LC
. self
::C_LC
, self
::L_UC
. self
::C_UC
);
165 * A function wrapper:
166 * - if there is no selected variant, leave the link
168 * - do not try to find variants for usernames
170 * @param string &$link
172 * @param bool $ignoreOtherCond
174 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
175 // check for user namespace
176 if ( is_object( $nt ) ) {
177 $ns = $nt->getNamespace();
178 if ( $ns == NS_USER ||
$ns == NS_USER_TALK
) {
184 parent
::findVariantLink( $link, $nt, $ignoreOtherCond );
185 if ( $this->getPreferredVariant() == $this->mMainLanguageCode
) {
191 * It translates text into variant, specials:
192 * - omitting roman numbers
194 * @param string $text
195 * @param bool $toVariant
197 * @throws MWException
200 function translate( $text, $toVariant ) {
201 switch ( $toVariant ) {
209 if ( !$this->mTablesLoaded
) {
213 if ( !isset( $this->mTables
[$toVariant] ) ) {
214 throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables
) ) );
217 switch ( $toVariant ) {
219 /* Check for roman numbers like VII, XIX...
220 * Only need to split on Roman numerals when converting to Cyrillic
221 * Lookahead assertion ensures $roman doesn't match the empty string, and
222 * non-period after first "Roman" character allows initials to be converted
224 $roman = '(?=[MDCLXVI]([^.]|$))M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})';
226 $breaks = '([^\w\x80-\xff])';
228 // allow for multiple Roman numerals in a row; rare but it happens
229 $romanRegex = '/^' . $roman . '$|^(' . $roman . $breaks . ')+|(' . $breaks . $roman . ')+$|' .
230 $breaks . '(' . $roman . $breaks . ')+/';
232 $matches = preg_split( $romanRegex, $text, -1, PREG_SPLIT_OFFSET_CAPTURE
);
235 foreach ( $matches as $m ) {
236 // copy over Roman numerals
237 $ret .= substr( $text, $mstart, $m[1] - $mstart );
239 // process everything else
240 if ( $m[0] !== '' ) {
241 $ret .= $this->regsConverter( $m[0], $toVariant );
244 $mstart = $m[1] +
strlen( $m[0] );
249 // Just process the whole string in one go
250 return $this->regsConverter( $text, $toVariant );
254 private function regsConverter( $text, $toVariant ) {
255 if ( $text == '' ) return $text;
259 switch ( $toVariant ) {
261 $text = strtr( $text, $this->mCyrl2LatnExceptions
);
262 foreach ( $this->mCyrl2LatnPatterns
as $pat => $rep ) {
263 $text = preg_replace( $pat, $rep, $text );
265 $text = parent
::translate( $text, $toVariant );
266 $text = strtr( $text, [ '«' => '"', '»' => '"', ] );
269 $text = strtr( $text, $this->mLatn2CyrlExceptions
);
270 foreach ( $this->mLatn2CyrlPatterns
as $pat => $rep ) {
271 $text = preg_replace( $pat, $rep, $text );
273 $text = parent
::translate( $text, $toVariant );
274 $text = strtr( $text, [ '“' => '«', '”' => '»', ] );
275 foreach ( $this->mCyrlCleanUpRegexes
as $pat => $rep ) {
276 $text = preg_replace( $pat, $rep, $text );
287 * Crimean Tatar (Qırımtatarca)
291 class LanguageCrh
extends Language
{
293 function __construct() {
294 parent
::__construct();
296 $variants = [ 'crh', 'crh-cyrl', 'crh-latn' ];
297 $variantfallbacks = [
299 'crh-cyrl' => 'crh-latn',
300 'crh-latn' => 'crh-cyrl',
303 $this->mConverter
= new CrhConverter( $this, 'crh', $variants, $variantfallbacks );