LanguageCrh.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * Crimean Tatar (Qırımtatarca) specific code.
  4. *
  5. * Adapted from https://crh.wikipedia.org/wiki/Qullan%C4%B1c%C4%B1:Don_Alessandro/Translit
  6. *
  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.
  11. *
  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.
  16. *
  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
  21. *
  22. * @file
  23. * @ingroup Language
  24. */
  25. /**
  26. * Crimean Tatar (Qırımtatarca) converter routines
  27. *
  28. * @ingroup Language
  29. */
  30. class CrhConverter extends LanguageConverter {
  31. // Defines working character ranges
  32. const WORD_BEGINS = '\r\s\"\'\(\)\-<>\[\]\/.,:;!?';
  33. const WORD_ENDS = '\r\s\"\'\(\)\-<>\[\]\/.,:;!?';
  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. # Crimean Tatar Cyrillic + CÑ consonants
  41. const C_CONS = 'бвгджзйклмнпрстфхцчшщcñБВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ';
  42. // Latin
  43. const L_UC = 'AÂBCÇDEFGĞHIİJKLMNÑOÖPQRSŞTUÜVYZ'; # Crimean Tatar Latin uppercase
  44. const L_LC = 'aâbcçdefgğhıijklmnñoöpqrsştuüvyz'; # Crimean Tatar Latin lowercase
  45. const L_N_CONS_UC = 'ÇNRSTZ'; # Crimean Tatar Latin N-type upper case consonants
  46. const L_N_CONS_LC = 'çnrstz'; # Crimean Tatar Latin N-type lower case consonants
  47. const L_N_CONS = 'çnrstzÇNRSTZ'; # Crimean Tatar Latin N-type consonants
  48. const L_M_CONS = 'bcgkmpşBCGKMPŞ'; # Crimean Tatar Latin M-type consonants
  49. const L_CONS_UC = 'BCÇDFGHJKLMNÑPRSŞTVZ'; # Crimean Tatar Latin uppercase consonants
  50. const L_CONS_LC = 'bcçdfghjklmnñprsştvz'; # Crimean Tatar Latin lowercase consonants
  51. const L_CONS = 'bcçdfghjklmnñprsştvzBCÇDFGHJKLMNÑPRSŞTVZ'; # Crimean Tatar Latin consonants
  52. const L_VOW_UC = 'AÂEIİOÖUÜ'; # Crimean Tatar Latin uppercase vowels
  53. const L_VOW = 'aâeıioöuüAÂEIİOÖUÜ'; # Crimean Tatar Latin vowels
  54. const L_F_UC = 'EİÖÜ'; # Crimean Tatar Latin uppercase front vowels
  55. const L_F = 'eiöüEİÖÜ'; # Crimean Tatar Latin front vowels
  56. /**
  57. * @param Language $langobj
  58. * @param string $maincode
  59. * @param array $variants
  60. * @param array $variantfallbacks
  61. * @param array $flags
  62. */
  63. function __construct( $langobj, $maincode,
  64. $variants = [],
  65. $variantfallbacks = [],
  66. $flags = [] ) {
  67. parent::__construct( $langobj, $maincode,
  68. $variants, $variantfallbacks, $flags );
  69. // No point delaying this since they're in code.
  70. // Waiting until loadDefaultTables() means they never get loaded
  71. // when the tables themselves are loaded from cache.
  72. $this->loadExceptions();
  73. }
  74. public $mCyrillicToLatin = [
  75. ## these are independent of location in the word, but have
  76. ## to go first so other transforms don't bleed them
  77. 'гъ' => 'ğ', 'Гъ' => 'Ğ', 'ГЪ' => 'Ğ',
  78. 'къ' => 'q', 'Къ' => 'Q', 'КЪ' => 'Q',
  79. 'нъ' => 'ñ', 'Нъ' => 'Ñ', 'НЪ' => 'Ñ',
  80. 'дж' => 'c', 'Дж' => 'C', 'ДЖ' => 'C',
  81. 'А' => 'A', 'а' => 'a', 'Б' => 'B', 'б' => 'b',
  82. 'В' => 'V', 'в' => 'v', 'Г' => 'G', 'г' => 'g',
  83. 'Д' => 'D', 'д' => 'd', 'Ж' => 'J', 'ж' => 'j',
  84. 'З' => 'Z', 'з' => 'z', 'И' => 'İ', 'и' => 'i',
  85. 'Й' => 'Y', 'й' => 'y', 'К' => 'K', 'к' => 'k',
  86. 'Л' => 'L', 'л' => 'l', 'М' => 'M', 'м' => 'm',
  87. 'Н' => 'N', 'н' => 'n', 'П' => 'P', 'п' => 'p',
  88. 'Р' => 'R', 'р' => 'r', 'С' => 'S', 'с' => 's',
  89. 'Т' => 'T', 'т' => 't', 'Ф' => 'F', 'ф' => 'f',
  90. 'Х' => 'H', 'х' => 'h', 'Ч' => 'Ç', 'ч' => 'ç',
  91. 'Ш' => 'Ş', 'ш' => 'ş', 'Ы' => 'I', 'ы' => 'ı',
  92. 'Э' => 'E', 'э' => 'e', 'Е' => 'E', 'е' => 'e',
  93. 'Я' => 'Â', 'я' => 'â', 'У' => 'U', 'у' => 'u',
  94. 'О' => 'O', 'о' => 'o',
  95. 'Ё' => 'Yo', 'ё' => 'yo', 'Ю' => 'Yu', 'ю' => 'yu',
  96. 'Ц' => 'Ts', 'ц' => 'ts', 'Щ' => 'Şç', 'щ' => 'şç',
  97. 'Ь' => '', 'ь' => '', 'Ъ' => '', 'ъ' => '',
  98. ];
  99. public $mLatinToCyrillic = [
  100. 'Â' => 'Я', 'â' => 'я', 'B' => 'Б', 'b' => 'б',
  101. 'Ç' => 'Ч', 'ç' => 'ч', 'D' => 'Д', 'd' => 'д',
  102. 'F' => 'Ф', 'f' => 'ф', 'G' => 'Г', 'g' => 'г',
  103. 'H' => 'Х', 'h' => 'х', 'I' => 'Ы', 'ı' => 'ы',
  104. 'İ' => 'И', 'i' => 'и', 'J' => 'Ж', 'j' => 'ж',
  105. 'K' => 'К', 'k' => 'к', 'L' => 'Л', 'l' => 'л',
  106. 'M' => 'М', 'm' => 'м', 'N' => 'Н', 'n' => 'н',
  107. 'O' => 'О', 'o' => 'о', 'P' => 'П', 'p' => 'п',
  108. 'R' => 'Р', 'r' => 'р', 'S' => 'С', 's' => 'с',
  109. 'Ş' => 'Ш', 'ş' => 'ш', 'T' => 'Т', 't' => 'т',
  110. 'V' => 'В', 'v' => 'в', 'Z' => 'З', 'z' => 'з',
  111. 'ya' => 'я', 'Ya' => 'Я', 'YA' => 'Я',
  112. 'ye' => 'е', 'YE' => 'Е', 'Ye' => 'Е',
  113. // hack, hack, hack
  114. 'A' => 'А', 'a' => 'а', 'E' => 'Е', 'e' => 'е',
  115. 'Ö' => 'Ё', 'ö' => 'ё', 'U' => 'У', 'u' => 'у',
  116. 'Ü' => 'Ю', 'ü' => 'ю', 'Y' => 'Й', 'y' => 'й',
  117. 'C' => 'Дж', 'c' => 'дж', 'Ğ' => 'Гъ', 'ğ' => 'гъ',
  118. 'Ñ' => 'Нъ', 'ñ' => 'нъ', 'Q' => 'Къ', 'q' => 'къ',
  119. ];
  120. public $mExceptions = [];
  121. public $mCyrl2LatnPatterns = [];
  122. public $mLatn2CyrlPatterns = [];
  123. public $mCyrlCleanUpRegexes = [];
  124. public $mExceptionsLoaded = false;
  125. function loadDefaultTables() {
  126. $this->mTables = [
  127. 'crh-latn' => new ReplacementArray( $this->mCyrillicToLatin ),
  128. 'crh-cyrl' => new ReplacementArray( $this->mLatinToCyrillic ),
  129. 'crh' => new ReplacementArray()
  130. ];
  131. }
  132. function loadExceptions() {
  133. if ( $this->mExceptionsLoaded ) {
  134. return;
  135. }
  136. $this->mExceptionsLoaded = true;
  137. $crhExceptions = new MediaWiki\Languages\Data\CrhExceptions();
  138. list( $this->mExceptions, $this->mCyrl2LatnPatterns, $this->mLatn2CyrlPatterns,
  139. $this->mCyrlCleanUpRegexes ) = $crhExceptions->loadExceptions( self::L_LC . self::C_LC,
  140. self::L_UC . self::C_UC );
  141. }
  142. /**
  143. * A function wrapper:
  144. * - if there is no selected variant, leave the link
  145. * names as they were
  146. * - do not try to find variants for usernames
  147. *
  148. * @param string &$link
  149. * @param Title &$nt
  150. * @param bool $ignoreOtherCond
  151. */
  152. function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
  153. // check for user namespace
  154. if ( is_object( $nt ) ) {
  155. $ns = $nt->getNamespace();
  156. if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
  157. return;
  158. }
  159. }
  160. $oldlink = $link;
  161. parent::findVariantLink( $link, $nt, $ignoreOtherCond );
  162. if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
  163. $link = $oldlink;
  164. }
  165. }
  166. /**
  167. * It translates text into variant, specials:
  168. * - ommiting roman numbers
  169. *
  170. * @param string $text
  171. * @param bool $toVariant
  172. *
  173. * @throws MWException
  174. * @return string
  175. */
  176. function translate( $text, $toVariant ) {
  177. $letters = '';
  178. switch ( $toVariant ) {
  179. case 'crh-cyrl':
  180. $letters = self::L_UC . self::L_LC . "\'";
  181. break;
  182. case 'crh-latn':
  183. $letters = self::C_UC . self::C_LC . "";
  184. break;
  185. default:
  186. return $text;
  187. break;
  188. }
  189. if ( !$this->mTablesLoaded ) {
  190. $this->loadTables();
  191. }
  192. if ( !isset( $this->mTables[$toVariant] ) ) {
  193. throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
  194. }
  195. // check for roman numbers like VII, XIX...
  196. // Lookahead assertion ensures $roman doesn't match the empty string
  197. $roman = '/^(?=[MDCLXVI])M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})$/u';
  198. # match any sub-string of the relevant letters and convert it
  199. $matches = preg_split( '/(\b|^)[^' . $letters . ']+(\b|$)/u',
  200. $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
  201. $mstart = 0;
  202. $ret = '';
  203. foreach ( $matches as $m ) {
  204. # copy over the non-matching bit
  205. $ret .= substr( $text, $mstart, $m[1] - $mstart );
  206. # skip certain classes of strings
  207. if ( array_key_exists( $m[0], $this->mExceptions ) ) {
  208. # if it's an exception, just copy down the right answer
  209. $ret .= $this->mExceptions[$m[0]];
  210. } elseif ( ! $m[0] || # empty strings
  211. preg_match( $roman, $m[0] ) || # roman numerals
  212. preg_match( '/[^' . $letters . ']/', $m[0] ) # mixed orthography
  213. ) {
  214. $ret .= $m[0];
  215. } else {
  216. # convert according to the rules
  217. $token = $this->regsConverter( $m[0], $toVariant );
  218. $ret .= parent::translate( $token, $toVariant );
  219. }
  220. $mstart = $m[1] + strlen( $m[0] );
  221. }
  222. # pick up stray quote marks
  223. switch ( $toVariant ) {
  224. case 'crh-cyrl':
  225. $ret = strtr( $ret, [ '“' => '«', '”' => '»', ] );
  226. $ret = $this->regsConverter( $ret, 'cyrl-cleanup' );
  227. break;
  228. case 'crh-latn':
  229. $ret = strtr( $ret, [ '«' => '"', '»' => '"', ] );
  230. break;
  231. }
  232. return $ret;
  233. }
  234. private function regsConverter( $text, $toVariant ) {
  235. if ( $text == '' ) return $text;
  236. $pat = [];
  237. $rep = [];
  238. switch ( $toVariant ) {
  239. case 'crh-latn':
  240. foreach ( $this->mCyrl2LatnPatterns as $pat => $rep ) {
  241. $text = preg_replace( $pat, $rep, $text );
  242. }
  243. return $text;
  244. case 'crh-cyrl':
  245. foreach ( $this->mLatn2CyrlPatterns as $pat => $rep ) {
  246. $text = preg_replace( $pat, $rep, $text );
  247. }
  248. return $text;
  249. case 'cyrl-cleanup':
  250. foreach ( $this->mCyrlCleanUpRegexes as $pat => $rep ) {
  251. $text = preg_replace( $pat, $rep, $text );
  252. }
  253. return $text;
  254. default:
  255. return $text;
  256. }
  257. }
  258. }
  259. /**
  260. * Crimean Tatar (Qırımtatarca)
  261. *
  262. * @ingroup Language
  263. */
  264. class LanguageCrh extends Language {
  265. function __construct() {
  266. parent::__construct();
  267. $variants = [ 'crh', 'crh-cyrl', 'crh-latn' ];
  268. $variantfallbacks = [
  269. 'crh' => 'crh-latn',
  270. 'crh-cyrl' => 'crh-latn',
  271. 'crh-latn' => 'crh-cyrl',
  272. ];
  273. $this->mConverter = new CrhConverter( $this, 'crh', $variants, $variantfallbacks );
  274. }
  275. }