MediaWiki REL1_35
SrConverter.php
Go to the documentation of this file.
1<?php
34
38 public $mToLatin = [
39 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
40 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
41 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
42 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
43 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
44 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
45
46 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
47 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
48 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
49 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
50 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
51 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
52 ];
53
57 public $mToCyrillics = [
58 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
59 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
60 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
61 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
62 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
63 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
64
65 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
66 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
67 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
68 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
69 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
70 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
71
72 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
73 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
74 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
75 ];
76
80 public function __construct( $langobj ) {
81 $variants = [ 'sr', 'sr-ec', 'sr-el' ];
82 $variantfallbacks = [
83 'sr' => 'sr-ec',
84 'sr-ec' => 'sr',
85 'sr-el' => 'sr',
86 ];
87
88 $flags = [
89 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
90 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
91 ];
92 parent::__construct( $langobj, 'sr', $variants, $variantfallbacks, $flags );
93 }
94
95 protected function loadDefaultTables() {
96 $this->mTables = [
97 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
98 'sr-el' => new ReplacementArray( $this->mToLatin ),
99 'sr' => new ReplacementArray()
100 ];
101 }
102
113 public function translate( $text, $toVariant ) {
114 $breaks = '[^\w\x80-\xff]';
115
116 // regexp for roman numbers
117 // Lookahead assertion ensures $roman doesn't match the empty string
118 $roman = '(?=[MDCLXVI])M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})';
119
120 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
121 . $roman . '$|' . $breaks . $roman . $breaks . '/';
122
123 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
124
125 $m = array_shift( $matches );
126 $this->loadTables();
127 if ( !isset( $this->mTables[$toVariant] ) ) {
128 throw new MWException( "Broken variant table: "
129 . implode( ',', array_keys( $this->mTables ) ) );
130 }
131 $ret = $this->mTables[$toVariant]->replace( $m[0] );
132 $mstart = $m[1] + strlen( $m[0] );
133 foreach ( $matches as $m ) {
134 $ret .= substr( $text, $mstart, $m[1] - $mstart );
135 $ret .= parent::translate( $m[0], $toVariant );
136 $mstart = $m[1] + strlen( $m[0] );
137 }
138
139 return $ret;
140 }
141
153 public function guessVariant( $text, $variant ) {
154 $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
155 $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
156
157 if ( $variant == 'sr-ec' ) {
158 return $numCyrillic > $numLatin;
159 } elseif ( $variant == 'sr-el' ) {
160 return $numLatin > $numCyrillic;
161 } else {
162 return false;
163 }
164 }
165
166}
A class that extends LanguageConverter with specific behaviour.
loadTables( $fromCache=true)
Load conversion tables either from the cache or the disk.
MediaWiki exception.
Wrapper around strtr() that holds replacements.
There are two levels of conversion for Serbian: the script level (Cyrillics <-> Latin),...
loadDefaultTables()
Load default conversion tables.
guessVariant( $text, $variant)
Guess if a text is written in Cyrillic or Latin.
string[] $mToLatin
translate( $text, $toVariant)
It translates text into variant, specials:
string[] $mToCyrillics
__construct( $langobj)