More documentation!
[lhc/web/wiklou.git] / languages / classes / LanguageSr.php
1 <?php
2
3 require_once( dirname( __FILE__ ) . '/../LanguageConverter.php' );
4 require_once( dirname( __FILE__ ) . '/LanguageSr_ec.php' );
5 require_once( dirname( __FILE__ ) . '/LanguageSr_el.php' );
6
7 /**
8 * There are two levels of conversion for Serbian: the script level
9 * (Cyrillics <-> Latin), and the variant level (ekavian
10 * <->iyekavian). The two are orthogonal. So we really only need two
11 * dictionaries: one for Cyrillics and Latin, and one for ekavian and
12 * iyekavian.
13 *
14 * @ingroup Language
15 */
16 class SrConverter extends LanguageConverter {
17 var $mToLatin = array(
18 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
19 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
20 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
21 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
22 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
23 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
24
25 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
26 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
27 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
28 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
29 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
30 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
31 );
32
33 var $mToCyrillics = array(
34 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
35 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
36 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
37 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
38 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
39 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
40
41 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
42 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
43 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
44 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
45 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
46 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
47
48 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
49 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
50 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
51 );
52
53 function loadDefaultTables() {
54 $this->mTables = array(
55 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
56 'sr-el' => new ReplacementArray( $this->mToLatin ),
57 'sr' => new ReplacementArray()
58 );
59 }
60
61 /* rules should be defined as -{ekavian | iyekavian-} -or-
62 -{code:text | code:text | ...}-
63 update: delete all rule parsing because it's not used
64 currently, and just produces a couple of bugs
65 */
66 function parseManualRule( $rule, $flags = array() ) {
67 if ( in_array( 'T', $flags ) ) {
68 return parent::parseManualRule( $rule, $flags );
69 }
70
71 $carray = array();
72 // otherwise ignore all formatting
73 foreach ( $this->mVariants as $v ) {
74 $carray[$v] = $rule;
75 }
76
77 return $carray;
78 }
79
80 /**
81 * A function wrapper:
82 * - if there is no selected variant, leave the link
83 * names as they were
84 * - do not try to find variants for usernames
85 */
86 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
87 // check for user namespace
88 if ( is_object( $nt ) ) {
89 $ns = $nt->getNamespace();
90 if ( $ns == NS_USER || $ns == NS_USER_TALK )
91 return;
92 }
93
94 $oldlink = $link;
95 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
96 if ( $this->getPreferredVariant() == $this->mMainLanguageCode )
97 $link = $oldlink;
98 }
99
100 /**
101 * We want our external link captions to be converted in variants,
102 * so we return the original text instead -{$text}-, except for URLs
103 *
104 * @return string
105 */
106 function markNoConversion( $text, $noParse = false ) {
107 if ( $noParse || preg_match( "/^https?:\/\/|ftp:\/\/|irc:\/\//", $text ) )
108 return parent::markNoConversion( $text );
109 return $text;
110 }
111
112 /**
113 * An ugly function wrapper for parsing Image titles
114 * (to prevent image name conversion)
115 *
116 * @return string
117 */
118 function autoConvert( $text, $toVariant = false ) {
119 global $wgTitle;
120 if ( is_object( $wgTitle ) && $wgTitle->getNameSpace() == NS_FILE ) {
121 $imagename = $wgTitle->getNsText();
122 if ( preg_match( "/^$imagename:/", $text ) ) return $text;
123 }
124 return parent::autoConvert( $text, $toVariant );
125 }
126
127 /**
128 * It translates text into variant, specials:
129 * - ommiting roman numbers
130 *
131 * @return string
132 */
133 function translate( $text, $toVariant ) {
134 $breaks = '[^\w\x80-\xff]';
135
136 // regexp for roman numbers
137 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
138
139 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks . $roman . '$|' . $breaks . $roman . $breaks . '/';
140
141 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
142
143 $m = array_shift( $matches );
144 if ( !isset( $this->mTables[$toVariant] ) ) {
145 throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
146 }
147 $ret = $this->mTables[$toVariant]->replace( $m[0] );
148 $mstart = $m[1] + strlen( $m[0] );
149 foreach ( $matches as $m ) {
150 $ret .= substr( $text, $mstart, $m[1] -$mstart );
151 $ret .= parent::translate( $m[0], $toVariant );
152 $mstart = $m[1] + strlen( $m[0] );
153 }
154
155 return $ret;
156 }
157
158 /**
159 * Guess if a text is written in Cyrillic or Latin.
160 *
161 * @author Nikola Smolenski <smolensk@eunet.rs>
162 * @since 1.18
163 *
164 * @return bool
165 */
166 public function guessVariant( $text, $variant ) {
167 $numCyrillic = preg_match_all("/[шђчћжШЂЧЋЖ]/u", $text, $dummy);
168 $numLatin = preg_match_all("/[šđč枊ĐČĆŽ]/u", $text, $dummy);
169
170 if( $variant == 'sr-ec' ) {
171 return $numCyrillic > $numLatin;
172 } else if( $variant == 'sr-el' ) {
173 return $numLatin > $numCyrillic;
174 } else {
175 return false;
176 }
177 }
178 }
179
180 /**
181 * Serbian (Српски / Srpski)
182 *
183 * @ingroup Language
184 */
185 class LanguageSr extends LanguageSr_ec {
186 function __construct() {
187 global $wgHooks;
188
189 parent::__construct();
190
191 $variants = array( 'sr', 'sr-ec', 'sr-el' );
192 $variantfallbacks = array(
193 'sr' => 'sr-ec',
194 'sr-ec' => 'sr',
195 'sr-el' => 'sr',
196 );
197
198 $flags = array(
199 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
200 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
201 );
202 $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
203 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
204 }
205
206 /**
207 * @param $count int
208 * @param $forms array
209 *
210 * @return string
211 */
212 function convertPlural( $count, $forms ) {
213 if ( !count( $forms ) ) {
214 return '';
215 }
216
217 // if no number with word, then use $form[0] for singular and $form[1] for plural or zero
218 if ( count( $forms ) === 2 ) {
219 return $count == 1 ? $forms[0] : $forms[1];
220 }
221
222 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
223 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
224 $forms = $this->preConvertPlural( $forms, 3 );
225
226 if ( $count > 10 && floor( ( $count % 100 ) / 10 ) == 1 ) {
227 return $forms[2];
228 } else {
229 switch ( $count % 10 ) {
230 case 1: return $forms[0];
231 case 2:
232 case 3:
233 case 4: return $forms[1];
234 default: return $forms[2];
235 }
236 }
237 }
238
239 }