Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.68% covered (warning)
73.68%
14 / 19
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
UzConverter
73.68% covered (warning)
73.68%
14 / 19
80.00% covered (warning)
80.00%
4 / 5
6.66
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
 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%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
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\LanguageConverter;
22use MediaWiki\Language\ReplacementArray;
23
24/**
25 * Uzbek specific code.
26 *
27 * @ingroup Languages
28 */
29class UzConverter extends LanguageConverter {
30    private const TO_LATIN = [
31        'а' => 'a', 'А' => 'A',
32        'б' => 'b', 'Б' => 'B',
33        'д' => 'd', 'Д' => 'D',
34        'е' => 'e', 'Е' => 'E',
35        'э' => 'e', 'Э' => 'E',
36        'в' => 'v', 'В' => 'V',
37        'х' => 'x', 'Х' => 'X',
38        'ғ' => 'gʻ', 'Ғ' => 'Gʻ',
39        'г' => 'g', 'Г' => 'G',
40        'ҳ' => 'h', 'Ҳ' => 'H',
41        'ж' => 'j', 'Ж' => 'J',
42        'з' => 'z', 'З' => 'Z',
43        'и' => 'i', 'И' => 'I',
44        'к' => 'k', 'К' => 'K',
45        'л' => 'l', 'Л' => 'L',
46        'м' => 'm', 'М' => 'M',
47        'н' => 'n', 'Н' => 'N',
48        'о' => 'o', 'О' => 'O',
49        'п' => 'p', 'П' => 'P',
50        'р' => 'r', 'Р' => 'R',
51        'с' => 's', 'С' => 'S',
52        'т' => 't', 'Т' => 'T',
53        'у' => 'u', 'У' => 'U',
54        'ф' => 'f', 'Ф' => 'F',
55        'ў' => 'oʻ', 'Ў' => 'Oʻ',
56        // note: at the beginning of a word and right after a consonant, only "s" is used
57        'ц' => 'ts', 'Ц' => 'Ts',
58        'қ' => 'q', 'Қ' => 'Q',
59        'ё' => 'yo', 'Ё' => 'Yo',
60        'ю' => 'yu', 'Ю' => 'Yu',
61        'ч' => 'ch', 'Ч' => 'Ch',
62        'ш' => 'sh', 'Ш' => 'Sh',
63        'й' => 'y', 'Й' => 'Y',
64        'я' => 'ya', 'Я' => 'Ya',
65        'ъ' => 'ʼ',
66    ];
67
68    private const TO_CYRILLIC = [
69        'a' => 'а', 'A' => 'А',
70        'b' => 'б', 'B' => 'Б',
71        'd' => 'д', 'D' => 'Д',
72        // at the beginning of a word and after a vowel, "э" is used instead of "e"
73        // (see regex below)
74        'e' => 'э', 'E' => 'Э',
75        'f' => 'ф', 'F' => 'Ф',
76        'g' => 'г', 'G' => 'Г',
77        'g‘' => 'ғ', 'G‘' => 'Ғ', 'gʻ' => 'ғ', 'Gʻ' => 'Ғ',
78        'h' => 'ҳ', 'H' => 'Ҳ',
79        'i' => 'и', 'I' => 'И',
80        'k' => 'к', 'K' => 'К',
81        'l' => 'л', 'L' => 'Л',
82        'm' => 'м', 'M' => 'М',
83        'n' => 'н', 'N' => 'Н',
84        'o' => 'о', 'O' => 'О',
85        'p' => 'п', 'P' => 'П',
86        'r' => 'р', 'R' => 'Р',
87        's' => 'с', 'S' => 'С',
88        't' => 'т', 'T' => 'Т',
89        'u' => 'у', 'U' => 'У',
90        'v' => 'в', 'V' => 'В',
91        'x' => 'х', 'X' => 'Х',
92        'z' => 'з', 'Z' => 'З',
93        'j' => 'ж', 'J' => 'Ж',
94        'o‘' => 'ў', 'O‘' => 'Ў', 'oʻ' => 'ў', 'Oʻ' => 'Ў',
95        'yo‘' => 'йў', 'Yo‘' => 'Йў', 'yoʻ' => 'йў', 'Yoʻ' => 'Йў',
96        'ts' => 'ц', 'Ts' => 'Ц',
97        'q' => 'қ', 'Q' => 'Қ',
98        'yo' => 'ё', 'Yo' => 'Ё',
99        'yu' => 'ю', 'Yu' => 'Ю',
100        'ch' => 'ч', 'Ch' => 'Ч',
101        'sh' => 'ш', 'Sh' => 'Ш',
102        'y' => 'й', 'Y' => 'Й',
103        'ya' => 'я', 'Ya' => 'Я',
104        'ʼ' => 'ъ',
105    ];
106
107    public function getMainCode(): string {
108        return 'uz';
109    }
110
111    public function getLanguageVariants(): array {
112        return [ 'uz', 'uz-latn', 'uz-cyrl' ];
113    }
114
115    public function getVariantsFallbacks(): array {
116        return [
117            'uz' => 'uz-latn',
118            'uz-cyrl' => 'uz',
119            'uz-latn' => 'uz',
120        ];
121    }
122
123    protected function loadDefaultTables(): array {
124        return [
125            'uz-cyrl' => new ReplacementArray( self::TO_CYRILLIC ),
126            'uz-latn' => new ReplacementArray( self::TO_LATIN ),
127            'uz' => new ReplacementArray()
128        ];
129    }
130
131    public function translate( $text, $toVariant ) {
132        if ( $toVariant == 'uz-cyrl' ) {
133            $text = str_replace( 'ye', 'е', $text );
134            $text = str_replace( 'Ye', 'Е', $text );
135            $text = str_replace( 'YE', 'Е', $text );
136            // "е" after consonants, otherwise "э" (see above)
137            $text = preg_replace( '/([BVGDJZYKLMNPRSTFXCWQʻ‘H])E/u', '$1Е', $text );
138            $text = preg_replace( '/([bvgdjzyklmnprstfxcwqʻ‘h])e/ui', '$1е', $text );
139        }
140        return parent::translate( $text, $toVariant );
141    }
142}