Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.94% |
53 / 68 |
|
66.67% |
6 / 9 |
CRAP | |
0.00% |
0 / 1 |
LanguageCode | |
79.10% |
53 / 67 |
|
66.67% |
6 / 9 |
32.17 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDeprecatedCodeMapping | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNonstandardLanguageCodeMapping | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
replaceDeprecatedCodes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
bcp47 | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
9 | |||
bcp47ToInternal | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
normalizeNonstandardCodeAndWarn | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
isWellFormedLanguageTag | |
100.00% |
27 / 27 |
|
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 | |
21 | namespace MediaWiki\Language; |
22 | |
23 | use Wikimedia\Bcp47Code\Bcp47Code; |
24 | use Wikimedia\Bcp47Code\Bcp47CodeValue; |
25 | |
26 | /** |
27 | * Methods for dealing with language codes. |
28 | * |
29 | * @since 1.29 |
30 | * @ingroup Language |
31 | */ |
32 | class LanguageCode { |
33 | |
34 | private string $code; |
35 | |
36 | /** |
37 | * @param string $code |
38 | * @unstable |
39 | * @since 1.43 |
40 | */ |
41 | public function __construct( string $code ) { |
42 | $this->code = $code; |
43 | } |
44 | |
45 | /** |
46 | * @return string |
47 | * @since 1.43 |
48 | */ |
49 | public function toString(): string { |
50 | return $this->code; |
51 | } |
52 | |
53 | /** |
54 | * Mapping of deprecated language codes that were used in previous |
55 | * versions of MediaWiki to up-to-date, current language codes. |
56 | * These may or may not be valid BCP 47 codes; they are included here |
57 | * because MediaWiki renamed these particular codes at some point. |
58 | * |
59 | * @var array Mapping from deprecated MediaWiki-internal language code |
60 | * to replacement MediaWiki-internal language code. |
61 | * |
62 | * @see https://meta.wikimedia.org/wiki/Special_language_codes |
63 | */ |
64 | private const DEPRECATED_LANGUAGE_CODE_MAPPING = [ |
65 | // Note that als is actually a valid ISO 639 code (Tosk Albanian), but it |
66 | // was previously used in MediaWiki for Alsatian, which comes under gsw |
67 | 'als' => 'gsw', // T25215 |
68 | 'bat-smg' => 'sgs', // T27522 |
69 | 'be-x-old' => 'be-tarask', // T11823 |
70 | 'fiu-vro' => 'vro', // T31186 |
71 | 'roa-rup' => 'rup', // T17988 |
72 | 'zh-classical' => 'lzh', // T30443 |
73 | 'zh-min-nan' => 'nan', // T30442 |
74 | 'zh-yue' => 'yue', // T30441 |
75 | ]; |
76 | |
77 | /** |
78 | * Mapping of non-standard language codes used in MediaWiki to |
79 | * standardized BCP 47 codes. These are not deprecated (yet?): |
80 | * IANA may eventually recognize the subtag, in which case the `-x-` |
81 | * infix could be removed, or else we could rename the code in |
82 | * MediaWiki, in which case they'd move up to the above mapping |
83 | * of deprecated codes. |
84 | * |
85 | * As a rule, we preserve all distinctions made by MediaWiki |
86 | * internally. For example, `de-formal` becomes `de-x-formal` |
87 | * instead of just `de` because MediaWiki distinguishes `de-formal` |
88 | * from `de` (for example, for interface translations). Similarly, |
89 | * BCP 47 indicates that `kk-Cyrl` SHOULD not be used because it |
90 | * "typically does not add information", but in our case MediaWiki |
91 | * LanguageConverter distinguishes `kk` (render content in a mix of |
92 | * Kurdish variants) from `kk-Cyrl` (convert content to be uniformly |
93 | * Cyrillic). As the BCP 47 requirement is a SHOULD not a MUST, |
94 | * `kk-Cyrl` is a valid code, although some validators may emit |
95 | * a warning note. |
96 | * |
97 | * @var array Mapping from nonstandard MediaWiki-internal codes to |
98 | * BCP 47 codes |
99 | * |
100 | * @see https://meta.wikimedia.org/wiki/Special_language_codes |
101 | * @see https://phabricator.wikimedia.org/T125073 |
102 | */ |
103 | private const NON_STANDARD_LANGUAGE_CODE_MAPPING = [ |
104 | // All codes returned by LanguageNameUtils::getLanguageNames() validated |
105 | // against IANA registry at |
106 | // https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry |
107 | // with help of validator at |
108 | // http://schneegans.de/lv/ |
109 | 'cbk-zam' => 'cbk', // T124657 |
110 | 'de-formal' => 'de-x-formal', |
111 | 'eml' => 'egl', // T36217 |
112 | 'en-rtl' => 'en-x-rtl', |
113 | 'es-formal' => 'es-x-formal', |
114 | 'hu-formal' => 'hu-x-formal', |
115 | 'map-bms' => 'jv-x-bms', // [[en:Banyumasan_dialect]] T125073 |
116 | 'mo' => 'ro-Cyrl-MD', // T125073 |
117 | 'nrm' => 'nrf', // [[en:Norman_language]] T25216 |
118 | 'nl-informal' => 'nl-x-informal', |
119 | 'roa-tara' => 'nap-x-tara', // [[en:Tarantino_dialect]] |
120 | 'simple' => 'en-simple', |
121 | 'sr-ec' => 'sr-Cyrl', // T117845 |
122 | 'sr-el' => 'sr-Latn', // T117845 |
123 | |
124 | // Although these next codes aren't *wrong* per se, including |
125 | // both the script and the country code helps compatibility with |
126 | // other BCP 47 users. Note that MW also uses |
127 | // `kk-Arab`/`kk-Cyrl`/`kk-Latn`, `zh-Hans`/`zh-Hant`, |
128 | // without a country code, and those should be left alone. |
129 | // `kk` has the Suppress-Script: Cyrl field, so `kk-KZ` won't be mapped |
130 | // to `kk-Cyrl-KZ`. |
131 | // (See getVariantsFallbacks() in KkConverter.php for Arab/Cyrl/Latn id.) |
132 | // (See getVariantsFallbacks() in ZhConverter.php for Hans/Hant id.) |
133 | 'crh-ro' => 'crh-Latn-RO', |
134 | 'kk-cn' => 'kk-Arab-CN', |
135 | 'kk-tr' => 'kk-Latn-TR', |
136 | 'zh-cn' => 'zh-Hans-CN', |
137 | 'zh-sg' => 'zh-Hans-SG', |
138 | 'zh-my' => 'zh-Hans-MY', |
139 | 'zh-tw' => 'zh-Hant-TW', |
140 | 'zh-hk' => 'zh-Hant-HK', |
141 | 'zh-mo' => 'zh-Hant-MO', |
142 | ]; |
143 | |
144 | /** |
145 | * Returns a mapping of deprecated language codes that were used in previous |
146 | * versions of MediaWiki to up-to-date, current language codes. |
147 | * |
148 | * This array is merged into $wgDummyLanguageCodes in |
149 | * SetupDynamicConfig.php, along with the fake language codes |
150 | * 'qqq' and 'qqx', which are used internally by MediaWiki's |
151 | * localisation system. |
152 | * |
153 | * @return string[] |
154 | * |
155 | * @since 1.29 |
156 | */ |
157 | public static function getDeprecatedCodeMapping() { |
158 | return self::DEPRECATED_LANGUAGE_CODE_MAPPING; |
159 | } |
160 | |
161 | /** |
162 | * Returns a mapping of non-standard language codes used by |
163 | * (current and previous version of) MediaWiki, mapped to standard |
164 | * BCP 47 names. |
165 | * |
166 | * This array is exported to JavaScript to ensure |
167 | * mediawiki.language.bcp47 stays in sync with LanguageCode::bcp47(). |
168 | * |
169 | * @return string[] |
170 | * |
171 | * @since 1.32 |
172 | */ |
173 | public static function getNonstandardLanguageCodeMapping() { |
174 | static $result = []; |
175 | if ( $result ) { |
176 | return $result; |
177 | } |
178 | foreach ( self::DEPRECATED_LANGUAGE_CODE_MAPPING as $code => $ignore ) { |
179 | $result[$code] = self::bcp47( $code ); |
180 | } |
181 | foreach ( self::NON_STANDARD_LANGUAGE_CODE_MAPPING as $code => $ignore ) { |
182 | $result[$code] = self::bcp47( $code ); |
183 | } |
184 | return $result; |
185 | } |
186 | |
187 | /** |
188 | * Replace deprecated language codes that were used in previous |
189 | * versions of MediaWiki to up-to-date, current language codes. |
190 | * Other values will be returned unchanged. |
191 | * |
192 | * @param string $code Old language code |
193 | * @return string New language code |
194 | * |
195 | * @since 1.30 |
196 | */ |
197 | public static function replaceDeprecatedCodes( $code ) { |
198 | return self::DEPRECATED_LANGUAGE_CODE_MAPPING[$code] ?? $code; |
199 | } |
200 | |
201 | /** |
202 | * Get the normalised IANA language tag |
203 | * See unit test for examples. |
204 | * See mediawiki.language.bcp47 for the JavaScript implementation. |
205 | * |
206 | * @param string $code The language code. |
207 | * @return string A language code complying with BCP 47 standards. |
208 | * |
209 | * @since 1.31 |
210 | */ |
211 | public static function bcp47( $code ) { |
212 | $code = self::replaceDeprecatedCodes( strtolower( $code ) ); |
213 | if ( isset( self::NON_STANDARD_LANGUAGE_CODE_MAPPING[$code] ) ) { |
214 | $code = self::NON_STANDARD_LANGUAGE_CODE_MAPPING[$code]; |
215 | } |
216 | $codeSegment = explode( '-', $code ); |
217 | $codeBCP = []; |
218 | foreach ( $codeSegment as $segNo => $seg ) { |
219 | // when the previous segment is x, it is a private segment and should be lc |
220 | if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) { |
221 | $codeBCP[$segNo] = strtolower( $seg ); |
222 | // ISO 3166 country code |
223 | } elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) { |
224 | $codeBCP[$segNo] = strtoupper( $seg ); |
225 | // ISO 15924 script code |
226 | } elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) { |
227 | $codeBCP[$segNo] = ucfirst( strtolower( $seg ) ); |
228 | // Use lowercase for other cases |
229 | } else { |
230 | $codeBCP[$segNo] = strtolower( $seg ); |
231 | } |
232 | } |
233 | return implode( '-', $codeBCP ); |
234 | } |
235 | |
236 | /** |
237 | * Convert standardized BCP 47 codes to the internal names used |
238 | * by MediaWiki and returned by Language::getCode(). This function |
239 | * should be the inverse of LanguageCode::bcp47(). Note that BCP 47 |
240 | * explicitly states that language codes are case-insensitive. |
241 | * |
242 | * Since LanguageFactory::getLanguage() is pretty generous about |
243 | * accepting aliases (as long as they are lowercased), this function |
244 | * should be equivalent to: |
245 | * LanguageFactory::getLanguage(strtolower($code))->getCode() |
246 | * but (a) better describes the caller's intention, and (b) should |
247 | * be much more efficient in practice. |
248 | * |
249 | * @param string|Bcp47Code $code The standard BCP-47 language code |
250 | * @return string A MediaWiki-internal code, as returned, for example, by |
251 | * Language::getCode() |
252 | * @since 1.40 |
253 | */ |
254 | public static function bcp47ToInternal( $code ): string { |
255 | if ( $code instanceof Language ) { |
256 | return $code->getCode(); |
257 | } |