MediaWiki  master
LanguageSr.php
Go to the documentation of this file.
1 <?php
34  public $mToLatin = [
35  'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
36  'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
37  'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
38  'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
39  'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
40  'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
41 
42  'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
43  'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
44  'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
45  'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
46  'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
47  'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
48  ];
49 
50  public $mToCyrillics = [
51  'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
52  'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
53  'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
54  'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
55  'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
56  't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
57 
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  'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
66  'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
67  'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
68  ];
69 
70  function loadDefaultTables() {
71  $this->mTables = [
72  'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
73  'sr-el' => new ReplacementArray( $this->mToLatin ),
74  'sr' => new ReplacementArray()
75  ];
76  }
77 
88  public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
89  // check for user namespace
90  if ( is_object( $nt ) ) {
91  $ns = $nt->getNamespace();
92  if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
93  return;
94  }
95  }
96 
97  $oldlink = $link;
98  parent::findVariantLink( $link, $nt, $ignoreOtherCond );
99  if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
100  $link = $oldlink;
101  }
102  }
103 
114  function translate( $text, $toVariant ) {
115  $breaks = '[^\w\x80-\xff]';
116 
117  // regexp for roman numbers
118  // Lookahead assertion ensures $roman doesn't match the empty string
119  $roman = '(?=[MDCLXVI])M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})';
120 
121  $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
122  . $roman . '$|' . $breaks . $roman . $breaks . '/';
123 
124  $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
125 
126  $m = array_shift( $matches );
127  $this->loadTables();
128  if ( !isset( $this->mTables[$toVariant] ) ) {
129  throw new MWException( "Broken variant table: "
130  . implode( ',', array_keys( $this->mTables ) ) );
131  }
132  $ret = $this->mTables[$toVariant]->replace( $m[0] );
133  $mstart = $m[1] + strlen( $m[0] );
134  foreach ( $matches as $m ) {
135  $ret .= substr( $text, $mstart, $m[1] - $mstart );
136  $ret .= parent::translate( $m[0], $toVariant );
137  $mstart = $m[1] + strlen( $m[0] );
138  }
139 
140  return $ret;
141  }
142 
154  public function guessVariant( $text, $variant ) {
155  $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
156  $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
157 
158  if ( $variant == 'sr-ec' ) {
159  return $numCyrillic > $numLatin;
160  } elseif ( $variant == 'sr-el' ) {
161  return $numLatin > $numCyrillic;
162  } else {
163  return false;
164  }
165  }
166 
167 }
168 
174 class LanguageSr extends Language {
175  protected function newConverter() : SrConverter {
176  $variants = [ 'sr', 'sr-ec', 'sr-el' ];
177  $variantfallbacks = [
178  'sr' => 'sr-ec',
179  'sr-ec' => 'sr',
180  'sr-el' => 'sr',
181  ];
182 
183  $flags = [
184  'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
185  'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
186  ];
187  return new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
188  }
189 }
Wrapper around strtr() that holds replacements.
There are two levels of conversion for Serbian: the script level (Cyrillics <-> Latin), and the variant level (ekavian <->iyekavian).
Definition: LanguageSr.php:33
Serbian (Српски / Srpski)
Definition: LanguageSr.php:174
loadDefaultTables()
Definition: LanguageSr.php:70
translate( $text, $toVariant)
It translates text into variant, specials:
Definition: LanguageSr.php:114
guessVariant( $text, $variant)
Guess if a text is written in Cyrillic or Latin.
Definition: LanguageSr.php:154
const NS_USER_TALK
Definition: Defines.php:63
getPreferredVariant()
Get preferred language variant.
loadTables( $fromCache=true)
Load conversion tables either from the cache or the disk.
findVariantLink(&$link, &$nt, $ignoreOtherCond=false)
A function wrapper:
Definition: LanguageSr.php:88
$matches