Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.67% covered (danger)
46.67%
14 / 30
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SrConverter
46.67% covered (danger)
46.67%
14 / 30
57.14% covered (warning)
57.14%
4 / 7
21.29
0.00% covered (danger)
0.00%
0 / 1
 getMainCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLanguageVariants
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getVariantsFallbacks
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getAdditionalFlags
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 loadDefaultTables
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 translate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 guessVariant
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21use MediaWiki\Language\ReplacementArray;
22
23/**
24 * Serbian (Српски / Srpski) specific code.
25 *
26 * There are two levels of conversion for Serbian: the script level
27 * (Cyrillics <-> Latin), and the variant level (ekavian
28 * <->iyekavian). The two are orthogonal. So we really only need two
29 * dictionaries: one for Cyrillics and Latin, and one for ekavian and
30 * iyekavian.
31 *
32 * @ingroup Languages
33 */
34class SrConverter extends LanguageConverterSpecific {
35
36    private const TO_LATIN = [
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ž', 'ш' => 'š',
43
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ž', 'Ш' => 'Š',
50    ];
51
52    private const TO_CYRILLICS = [
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' => 'з', 'ž' => 'ж',
59
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' => 'З', 'Ž' => 'Ж',
66
67        'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
68        'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
69        'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
70    ];
71
72    public function getMainCode(): string {
73        return 'sr';
74    }
75
76    public function getLanguageVariants(): array {
77        return [ 'sr', 'sr-ec', 'sr-el' ];
78    }
79
80    public function getVariantsFallbacks(): array {
81        return [
82            'sr' => 'sr-ec',
83            'sr-ec' => 'sr',
84            'sr-el' => 'sr',
85        ];
86    }
87
88    protected function getAdditionalFlags(): array {
89        return [
90            'S' => 'S',
91            'писмо' => 'S',
92            'pismo' => 'S',
93            'W' => 'W',
94            'реч' => 'W',
95            'reč' => 'W',
96            'ријеч' => 'W',
97            'riječ' => 'W'
98        ];
99    }
100
101    protected function loadDefaultTables(): array {
102        return [
103            'sr-ec' => new ReplacementArray( self::TO_CYRILLICS ),
104            'sr-el' => new ReplacementArray( self::TO_LATIN ),
105            'sr' => new ReplacementArray()
106        ];
107    }
108
109    /**
110     * Omits roman numbers
111     *
112     * @inheritDoc
113     */
114    public function translate( $text, $variant ) {
115        return $this->translateWithoutRomanNumbers( $text, $variant );
116    }
117
118    public function guessVariant( $text, $variant ) {
119        $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
120        $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
121
122        if ( $variant == 'sr-ec' ) {
123            return $numCyrillic > $numLatin;
124        } elseif ( $variant == 'sr-el' ) {
125            return $numLatin > $numCyrillic;
126        } else {
127            return false;
128        }
129    }
130}