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
ShiConverter
73.68% covered (warning)
73.68%
14 / 19
80.00% covered (warning)
80.00%
4 / 5
7.89
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%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 translate
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
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 * Shilha specific code.
25 *
26 * Conversion script between Latin and Tifinagh for Tachelhit.
27 * - Tifinagh -> lowercase Latin
28 * - lowercase/uppercase Latin -> Tifinagh
29 *
30 *
31 * Based on:
32 *   - https://en.wikipedia.org/wiki/Shilha_language
33 *   - LanguageSr.php
34 *
35 * @ingroup Languages
36 */
37class ShiConverter extends LanguageConverterSpecific {
38    /**
39     * The Tifinagh alphabet sequence is based on
40     * "Dictionnaire Général de la Langue Amazighe Informatisé"
41     * by IRCAM (https://tal.ircam.ma/dglai/lexieam.php, DGLAi),
42     * with the labio-velarization mark in the end
43     */
44    private const TO_LATIN = [
45        'ⴰ' => 'a',
46        'ⴱ' => 'b',
47        'ⴳ' => 'g',
48        'ⴷ' => 'd',
49        'ⴹ' => 'ḍ',
50        'ⴻ' => 'e',
51        'ⴼ' => 'f',
52        'ⴽ' => 'k',
53        'ⵀ' => 'h',
54        'ⵃ' => 'ḥ',
55        'ⵄ' => 'ɛ',
56        'ⵅ' => 'x',
57        'ⵇ' => 'q',
58        'ⵉ' => 'i',
59        'ⵊ' => 'j',
60        'ⵍ' => 'l',
61        'ⵎ' => 'm',
62        'ⵏ' => 'n',
63        'ⵓ' => 'u',
64        'ⵔ' => 'r',
65        'ⵕ' => 'ṛ',
66        'ⵖ' => 'ɣ',
67        'ⵙ' => 's',
68        'ⵚ' => 'ṣ',
69        'ⵛ' => 'c',
70        'ⵜ' => 't',
71        'ⵟ' => 'ṭ',
72        'ⵡ' => 'w',
73        'ⵢ' => 'y',
74        'ⵣ' => 'z',
75        'ⵥ' => 'ẓ',
76        'ⵯ' => 'ʷ',
77    ];
78
79    /** The sequence is based on DGLAi, with the non-standard letters in the end */
80    private const UPPER_TO_LOWER_CASE_LATIN = [
81        'A' => 'a',
82        'B' => 'b',
83        'G' => 'g',
84        'D' => 'd',
85        'Ḍ' => 'ḍ',
86        'E' => 'e',
87        'F' => 'f',
88        'K' => 'k',
89        'H' => 'h',
90        'Ḥ' => 'ḥ',
91        'Ɛ' => 'ɛ',
92        'X' => 'x',
93        'Q' => 'q',
94        'I' => 'i',
95        'J' => 'j',
96        'L' => 'l',
97        'M' => 'm',
98        'N' => 'n',
99        'U' => 'u',
100        'R' => 'r',
101        'Ṛ' => 'ṛ',
102        'Ɣ' => 'ɣ',
103        'S' => 's',
104        'Ṣ' => 'ṣ',
105        'C' => 'c',
106        'T' => 't',
107        'Ṭ' => 'ṭ',
108        'W' => 'w',
109        'Y' => 'y',
110        'Z' => 'z',
111        'Ẓ' => 'ẓ',
112        'O' => 'o',
113        'P' => 'p',
114        'V' => 'v',
115    ];
116
117    /**
118     * The sequence is based on DGLAi, with the labio-velarization mark and
119     * the non-standard letters in the end
120     */
121    private const TO_TIFINAGH = [
122        'a' => 'ⴰ',
123        'b' => 'ⴱ',
124        'g' => 'ⴳ',
125        'd' => 'ⴷ',
126        'ḍ' => 'ⴹ',
127        'e' => 'ⴻ',
128        'f' => 'ⴼ',
129        'k' => 'ⴽ',
130        'h' => 'ⵀ',
131        'ḥ' => 'ⵃ',
132        'ɛ' => 'ⵄ',
133        'x' => 'ⵅ',
134        'q' => 'ⵇ',
135        'i' => 'ⵉ',
136        'j' => 'ⵊ',
137        'l' => 'ⵍ',
138        'm' => 'ⵎ',
139        'n' => 'ⵏ',
140        'u' => 'ⵓ',
141        'r' => 'ⵔ',
142        'ṛ' => 'ⵕ',
143        'ɣ' => 'ⵖ',
144        's' => 'ⵙ',
145        'ṣ' => 'ⵚ',
146        'c' => 'ⵛ',
147        't' => 'ⵜ',
148        'ṭ' => 'ⵟ',
149        'w' => 'ⵡ',
150        'y' => 'ⵢ',
151        'z' => 'ⵣ',
152        'ẓ' => 'ⵥ',
153        'ʷ' => 'ⵯ',
154        'o' => 'ⵓ',
155        'p' => 'ⴱ',
156        'v' => 'ⴼ',
157    ];
158
159    public function getMainCode(): string {
160        return 'shi';
161    }
162
163    public function getLanguageVariants(): array {
164        return [ 'shi', 'shi-tfng', 'shi-latn' ];
165    }
166
167    public function getVariantsFallbacks(): array {
168        return [
169            'shi' => [ 'shi-latn', 'shi-tfng' ],
170            'shi-tfng' => 'shi',
171            'shi-latn' => 'shi',
172        ];
173    }
174
175    protected function loadDefaultTables(): array {
176        return [
177            'lowercase' => new ReplacementArray( self::UPPER_TO_LOWER_CASE_LATIN ),
178            'shi-tfng' => new ReplacementArray( self::TO_TIFINAGH ),
179            'shi-latn' => new ReplacementArray( self::TO_LATIN ),
180            'shi' => new ReplacementArray()
181        ];
182    }
183
184    public function translate( $text, $toVariant ) {
185        // If $text is empty or only includes spaces, do nothing
186        // Otherwise translate it
187        if ( trim( $text ) ) {
188            $this->loadTables();
189            // For Tifinagh, first translate uppercase to lowercase Latin
190            if ( $toVariant == 'shi-tfng' ) {
191                $text = $this->mTables['lowercase']->replace( $text );
192            }
193            $text = $this->mTables[$toVariant]->replace( $text );
194        }
195        return $text;
196    }
197}