3 * Serbian (Српски / Srpski) specific code.
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.
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.
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
24 require_once __DIR__
. '/../LanguageConverter.php';
27 * There are two levels of conversion for Serbian: the script level
28 * (Cyrillics <-> Latin), and the variant level (ekavian
29 * <->iyekavian). The two are orthogonal. So we really only need two
30 * dictionaries: one for Cyrillics and Latin, and one for ekavian and
35 class SrConverter
extends LanguageConverter
{
36 public $mToLatin = array(
37 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
38 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
39 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
40 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
41 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
42 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
44 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
45 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
46 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
47 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
48 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
49 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
52 public $mToCyrillics = array(
53 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
54 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
55 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
56 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
57 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
58 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
60 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
61 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
62 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
63 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
64 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
65 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
67 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
68 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
69 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
72 function loadDefaultTables() {
73 $this->mTables
= array(
74 'sr-ec' => new ReplacementArray( $this->mToCyrillics
),
75 'sr-el' => new ReplacementArray( $this->mToLatin
),
76 'sr' => new ReplacementArray()
81 * rules should be defined as -{ekavian | iyekavian-} -or-
82 * -{code:text | code:text | ...}-
84 * update: delete all rule parsing because it's not used
85 * currently, and just produces a couple of bugs
91 function parseManualRule( $rule, $flags = array() ) {
92 if ( in_array( 'T', $flags ) ) {
93 return parent
::parseManualRule( $rule, $flags );
97 // otherwise ignore all formatting
98 foreach ( $this->mVariants
as $v ) {
106 * A function wrapper:
107 * - if there is no selected variant, leave the link
109 * - do not try to find variants for usernames
111 * @param string $link
113 * @param bool $ignoreOtherCond
115 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
116 // check for user namespace
117 if ( is_object( $nt ) ) {
118 $ns = $nt->getNamespace();
119 if ( $ns == NS_USER ||
$ns == NS_USER_TALK
) {
125 parent
::findVariantLink( $link, $nt, $ignoreOtherCond );
126 if ( $this->getPreferredVariant() == $this->mMainLanguageCode
) {
132 * An ugly function wrapper for parsing Image titles
133 * (to prevent image name conversion)
135 * @param string $text
136 * @param bool $toVariant
140 function autoConvert( $text, $toVariant = false ) {
142 if ( is_object( $wgTitle ) && $wgTitle->getNamespace() == NS_FILE
) {
143 $imagename = $wgTitle->getNsText();
144 if ( preg_match( "/^$imagename:/", $text ) ) {
148 return parent
::autoConvert( $text, $toVariant );
152 * It translates text into variant, specials:
153 * - ommiting roman numbers
155 * @param string $text
156 * @param string $toVariant
158 * @throws MWException
161 function translate( $text, $toVariant ) {
162 $breaks = '[^\w\x80-\xff]';
164 // regexp for roman numbers
165 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
167 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
168 . $roman . '$|' . $breaks . $roman . $breaks . '/';
170 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE
);
172 $m = array_shift( $matches );
174 if ( !isset( $this->mTables
[$toVariant] ) ) {
175 throw new MWException( "Broken variant table: "
176 . implode( ',', array_keys( $this->mTables
) ) );
178 $ret = $this->mTables
[$toVariant]->replace( $m[0] );
179 $mstart = $m[1] +
strlen( $m[0] );
180 foreach ( $matches as $m ) {
181 $ret .= substr( $text, $mstart, $m[1] -$mstart );
182 $ret .= parent
::translate( $m[0], $toVariant );
183 $mstart = $m[1] +
strlen( $m[0] );
190 * Guess if a text is written in Cyrillic or Latin.
191 * Overrides LanguageConverter::guessVariant()
193 * @param string $text The text to be checked
194 * @param string $variant Language code of the variant to be checked for
195 * @return bool True if $text appears to be written in $variant
197 * @author Nikola Smolenski <smolensk@eunet.rs>
200 public function guessVariant( $text, $variant ) {
201 $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
202 $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
204 if ( $variant == 'sr-ec' ) {
205 return $numCyrillic > $numLatin;
206 } elseif ( $variant == 'sr-el' ) {
207 return $numLatin > $numCyrillic;
217 * Serbian (Српски / Srpski)
221 class LanguageSr
extends Language
{
222 function __construct() {
225 parent
::__construct();
227 $variants = array( 'sr', 'sr-ec', 'sr-el' );
228 $variantfallbacks = array(
235 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
236 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
238 $this->mConverter
= new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
239 $wgHooks['PageContentSaveComplete'][] = $this->mConverter
;