| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Language\Language; |
| 22 |
use MediaWiki\Languages\Data\CrhExceptions; |
| 23 |
use MediaWiki\StubObject\StubUserLang; |
| 24 |
|
| 25 |
|
| 26 |
s. |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class CrhConverter extends LanguageConverterSpecific { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public const C_UC = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'; |
| 38 |
|
| 39 |
public const C_LC = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'; |
| 40 |
s |
| 41 |
public const C_CONS_UC = 'БВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ'; |
| 42 |
s |
| 43 |
public const C_CONS_LC = 'бвгджзйклмнпрстфхцчшщcñ'; |
| 44 |
|
| 45 |
public const C_M_CONS = 'бгкмшcБГКМШC'; |
| 46 |
|
| 47 |
s |
| 48 |
public const C_CONS = 'бвгджзйклмнпрстфхцчшщcñБВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ'; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public const L_UC = 'AÂBCÇDEFGĞHIİJKLMNÑOÖPQRSŞTUÜVYZ'; |
| 53 |
|
| 54 |
public const L_LC = 'aâbcçdefgğhıijklmnñoöpqrsştuüvyz'; |
| 55 |
|
| 56 |
public const L_N_CONS_UC = 'ÇNRSTZ'; |
| 57 |
|
| 58 |
public const L_N_CONS_LC = 'çnrstz'; |
| 59 |
|
| 60 |
public const L_N_CONS = 'çnrstzÇNRSTZ'; |
| 61 |
|
| 62 |
public const L_M_CONS = 'bcgkmpşBCGKMPŞ'; |
| 63 |
|
| 64 |
public const L_CONS_UC = 'BCÇDFGĞHJKLMNÑPQRSŞTVZ'; |
| 65 |
|
| 66 |
public const L_CONS_LC = 'bcçdfgğhjklmnñpqrsştvz'; |
| 67 |
|
| 68 |
public const L_CONS = 'bcçdfgğhjklmnñpqrsştvzBCÇDFGĞHJKLMNÑPQRSŞTVZ'; |
| 69 |
|
| 70 |
public const L_VOW_UC = 'AÂEIİOÖUÜ'; |
| 71 |
|
| 72 |
public const L_VOW = 'aâeıioöuüAÂEIİOÖUÜ'; |
| 73 |
|
| 74 |
public const L_F_UC = 'EİÖÜ'; |
| 75 |
|
| 76 |
public const L_F = 'eiöüEİÖÜ'; |
| 77 |
|
| 78 |
public function getMainCode(): string { |
| 79 |
return 'crh'; |
| 80 |
} |
| 81 |
|
| 82 |
public function getLanguageVariants(): array { |
| 83 |
return [ 'crh', 'crh-cyrl', 'crh-latn' ]; |
| 84 |
} |
| 85 |
|
| 86 |
public function getVariantsFallbacks(): array { |
| 87 |
return [ |
| 88 |
'crh' => 'crh-latn', |
| 89 |
'crh-cyrl' => 'crh-latn', |
| 90 |
'crh-latn' => 'crh-cyrl', |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function __construct( $langobj ) { |
| 98 |
parent::__construct( $langobj ); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
$this->loadExceptions(); |
| 104 |
} |
| 105 |
|
| 106 |
public const CYRILLIC_TO_LATIN = [ |
| 107 |
|
| 108 |
|
| 109 |
'гъ' => 'ğ', 'Гъ' => 'Ğ', 'ГЪ' => 'Ğ', |
| 110 |
'къ' => 'q', 'Къ' => 'Q', 'КЪ' => 'Q', |
| 111 |
'нъ' => 'ñ', 'Нъ' => 'Ñ', 'НЪ' => 'Ñ', |
| 112 |
'дж' => 'c', 'Дж' => 'C', 'ДЖ' => 'C', |
| 113 |
|
| 114 |
'А' => 'A', 'а' => 'a', 'Б' => 'B', 'б' => 'b', |
| 115 |
'В' => 'V', 'в' => 'v', 'Г' => 'G', 'г' => 'g', |
| 116 |
'Д' => 'D', 'д' => 'd', 'Ж' => 'J', 'ж' => 'j', |
| 117 |
'З' => 'Z', 'з' => 'z', 'И' => 'İ', 'и' => 'i', |
| 118 |
'Й' => 'Y', 'й' => 'y', 'К' => 'K', 'к' => 'k', |
| 119 |
'Л' => 'L', 'л' => 'l', 'М' => 'M', 'м' => 'm', |
| 120 |
'Н' => 'N', 'н' => 'n', 'П' => 'P', 'п' => 'p', |
| 121 |
'Р' => 'R', 'р' => 'r', 'С' => 'S', 'с' => 's', |
| 122 |
'Т' => 'T', 'т' => 't', 'Ф' => 'F', 'ф' => 'f', |
| 123 |
'Х' => 'H', 'х' => 'h', 'Ч' => 'Ç', 'ч' => 'ç', |
| 124 |
'Ш' => 'Ş', 'ш' => 'ş', 'Ы' => 'I', 'ы' => 'ı', |
| 125 |
'Э' => 'E', 'э' => 'e', 'Е' => 'E', 'е' => 'e', |
| 126 |
'Я' => 'Â', 'я' => 'â', 'У' => 'U', 'у' => 'u', |
| 127 |
'О' => 'O', 'о' => 'o', |
| 128 |
|
| 129 |
'Ё' => 'Yo', 'ё' => 'yo', 'Ю' => 'Yu', 'ю' => 'yu', |
| 130 |
'Ц' => 'Ts', 'ц' => 'ts', 'Щ' => 'Şç', 'щ' => 'şç', |
| 131 |
'Ь' => '', 'ь' => '', 'Ъ' => '', 'ъ' => '', |
| 132 |
]; |
| 133 |
|
| 134 |
public const LATIN_TO_CYRILLIC = [ |
| 135 |
'Â' => 'Я', 'â' => 'я', 'B' => 'Б', 'b' => 'б', |
| 136 |
'Ç' => 'Ч', 'ç' => 'ч', 'D' => 'Д', 'd' => 'д', |
| 137 |
'F' => 'Ф', 'f' => 'ф', 'G' => 'Г', 'g' => 'г', |
| 138 |
'H' => 'Х', 'h' => 'х', 'I' => 'Ы', 'ı' => 'ы', |
| 139 |
'İ' => 'И', 'i' => 'и', 'J' => 'Ж', 'j' => 'ж', |
| 140 |
'K' => 'К', 'k' => 'к', 'L' => 'Л', 'l' => 'л', |
| 141 |
'M' => 'М', 'm' => 'м', 'N' => 'Н', 'n' => 'н', |
| 142 |
'O' => 'О', 'o' => 'о', 'P' => 'П', 'p' => 'п', |
| 143 |
'R' => 'Р', 'r' => 'р', 'S' => 'С', 's' => 'с', |
| 144 |
'Ş' => 'Ш', 'ş' => 'ш', 'T' => 'Т', 't' => 'т', |
| 145 |
'V' => 'В', 'v' => 'в', 'Z' => 'З', 'z' => 'з', |
| 146 |
|
| 147 |
'ya' => 'я', 'Ya' => 'Я', 'YA' => 'Я', |
| 148 |
'ye' => 'е', 'YE' => 'Е', 'Ye' => 'Е', |
| 149 |
|
| 150 |
|
| 151 |
'A' => 'А', 'a' => 'а', 'E' => 'Е', 'e' => 'е', |
| 152 |
'Ö' => 'Ё', 'ö' => 'ё', 'U' => 'У', 'u' => 'у', |
| 153 |
'Ü' => 'Ю', 'ü' => 'ю', 'Y' => 'Й', 'y' => 'й', |
| 154 |
'C' => 'Дж', 'c' => 'дж', 'Ğ' => 'Гъ', 'ğ' => 'гъ', |
| 155 |
'Ñ' => 'Нъ', 'ñ' => 'нъ', 'Q' => 'Къ', 'q' => 'къ', |
| 156 |
]; |
| 157 |
|
| 158 |
|
| 159 |
private array $mCyrl2LatnExceptions = []; |
| 160 |
|
| 161 |
private array $mLatn2CyrlExceptions = []; |
| 162 |
|
| 163 |
|
| 164 |
private array $mCyrl2LatnPatterns = []; |
| 165 |
|
| 166 |
private array $mLatn2CyrlPatterns = []; |
| 167 |
|
| 168 |
|
| 169 |
private array $mCyrlCleanUpRegexes = []; |
| 170 |
|
| 171 |
private bool $mExceptionsLoaded = false; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
protected function loadDefaultTables(): array { |
| 177 |
return [ |
| 178 |
'crh-latn' => new ReplacementArray( self::CYRILLIC_TO_LATIN ), |
| 179 |
'crh-cyrl' => new ReplacementArray( self::LATIN_TO_CYRILLIC ), |
| 180 |
'crh' => new ReplacementArray() |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
private function loadExceptions() { |
| 185 |
if ( $this->mExceptionsLoaded ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$this->mExceptionsLoaded = true; |
| 190 |
$crhExceptions = new CrhExceptions(); |
| 191 |
[ $this->mCyrl2LatnExceptions, $this->mLatn2CyrlExceptions, |
| 192 |
$this->mCyrl2LatnPatterns, $this->mLatn2CyrlPatterns, $this->mCyrlCleanUpRegexes ] = |
| 193 |
$crhExceptions->loadExceptions( self::L_LC . self::C_LC, self::L_UC . self::C_UC ); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function translate( $text, $toVariant ) { |
| 205 |
switch ( $toVariant ) { |
| 206 |
case 'crh-cyrl': |
| 207 |
case 'crh-latn': |
| 208 |
break; |
| 209 |
default: |
| 210 |
return $text; |
| 211 |
} |
| 212 |
|
| 213 |
$this->loadTables(); |
| 214 |
|
| 215 |
if ( !isset( $this->mTables[$toVariant] ) ) { |
| 216 |
throw new LogicException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) ); |
| 217 |
} |
| 218 |
|
| 219 |
switch ( $toVariant ) { |
| 220 |
case 'crh-cyrl': |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
$roman = '(?=[MDCLXVI]([^.]|$))M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})'; |
| 227 |
|
| 228 |
$breaks = '([^\w\x80-\xff])'; |
| 229 |
|
| 230 |
|
| 231 |
$romanRegex = '/^' . $roman . '$|^(' . $roman . $breaks . ')+|(' . $breaks . $roman . ')+$|' . |
| 232 |
$breaks . '(' . $roman . $breaks . ')+/'; |
| 233 |
|
| 234 |
$matches = preg_split( $romanRegex, $text, -1, PREG_SPLIT_OFFSET_CAPTURE ); |
| 235 |
$mstart = 0; |
| 236 |
$ret = ''; |
| 237 |
foreach ( $matches as $m ) { |
| 238 |
|
| 239 |
$ret .= substr( $text, $mstart, (int)$m[1] - $mstart ); |
| 240 |
|
| 241 |
|
| 242 |
if ( $m[0] !== '' ) { |
| 243 |
$ret .= $this->regsConverter( $m[0], $toVariant ); |
| 244 |
} |
| 245 |
|
| 246 |
$mstart = (int)$m[1] + strlen( $m[0] ); |
| 247 |
} |
| 248 |
|
| 249 |
return $ret; |
| 250 |
|
| 251 |
default: |
| 252 |
|
| 253 |
return $this->regsConverter( $text, $toVariant ); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
private function regsConverter( $text, $toVariant ) { |
| 258 |
if ( $text == '' ) { |
| 259 |
return $text; |
| 260 |
} |
| 261 |
|
| 262 |
switch ( $toVariant ) { |
| 263 |
case 'crh-latn': |
| 264 |
$text = strtr( $text, $this->mCyrl2LatnExceptions ); |
| 265 |
foreach ( $this->mCyrl2LatnPatterns as $pat => $rep ) { |
| 266 |
$text = preg_replace( $pat, $rep, $text ); |
| 267 |
} |
| 268 |
$text = parent::translate( $text, $toVariant ); |
| 269 |
return strtr( $text, [ '«' => '"', '»' => '"', ] ); |
| 270 |
|
| 271 |
case 'crh-cyrl': |
| 272 |
$text = strtr( $text, $this->mLatn2CyrlExceptions ); |
| 273 |
foreach ( $this->mLatn2CyrlPatterns as $pat => $rep ) { |
| 274 |
$text = preg_replace( $pat, $rep, $text ); |
| 275 |
} |
| 276 |
$text = parent::translate( $text, $toVariant ); |
| 277 |
$text = strtr( $text, [ '“' => '«', '”' => '»', ] ); |
| 278 |
foreach ( $this->mCyrlCleanUpRegexes as $pat => $rep ) { |
| 279 |
$text = preg_replace( $pat, $rep, $text ); |
| 280 |
} |
| 281 |
return $text; |
| 282 |
|
| 283 |
default: |
| 284 |
return $text; |
| 285 |
} |
| 286 |
} |
| 287 |
} |