Debian Codegraph @

includes/language/LanguageCode.php

raw symbols SCIP
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 * Any other code 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 }
258 if ( $code instanceof Bcp47Code ) {
259 $code = $code->toBcp47Code();
260 }
261 static $invertedLookup = [];
262 if ( !$invertedLookup ) {
263 // There should never be two different entries in
264 // NON_STANDARD_LANGUAGE_CODE_MAPPING that map *different*
265 // internal codes to the same external BCP-47 code. That is,
266 // BCP-47 should preserve all the information from the internal
267 // code (discussed further above)[*]. But note the converse isn't
268 // true: multiple BCP-47 codes can alias to the same internal code:
269 // BCP-47 internal
270 // zh-Hans-CN => zh-cn (in NON_STANDARD_LANGUAGE_CODE_MAPPING)
271 // zh-Hans => zh-hans (not in " )
272 // zh-CN => zh-cn (not in " )
273 //
274 // [*] eml/egl are the "exception that proves the rule": `egl` *is*
275 // (prematurely?) defined as an internal code, but only
276 // eml.wikipedia.org exists, and it defines its language as `eml`;
277 // for internal purposes `egl` should map back into `eml` until
278 // `eml` is deprecated (aka an `eml => egl` entry is added to
279 // DEPRECATED_LANGUAGE_CODE_MAPPING): T36217.
280 foreach ( self::NON_STANDARD_LANGUAGE_CODE_MAPPING as $internal => $bcp47 ) {
281 $invertedLookup[strtolower( $bcp47 )] = $internal;
282 }
283 // We deliberately do *not* use DEPRECATED_LANGUAGE_CODE_MAPPING
284 // here: deprecated codes are no longer valid mediawiki internal
285 // codes, and we should never return them.
286 }
287 // Internal codes are all lowercase. This also achieves
288 // case-insensitivity in the lookup.
289 $code = strtolower( $code );
290 return $invertedLookup[$code] ?? $code;
291 }
292
293 /**
294 * We want to eventually require valid BCP-47 codes on HTTP and HTML
295 * APIs (where the standards require it). This will "prefer" to
296 * interpret the given $code as BCP-47, but if a mediawiki internal
297 * code is provided, it will map it to the proper BCP-47 code. We
298 * don't emit a logged warning on this path yet, but we intend to
299 * in the future.
300 *
301 * @param string $code A "language code" provided from an HTTP or HTML
302 * API, presumed to be BCP-47
303 * @return Bcp47Code An "actual" BCP-47 code
304 * @internal
305 */
306 public static function normalizeNonstandardCodeAndWarn( string $code ): Bcp47Code {
307 $compatMap = self::getNonstandardLanguageCodeMapping();
308 if ( isset( $compatMap[strtolower( $code )] ) ) {
309 // Backward compatibility, since clients may have been
310 // sending us non-standards-compliant
311 // "mediawiki internal language codes"; eventually we'll
312 // emit a logged warning here.
313 $code = $compatMap[strtolower( $code )];
314 }
315 return new Bcp47CodeValue( $code );
316 }
317
318 /**
319 * Returns true if a language code string is a well-formed language tag
320 * according to RFC 5646.
321 * This function only checks well-formedness; it doesn't check that
322 * language, script or variant codes actually exist in the repositories.
323 *
324 * Based on regexes by Mark Davis of the Unicode Consortium:
325 * https://github.com/unicode-org/icu/blob/37e295627156bc334e1f1e88807025fac984da0e/icu4j/main/tests/translit/src/com/ibm/icu/dev/test/translit/langtagRegex.txt
326 *
327 * @param string $code
328 * @param bool $lenient Whether to allow '_' as separator. The default is only '-'.
329 *
330 * @return bool
331 * @since 1.39
332 */
333 public static function isWellFormedLanguageTag( string $code, bool $lenient = false ): bool {
334 $alpha = '[a-z]';
335 $digit = '[0-9]';
336 $alphanum = '[a-z0-9]';
337 $x = 'x'; # private use singleton
338 $singleton = '[a-wy-z]'; # other singleton
339 $s = $lenient ? '[-_]' : '-';
340
341 $language = "$alpha{2,8}|$alpha{2,3}$s$alpha{3}";
342 $script = "$alpha{4}"; # ISO 15924
343 $region = "(?:$alpha{2}|$digit{3})"; # ISO 3166-1 alpha-2 or UN M.49
344 $variant = "(?:$alphanum{5,8}|$digit$alphanum{3})";
345 $extension = "$singleton(?:$s$alphanum{2,8})+";
346 $privateUse = "$x(?:$s$alphanum{1,8})+";
347
348 # Define certain legacy language tags (marked as “Type: grandfathered” in BCP 47),
349 # since otherwise the regex is pretty useless.
350 # Since these are limited, this is safe even later changes to the registry --
351 # the only oddity is that it might change the type of the tag, and thus
352 # the results from the capturing groups.
353 # https://www.iana.org/assignments/language-subtag-registry
354
355 $legacy = "en{$s}gb{$s}oed"
356 . "|i{$s}(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)"
357 . "|no{$s}(?:bok|nyn)"
358 . "|sgn{$s}(?:be{$s}(?:fr|nl)|ch{$s}de)"
359 . "|zh{$s}min{$s}nan";
360
361 $variantList = "$variant(?:$s$variant)*";
362 $extensionList = "$extension(?:$s$extension)*";
363
364 $langtag = "(?:($language)"
365 . "(?:$s$script)?"
366 . "(?:$s$region)?"
367 . "(?:$s$variantList)?"
368 . "(?:$s$extensionList)?"
369 . "(?:$s$privateUse)?)";
370
371 # Here is the final breakdown, with capturing groups for each of these components
372 # The variants, extensions, legacy, and private-use may have interior '-'
373
374 $root = "^(?:$langtag|$privateUse|$legacy)$";
375
376 return preg_match( "/$root/i", $code );
377 }
378 }
379
380 /** @deprecated class alias since 1.43 */
381 class_alias( LanguageCode::class, 'LanguageCode' );