MediaWiki REL1_39
LanguageCode.php
Go to the documentation of this file.
1<?php
39 private const DEPRECATED_LANGUAGE_CODE_MAPPING = [
40 // Note that als is actually a valid ISO 639 code (Tosk Albanian), but it
41 // was previously used in MediaWiki for Alsatian, which comes under gsw
42 'als' => 'gsw', // T25215
43 'bat-smg' => 'sgs', // T27522
44 'be-x-old' => 'be-tarask', // T11823
45 'fiu-vro' => 'vro', // T31186
46 'roa-rup' => 'rup', // T17988
47 'zh-classical' => 'lzh', // T30443
48 'zh-min-nan' => 'nan', // T30442
49 'zh-yue' => 'yue', // T30441
50 ];
51
78 private const NON_STANDARD_LANGUAGE_CODE_MAPPING = [
79 // All codes returned by LanguageNameUtils::getLanguageNames() validated
80 // against IANA registry at
81 // https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
82 // with help of validator at
83 // http://schneegans.de/lv/
84 'cbk-zam' => 'cbk', // T124657
85 'de-formal' => 'de-x-formal',
86 'eml' => 'egl', // T36217
87 'en-rtl' => 'en-x-rtl',
88 'es-formal' => 'es-x-formal',
89 'hu-formal' => 'hu-x-formal',
90 'map-bms' => 'jv-x-bms', // [[en:Banyumasan_dialect]] T125073
91 'mo' => 'ro-Cyrl-MD', // T125073
92 'nrm' => 'nrf', // [[en:Norman_language]] T25216
93 'nl-informal' => 'nl-x-informal',
94 'roa-tara' => 'nap-x-tara', // [[en:Tarantino_dialect]]
95 'simple' => 'en-simple',
96 'sr-ec' => 'sr-Cyrl', // T117845
97 'sr-el' => 'sr-Latn', // T117845
98
99 // Although these next codes aren't *wrong* per se, including
100 // both the script and the country code helps compatibility with
101 // other BCP 47 users. Note that MW also uses `zh-Hans`/`zh-Hant`,
102 // without a country code, and those should be left alone.
103 // (See $variantfallbacks in LanguageZh.php for Hans/Hant id.)
104 'zh-cn' => 'zh-Hans-CN',
105 'zh-sg' => 'zh-Hans-SG',
106 'zh-my' => 'zh-Hans-MY',
107 'zh-tw' => 'zh-Hant-TW',
108 'zh-hk' => 'zh-Hant-HK',
109 'zh-mo' => 'zh-Hant-MO',
110 ];
111
124 public static function getDeprecatedCodeMapping() {
125 return self::DEPRECATED_LANGUAGE_CODE_MAPPING;
126 }
127
140 public static function getNonstandardLanguageCodeMapping() {
141 $result = [];
142 foreach ( self::DEPRECATED_LANGUAGE_CODE_MAPPING as $code => $ignore ) {
143 $result[$code] = self::bcp47( $code );
144 }
145 foreach ( self::NON_STANDARD_LANGUAGE_CODE_MAPPING as $code => $ignore ) {
146 $result[$code] = self::bcp47( $code );
147 }
148 return $result;
149 }
150
161 public static function replaceDeprecatedCodes( $code ) {
162 return self::DEPRECATED_LANGUAGE_CODE_MAPPING[$code] ?? $code;
163 }
164
175 public static function bcp47( $code ) {
176 $code = self::replaceDeprecatedCodes( strtolower( $code ) );
177 if ( isset( self::NON_STANDARD_LANGUAGE_CODE_MAPPING[$code] ) ) {
178 $code = self::NON_STANDARD_LANGUAGE_CODE_MAPPING[$code];
179 }
180 $codeSegment = explode( '-', $code );
181 $codeBCP = [];
182 foreach ( $codeSegment as $segNo => $seg ) {
183 // when previous segment is x, it is a private segment and should be lc
184 if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
185 $codeBCP[$segNo] = strtolower( $seg );
186 // ISO 3166 country code
187 } elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
188 $codeBCP[$segNo] = strtoupper( $seg );
189 // ISO 15924 script code
190 } elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
191 $codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
192 // Use lowercase for other cases
193 } else {
194 $codeBCP[$segNo] = strtolower( $seg );
195 }
196 }
197 $langCode = implode( '-', $codeBCP );
198 return $langCode;
199 }
200
216 public static function isWellFormedLanguageTag( string $code, bool $lenient = false ): bool {
217 $alpha = '[a-z]';
218 $digit = '[0-9]';
219 $alphanum = '[a-z0-9]';
220 $x = 'x'; # private use singleton
221 $singleton = '[a-wy-z]'; # other singleton
222 $s = $lenient ? '[-_]' : '-';
223
224 $language = "$alpha{2,8}|$alpha{2,3}$s$alpha{3}";
225 $script = "$alpha{4}"; # ISO 15924
226 $region = "(?:$alpha{2}|$digit{3})"; # ISO 3166-1 alpha-2 or UN M.49
227 $variant = "(?:$alphanum{5,8}|$digit$alphanum{3})";
228 $extension = "$singleton(?:$s$alphanum{2,8})+";
229 $privateUse = "$x(?:$s$alphanum{1,8})+";
230
231 # Define certain legacy language tags (marked as “Type: grandfathered” in BCP 47),
232 # since otherwise the regex is pretty useless.
233 # Since these are limited, this is safe even later changes to the registry --
234 # the only oddity is that it might change the type of the tag, and thus
235 # the results from the capturing groups.
236 # https://www.iana.org/assignments/language-subtag-registry
237
238 $legacy = "en{$s}GB{$s}oed"
239 . "|i{$s}(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)"
240 . "|no{$s}(?:bok|nyn)"
241 . "|sgn{$s}(?:BE{$s}(?:fr|nl)|CH{$s}de)"
242 . "|zh{$s}min{$s}nan";
243
244 $variantList = "$variant(?:$s$variant)*";
245 $extensionList = "$extension(?:$s$extension)*";
246
247 $langtag = "(?:($language)"
248 . "(?:$s$script)?"
249 . "(?:$s$region)?"
250 . "(?:$s$variantList)?"
251 . "(?:$s$extensionList)?"
252 . "(?:$s$privateUse)?)";
253
254 # Here is the final breakdown, with capturing groups for each of these components
255 # The variants, extensions, legacy, and private-use may have interior '-'
256
257 $root = "^(?:$langtag|$privateUse|$legacy)$";
258
259 return preg_match( "/$root/", strtolower( $code ) );
260 }
261}
Methods for dealing with language codes.
static getNonstandardLanguageCodeMapping()
Returns a mapping of non-standard language codes used by (current and previous version of) MediaWiki,...
static replaceDeprecatedCodes( $code)
Replace deprecated language codes that were used in previous versions of MediaWiki to up-to-date,...
static getDeprecatedCodeMapping()
Returns a mapping of deprecated language codes that were used in previous versions of MediaWiki to up...
static bcp47( $code)
Get the normalised IETF language tag See unit test for examples.
static isWellFormedLanguageTag(string $code, bool $lenient=false)
Returns true if a language code string is a well-formed language tag according to RFC 5646.
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s