3 require_once( dirname( __FILE__
) . '/../LanguageConverter.php' );
4 require_once( dirname( __FILE__
) . '/LanguageSr_ec.php' );
5 require_once( dirname( __FILE__
) . '/LanguageSr_el.php' );
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
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ž', 'ш' => 'š',
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ž', 'Ш' => 'Š',
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' => 'з', 'ž' => 'ж',
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' => 'З', 'Ž' => 'Ж',
48 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
49 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
50 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
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()
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
66 function parseManualRule( $rule, $flags = array() ) {
67 if ( in_array( 'T', $flags ) ) {
68 return parent
::parseManualRule( $rule, $flags );
72 // otherwise ignore all formatting
73 foreach ( $this->mVariants
as $v ) {
82 * - if there is no selected variant, leave the link
84 * - do not try to find variants for usernames
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
)
95 parent
::findVariantLink( $link, $nt, $ignoreOtherCond );
96 if ( $this->getPreferredVariant() == $this->mMainLanguageCode
)
101 * We want our external link captions to be converted in variants,
102 * so we return the original text instead -{$text}-, except for URLs
104 function markNoConversion( $text, $noParse = false ) {
105 if ( $noParse ||
preg_match( "/^https?:\/\/|ftp:\/\/|irc:\/\//", $text ) )
106 return parent
::markNoConversion( $text );
111 * An ugly function wrapper for parsing Image titles
112 * (to prevent image name conversion)
114 function autoConvert( $text, $toVariant = false ) {
116 if ( is_object( $wgTitle ) && $wgTitle->getNameSpace() == NS_FILE
) {
117 $imagename = $wgTitle->getNsText();
118 if ( preg_match( "/^$imagename:/", $text ) ) return $text;
120 return parent
::autoConvert( $text, $toVariant );
124 * It translates text into variant, specials:
125 * - ommiting roman numbers
127 function translate( $text, $toVariant ) {
128 $breaks = '[^\w\x80-\xff]';
130 // regexp for roman numbers
131 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
133 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks . $roman . '$|' . $breaks . $roman . $breaks . '/';
135 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE
);
137 $m = array_shift( $matches );
138 if ( !isset( $this->mTables
[$toVariant] ) ) {
139 throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables
) ) );
141 $ret = $this->mTables
[$toVariant]->replace( $m[0] );
142 $mstart = $m[1] +
strlen( $m[0] );
143 foreach ( $matches as $m ) {
144 $ret .= substr( $text, $mstart, $m[1] -$mstart );
145 $ret .= parent
::translate( $m[0], $toVariant );
146 $mstart = $m[1] +
strlen( $m[0] );
153 * Guess if a text is written in Cyrillic or Latin.
155 * @author Nikola Smolenski <smolensk@eunet.rs>
158 public function guessVariant( $text, $variant ) {
159 $numCyrillic = preg_match_all("/[шђчћжШЂЧЋЖ]/u", $text, $dummy);
160 $numLatin = preg_match_all("/[šđč枊ĐČĆŽ]/u", $text, $dummy);
162 if( $variant == 'sr-ec' ) {
163 return $numCyrillic > $numLatin;
164 } else if( $variant == 'sr-el' ) {
165 return $numLatin > $numCyrillic;
175 * Serbian (Српски / Srpski)
179 class LanguageSr
extends LanguageSr_ec
{
180 function __construct() {
183 parent
::__construct();
185 $variants = array( 'sr', 'sr-ec', 'sr-el' );
186 $variantfallbacks = array(
193 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
194 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
196 $this->mConverter
= new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
197 $wgHooks['ArticleSaveComplete'][] = $this->mConverter
;
200 function convertPlural( $count, $forms ) {
201 if ( !count( $forms ) ) {
205 // if no number with word, then use $form[0] for singular and $form[1] for plural or zero
206 if ( count( $forms ) === 2 ) {
207 return $count == 1 ?
$forms[0] : $forms[1];
210 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
211 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
212 $forms = $this->preConvertPlural( $forms, 3 );
214 if ( $count > 10 && floor( ( $count %
100 ) / 10 ) == 1 ) {
217 switch ( $count %
10 ) {
218 case 1: return $forms[0];
221 case 4: return $forms[1];
222 default: return $forms[2];