Merge "(bug 1) doc for SiteConfiguration"
[lhc/web/wiklou.git] / languages / classes / LanguageSr.php
1 <?php
2 /**
3 * Serbian (Српски / Srpski) specific code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Language
22 */
23
24 require_once( dirname( __FILE__ ) . '/../LanguageConverter.php' );
25 require_once( dirname( __FILE__ ) . '/LanguageSr_ec.php' );
26 require_once( dirname( __FILE__ ) . '/LanguageSr_el.php' );
27
28 /**
29 * There are two levels of conversion for Serbian: the script level
30 * (Cyrillics <-> Latin), and the variant level (ekavian
31 * <->iyekavian). The two are orthogonal. So we really only need two
32 * dictionaries: one for Cyrillics and Latin, and one for ekavian and
33 * iyekavian.
34 *
35 * @ingroup Language
36 */
37 class SrConverter extends LanguageConverter {
38 var $mToLatin = array(
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
54 var $mToCyrillics = array(
55 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
56 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
57 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
58 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
59 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
60 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
61
62 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
63 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
64 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
65 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
66 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
67 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
68
69 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
70 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
71 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
72 );
73
74 function loadDefaultTables() {
75 $this->mTables = array(
76 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
77 'sr-el' => new ReplacementArray( $this->mToLatin ),
78 'sr' => new ReplacementArray()
79 );
80 }
81
82 /**
83 * rules should be defined as -{ekavian | iyekavian-} -or-
84 * -{code:text | code:text | ...}-
85 *
86 * update: delete all rule parsing because it's not used
87 * currently, and just produces a couple of bugs
88 *
89 * @param $rule string
90 * @param $flags array
91 * @return array
92 */
93 function parseManualRule( $rule, $flags = array() ) {
94 if ( in_array( 'T', $flags ) ) {
95 return parent::parseManualRule( $rule, $flags );
96 }
97
98 $carray = array();
99 // otherwise ignore all formatting
100 foreach ( $this->mVariants as $v ) {
101 $carray[$v] = $rule;
102 }
103
104 return $carray;
105 }
106
107 /**
108 * A function wrapper:
109 * - if there is no selected variant, leave the link
110 * names as they were
111 * - do not try to find variants for usernames
112 *
113 * @param $link string
114 * @param $nt Title
115 * @param $ignoreOtherCond bool
116 */
117 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
118 // check for user namespace
119 if ( is_object( $nt ) ) {
120 $ns = $nt->getNamespace();
121 if ( $ns == NS_USER || $ns == NS_USER_TALK )
122 return;
123 }
124
125 $oldlink = $link;
126 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
127 if ( $this->getPreferredVariant() == $this->mMainLanguageCode )
128 $link = $oldlink;
129 }
130
131 /**
132 * We want our external link captions to be converted in variants,
133 * so we return the original text instead -{$text}-, except for URLs
134 *
135 * @param $text string
136 * @param $noParse bool
137 *
138 * @return string
139 */
140 function markNoConversion( $text, $noParse = false ) {
141 if ( $noParse || preg_match( "/^https?:\/\/|ftp:\/\/|irc:\/\//", $text ) )
142 return parent::markNoConversion( $text );
143 return $text;
144 }
145
146 /**
147 * An ugly function wrapper for parsing Image titles
148 * (to prevent image name conversion)
149 *
150 * @param $text string
151 * @param $toVariant bool
152 *
153 * @return string
154 */
155 function autoConvert( $text, $toVariant = false ) {
156 global $wgTitle;
157 if ( is_object( $wgTitle ) && $wgTitle->getNameSpace() == NS_FILE ) {
158 $imagename = $wgTitle->getNsText();
159 if ( preg_match( "/^$imagename:/", $text ) ) return $text;
160 }
161 return parent::autoConvert( $text, $toVariant );
162 }
163
164 /**
165 * It translates text into variant, specials:
166 * - ommiting roman numbers
167 *
168 * @param $text string
169 * @param $toVariant string
170 *
171 * @throws MWException
172 * @return string
173 */
174 function translate( $text, $toVariant ) {
175 $breaks = '[^\w\x80-\xff]';
176
177 // regexp for roman numbers
178 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
179
180 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks . $roman . '$|' . $breaks . $roman . $breaks . '/';
181
182 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
183
184 $m = array_shift( $matches );
185 if ( !isset( $this->mTables[$toVariant] ) ) {
186 throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
187 }
188 $ret = $this->mTables[$toVariant]->replace( $m[0] );
189 $mstart = $m[1] + strlen( $m[0] );
190 foreach ( $matches as $m ) {
191 $ret .= substr( $text, $mstart, $m[1] -$mstart );
192 $ret .= parent::translate( $m[0], $toVariant );
193 $mstart = $m[1] + strlen( $m[0] );
194 }
195
196 return $ret;
197 }
198
199 /**
200 * Guess if a text is written in Cyrillic or Latin.
201 * Overrides LanguageConverter::guessVariant()
202 *
203 * @param string $text The text to be checked
204 * @param string $variant Language code of the variant to be checked for
205 * @return bool true if $text appears to be written in $variant
206 *
207 * @author Nikola Smolenski <smolensk@eunet.rs>
208 * @since 1.19
209 */
210 public function guessVariant( $text, $variant ) {
211 $numCyrillic = preg_match_all("/[шђчћжШЂЧЋЖ]/u", $text, $dummy);
212 $numLatin = preg_match_all("/[šđč枊ĐČĆŽ]/u", $text, $dummy);
213
214 if( $variant == 'sr-ec' ) {
215 return (boolean) ($numCyrillic > $numLatin);
216 } elseif( $variant == 'sr-el' ) {
217 return (boolean) ($numLatin > $numCyrillic);
218 } else {
219 return false;
220 }
221
222 }
223
224 }
225
226 /**
227 * Serbian (Српски / Srpski)
228 *
229 * @ingroup Language
230 */
231 class LanguageSr extends LanguageSr_ec {
232 function __construct() {
233 global $wgHooks;
234
235 parent::__construct();
236
237 $variants = array( 'sr', 'sr-ec', 'sr-el' );
238 $variantfallbacks = array(
239 'sr' => 'sr-ec',
240 'sr-ec' => 'sr',
241 'sr-el' => 'sr',
242 );
243
244 $flags = array(
245 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
246 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
247 );
248 $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
249 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
250 }
251
252 /**
253 * @param $count int
254 * @param $forms array
255 *
256 * @return string
257 */
258 function convertPlural( $count, $forms ) {
259 if ( !count( $forms ) ) {
260 return '';
261 }
262
263 // If the actual number is not mentioned in the expression, then just two forms are enough:
264 // singular for $count == 1
265 // plural for $count != 1
266 // For example, "This user belongs to {{PLURAL:$1|one group|several groups}}."
267 if ( count( $forms ) === 2 ) {
268 return $count == 1 ? $forms[0] : $forms[1];
269 }
270
271 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
272 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
273 $forms = $this->preConvertPlural( $forms, 3 );
274
275 if ( $count > 10 && floor( ( $count % 100 ) / 10 ) == 1 ) {
276 return $forms[2];
277 } else {
278 switch ( $count % 10 ) {
279 case 1: return $forms[0];
280 case 2:
281 case 3:
282 case 4: return $forms[1];
283 default: return $forms[2];
284 }
285 }
286 }
287 }