MediaWiki  1.30.0
LanguageSr.php
Go to the documentation of this file.
1 <?php
33 class SrConverter extends LanguageConverter {
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  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  $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|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 }
167 
173 class LanguageSr extends Language {
174  function __construct() {
175  parent::__construct();
176 
177  $variants = [ 'sr', 'sr-ec', 'sr-el' ];
178  $variantfallbacks = [
179  'sr' => 'sr-ec',
180  'sr-ec' => 'sr',
181  'sr-el' => 'sr',
182  ];
183 
184  $flags = [
185  'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
186  'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
187  ];
188  $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
189  }
190 }
SrConverter\guessVariant
guessVariant( $text, $variant)
Guess if a text is written in Cyrillic or Latin.
Definition: LanguageSr.php:153
SrConverter\findVariantLink
findVariantLink(&$link, &$nt, $ignoreOtherCond=false)
A function wrapper:
Definition: LanguageSr.php:88
LanguageSr\__construct
__construct()
Definition: LanguageSr.php:174
SrConverter
There are two levels of conversion for Serbian: the script level (Cyrillics <-> Latin),...
Definition: LanguageSr.php:33
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MWException
MediaWiki exception.
Definition: MWException.php:26
SrConverter\loadDefaultTables
loadDefaultTables()
Definition: LanguageSr.php:70
$matches
$matches
Definition: NoLocalSettings.php:24
LanguageSr
Serbian (Српски / Srpski)
Definition: LanguageSr.php:173
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:68
ReplacementArray
Wrapper around strtr() that holds replacements.
Definition: ReplacementArray.php:24
SrConverter\$mToLatin
$mToLatin
Definition: LanguageSr.php:34
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1965
Makefile.translate
def translate(text, conv_table)
Definition: Makefile.py:235
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
NS_USER
const NS_USER
Definition: Defines.php:67
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:2981
SrConverter\translate
translate( $text, $toVariant)
It translates text into variant, specials:
Definition: LanguageSr.php:114
SrConverter\$mToCyrillics
$mToCyrillics
Definition: LanguageSr.php:50
Language
Internationalisation code.
Definition: Language.php:35
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2801