Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / languages / classes / LanguageSr.php
blob6491f551401d4d0d855fa34b2df8f99a5d2b5eb4
1 <?php
2 /**
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
20 * @file
21 * @ingroup Language
24 /**
25 * There are two levels of conversion for Serbian: the script level
26 * (Cyrillics <-> Latin), and the variant level (ekavian
27 * <->iyekavian). The two are orthogonal. So we really only need two
28 * dictionaries: one for Cyrillics and Latin, and one for ekavian and
29 * iyekavian.
31 * @ingroup Language
33 class SrConverter extends LanguageConverter {
34 public $mToLatin = [
35 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
36 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
37 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
38 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
39 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
40 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
42 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
43 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
44 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
45 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
46 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
47 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
50 public $mToCyrillics = [
51 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
52 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
53 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
54 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
55 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
56 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
58 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
59 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
60 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
61 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
62 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
63 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
65 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
66 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
67 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
70 function loadDefaultTables() {
71 $this->mTables = [
72 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
73 'sr-el' => new ReplacementArray( $this->mToLatin ),
74 'sr' => new ReplacementArray()
78 /**
79 * A function wrapper:
80 * - if there is no selected variant, leave the link
81 * names as they were
82 * - do not try to find variants for usernames
84 * @param string &$link
85 * @param Title &$nt
86 * @param bool $ignoreOtherCond
88 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
89 // check for user namespace
90 if ( is_object( $nt ) ) {
91 $ns = $nt->getNamespace();
92 if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
93 return;
97 $oldlink = $link;
98 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
99 if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
100 $link = $oldlink;
105 * It translates text into variant, specials:
106 * - ommiting roman numbers
108 * @param string $text
109 * @param string $toVariant
111 * @throws MWException
112 * @return string
114 function translate( $text, $toVariant ) {
115 $breaks = '[^\w\x80-\xff]';
117 // regexp for roman numbers
118 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
120 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
121 . $roman . '$|' . $breaks . $roman . $breaks . '/';
123 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
125 $m = array_shift( $matches );
126 $this->loadTables();
127 if ( !isset( $this->mTables[$toVariant] ) ) {
128 throw new MWException( "Broken variant table: "
129 . implode( ',', array_keys( $this->mTables ) ) );
131 $ret = $this->mTables[$toVariant]->replace( $m[0] );
132 $mstart = $m[1] + strlen( $m[0] );
133 foreach ( $matches as $m ) {
134 $ret .= substr( $text, $mstart, $m[1] -$mstart );
135 $ret .= parent::translate( $m[0], $toVariant );
136 $mstart = $m[1] + strlen( $m[0] );
139 return $ret;
143 * Guess if a text is written in Cyrillic or Latin.
144 * Overrides LanguageConverter::guessVariant()
146 * @param string $text The text to be checked
147 * @param string $variant Language code of the variant to be checked for
148 * @return bool True if $text appears to be written in $variant
150 * @author Nikola Smolenski <smolensk@eunet.rs>
151 * @since 1.19
153 public function guessVariant( $text, $variant ) {
154 $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
155 $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
157 if ( $variant == 'sr-ec' ) {
158 return $numCyrillic > $numLatin;
159 } elseif ( $variant == 'sr-el' ) {
160 return $numLatin > $numCyrillic;
161 } else {
162 return false;
169 * Serbian (Српски / Srpski)
171 * @ingroup Language
173 class LanguageSr extends Language {
174 function __construct() {
175 parent::__construct();
177 $variants = [ 'sr', 'sr-ec', 'sr-el' ];
178 $variantfallbacks = [
179 'sr' => 'sr-ec',
180 'sr-ec' => 'sr',
181 'sr-el' => 'sr',
184 $flags = [
185 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
186 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
188 $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );