Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
46.67% |
14 / 30 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
SrConverter | |
46.67% |
14 / 30 |
|
57.14% |
4 / 7 |
21.29 | |
0.00% |
0 / 1 |
getMainCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLanguageVariants | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getVariantsFallbacks | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getAdditionalFlags | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
loadDefaultTables | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
translate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
guessVariant | |
85.71% |
6 / 7 |
|
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 | |
21 | /** |
22 | * Serbian (Српски / Srpski) specific code. |
23 | * |
24 | * There are two levels of conversion for Serbian: the script level |
25 | * (Cyrillics <-> Latin), and the variant level (ekavian |
26 | * <->iyekavian). The two are orthogonal. So we really only need two |
27 | * dictionaries: one for Cyrillics and Latin, and one for ekavian and |
28 | * iyekavian. |
29 | * |
30 | * @ingroup Languages |
31 | */ |
32 | class SrConverter extends LanguageConverterSpecific { |
33 | |
34 | private const TO_LATIN = [ |
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ž', 'ш' => 'š', |
41 | |
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ž', 'Ш' => 'Š', |
48 | ]; |
49 | |
50 | private const TO_CYRILLICS = [ |
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' => 'з', 'ž' => 'ж', |
57 | |
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' => 'З', 'Ž' => 'Ж', |
64 | |
65 | 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ', |
66 | 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ', |
67 | 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ' |
68 | ]; |
69 | |
70 | public function getMainCode(): string { |
71 | return 'sr'; |
72 | } |
73 | |
74 | public function getLanguageVariants(): array { |
75 | return [ 'sr', 'sr-ec', 'sr-el' ]; |
76 | } |
77 | |
78 | public function getVariantsFallbacks(): array { |
79 | return [ |
80 | 'sr' => 'sr-ec', |
81 | 'sr-ec' => 'sr', |
82 | 'sr-el' => 'sr', |
83 | ]; |
84 | } |
85 | |
86 | protected function getAdditionalFlags(): array { |
87 | return [ |
88 | 'S' => 'S', |
89 | 'писмо' => 'S', |
90 | 'pismo' => 'S', |
91 | 'W' => 'W', |
92 | 'реч' => 'W', |
93 | 'reč' => 'W', |
94 | 'ријеч' => 'W', |
95 | 'riječ' => 'W' |
96 | ]; |
97 | } |
98 | |
99 | protected function loadDefaultTables(): array { |
100 | return [ |
101 | 'sr-ec' => new ReplacementArray( self::TO_CYRILLICS ), |
102 | 'sr-el' => new ReplacementArray( self::TO_LATIN ), |
103 | 'sr' => new ReplacementArray() |
104 | ]; |
105 | } |
106 | |
107 | /** |
108 | * Omits roman numbers |
109 | * |
110 | * @inheritDoc |
111 | */ |
112 | public function translate( $text, $variant ) { |
113 | return $this->translateWithoutRomanNumbers( $text, $variant ); |
114 | } |
115 | |
116 | public function guessVariant( $text, $variant ) { |
117 | $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy ); |
118 | $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy ); |
119 | |
120 | if ( $variant == 'sr-ec' ) { |
121 | return $numCyrillic > $numLatin; |
122 | } elseif ( $variant == 'sr-el' ) { |
123 | return $numLatin > $numCyrillic; |
124 | } else { |
125 | return false; |
126 | } |
127 | } |
128 | } |