More language variant stuff:
[lhc/web/wiklou.git] / languages / classes / LanguageSr.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Language
5 */
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 require_once( dirname(__FILE__).'/../LanguageConverter.php' );
15 require_once( dirname(__FILE__).'/LanguageSr_ec.php' );
16 require_once( dirname(__FILE__).'/LanguageSr_el.php' );
17
18 class SrConverter extends LanguageConverter {
19 var $mToLatin = array(
20 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
21 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
22 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
23 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
24 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
25 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
26
27 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
28 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
29 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
30 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
31 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
32 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
33 );
34
35 var $mToCyrillics = array(
36 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
37 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
38 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
39 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
40 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
41 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
42
43 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
44 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
45 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
46 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
47 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
48 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
49
50 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž'=> 'Дж', 'D!Ž'=> 'ДЖ',
51 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j'=> 'Лј', 'L!J'=> 'ЛЈ',
52 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j'=> 'Нј', 'N!J'=> 'НЈ'
53 );
54
55 var $mParsingContent=false;
56
57 function loadDefaultTables() {
58 $this->mTables = array(
59 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
60 'sr-jc' => new ReplacementArray( $this->mToCyrillics),
61 'sr-el' => new ReplacementArray( $this->mToLatin),
62 'sr-jl' => new ReplacementArray( $this->mToLatin),
63 'sr' => new ReplacementArray()
64 );
65 }
66
67 /* rules should be defined as -{ekavian | iyekavian-} -or-
68 -{code:text | code:text | ...}-
69 update: delete all rule parsing because it's not used
70 currently, and just produces a couple of bugs
71 */
72 function parseManualRule($rule, $flags=array()) {
73 // ignore all formatting
74 foreach($this->mVariants as $v) {
75 $carray[$v] = $rule;
76 }
77
78 return $carray;
79 }
80
81 // Set a flag when parsing content, this is used to prevent
82 // conversion of content within talk pages
83 function parserConvert( $text, &$parser ){
84 $this->mParsingContent = true;
85 $output = parent::parserConvert($text, $parser );
86 $this->mParsingContent = false;
87 return $output;
88
89 }
90
91 /*
92 * Override function from LanguageConvertor
93 * Additional checks:
94 * - There should be no conversion for Talk pages
95 */
96 function getPreferredVariant( $fromUser=true ){
97 global $wgTitle;
98 if(is_object($wgTitle) && $wgTitle->isTalkPage() && $this->mParsingContent){
99 return $this->mMainLanguageCode;
100 }
101 return parent::getPreferredVariant($fromUser);
102 }
103
104
105 /*
106 * A function wrapper:
107 * - if there is no selected variant, leave the link
108 * names as they were
109 * - do not try to find variants for usernames
110 */
111 function findVariantLink( &$link, &$nt ) {
112 // check for user namespace
113 if(is_object($nt)){
114 $ns = $nt->getNamespace();
115 if($ns==NS_USER || $ns==NS_USER_TALK)
116 return;
117 }
118
119 $oldlink=$link;
120 parent::findVariantLink($link,$nt);
121 if($this->getPreferredVariant()==$this->mMainLanguageCode)
122 $link=$oldlink;
123 }
124
125 /*
126 * We want our external link captions to be converted in variants,
127 * so we return the original text instead -{$text}-, except for URLs
128 */
129 function markNoConversion($text, $noParse=false) {
130 if($noParse || preg_match("/^https?:\/\/|ftp:\/\/|irc:\/\//",$text))
131 return parent::markNoConversion($text);
132 return $text;
133 }
134
135 /*
136 * An ugly function wrapper for parsing Image titles
137 * (to prevent image name conversion)
138 */
139 function autoConvert($text, $toVariant=false) {
140 global $wgTitle;
141 if(is_object($wgTitle) && $wgTitle->getNameSpace()==NS_IMAGE){
142 $imagename = $wgTitle->getNsText();
143 if(preg_match("/^$imagename:/",$text)) return $text;
144 }
145 return parent::autoConvert($text,$toVariant);
146 }
147
148 /**
149 * It translates text into variant, specials:
150 * - ommiting roman numbers
151 */
152 function translate($text, $toVariant){
153 $breaks = '[^\w\x80-\xff]';
154
155 // regexp for roman numbers
156 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
157
158 $reg = '/^'.$roman.'$|^'.$roman.$breaks.'|'.$breaks.$roman.'$|'.$breaks.$roman.$breaks.'/';
159
160 $matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE);
161
162 $m = array_shift($matches);
163 $ret = $this->mTables[$toVariant]->replace( $m[0] );
164 $mstart = $m[1]+strlen($m[0]);
165 foreach($matches as $m) {
166 $ret .= substr($text, $mstart, $m[1]-$mstart);
167 $ret .= parent::translate($m[0], $toVariant);
168 $mstart = $m[1] + strlen($m[0]);
169 }
170
171 return $ret;
172 }
173
174 function armourMath($text){
175 return parent::markNoConversion($text);
176 }
177
178 }
179
180 class LanguageSr extends LanguageSr_ec {
181 function __construct() {
182 global $wgHooks;
183
184 parent::__construct();
185
186 // these variants are currently UNUSED:
187 // 'sr-jc', 'sr-jl'
188 $variants = array('sr', 'sr-ec', 'sr-el');
189 $variantfallbacks = array(
190 'sr' => 'sr-ec',
191 'sr-ec' => 'sr-ec',
192 'sr-el' => 'sr-el',
193 );
194
195
196 $marker = array();//don't mess with these, leave them as they are
197 $flags = array(
198 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
199 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
200 );
201 $this->mConverter = new SrConverter($this, 'sr', $variants, $variantfallbacks, $marker, $flags);
202 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
203 }
204 }
205 ?>