MediaWiki  master
LanguageCode.php
Go to the documentation of this file.
1 <?php
27 class LanguageCode {
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 
219  public static function bcp47ToInternal( $code ) {
220  static $invertedLookup = [];
221  if ( !$invertedLookup ) {
222  // There should never be two different entries in
223  // NON_STANDARD_LANGUAGE_CODE_MAPPING which map *different*
224  // internal codes to the same external BCP-47 code. That is,
225  // BCP-47 should preserve all the information from the internal
226  // code (discussed further above)[*]. But note the converse isn't
227  // true: multiple BCP-47 codes can alias to the same internal code:
228  // BCP-47 internal
229  // zh-Hans-CN => zh-cn (in NON_STANDARD_LANGUAGE_CODE_MAPPING)
230  // zh-Hans => zh-hans (not in " )
231  // zh-CN => zh-cn (not in " )
232  //
233  // [*] eml/egl are the "exception that proves the rule": `egl` *is*
234  // (prematurely?) defined as an internal code, but only
235  // eml.wikipedia.org exists, and it defines its language as `eml`;
236  // for internal purposes `egl` should map back into `eml` until
237  // `eml` is deprecated (aka an `eml => egl` entry is added to
238  // DEPRECATED_LANGUAGE_CODE_MAPPING): T36217.
239  foreach ( self::NON_STANDARD_LANGUAGE_CODE_MAPPING as $internal => $bcp47 ) {
240  $invertedLookup[strtolower( $bcp47 )] = $internal;
241  }
242  // We deliberately do *not* use DEPRECATED_LANGUAGE_CODE_MAPPING
243  // here: deprecated codes are no longer valid mediawiki internal
244  // codes and we should never return them.
245  }
246  // Internal codes are all lowercase. This also achieves
247  // case-insensitivity in the lookup.
248  $code = strtolower( $code );
249  return $invertedLookup[$code] ?? $code;
250  }
251 
267  public static function isWellFormedLanguageTag( string $code, bool $lenient = false ): bool {
268  $alpha = '[a-z]';
269  $digit = '[0-9]';
270  $alphanum = '[a-z0-9]';
271  $x = 'x'; # private use singleton
272  $singleton = '[a-wy-z]'; # other singleton
273  $s = $lenient ? '[-_]' : '-';
274 
275  $language = "$alpha{2,8}|$alpha{2,3}$s$alpha{3}";
276  $script = "$alpha{4}"; # ISO 15924
277  $region = "(?:$alpha{2}|$digit{3})"; # ISO 3166-1 alpha-2 or UN M.49
278  $variant = "(?:$alphanum{5,8}|$digit$alphanum{3})";
279  $extension = "$singleton(?:$s$alphanum{2,8})+";
280  $privateUse = "$x(?:$s$alphanum{1,8})+";
281 
282  # Define certain legacy language tags (marked as “Type: grandfathered” in BCP 47),
283  # since otherwise the regex is pretty useless.
284  # Since these are limited, this is safe even later changes to the registry --
285  # the only oddity is that it might change the type of the tag, and thus
286  # the results from the capturing groups.
287  # https://www.iana.org/assignments/language-subtag-registry
288 
289  $legacy = "en{$s}GB{$s}oed"
290  . "|i{$s}(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)"
291  . "|no{$s}(?:bok|nyn)"
292  . "|sgn{$s}(?:BE{$s}(?:fr|nl)|CH{$s}de)"
293  . "|zh{$s}min{$s}nan";
294 
295  $variantList = "$variant(?:$s$variant)*";
296  $extensionList = "$extension(?:$s$extension)*";
297 
298  $langtag = "(?:($language)"
299  . "(?:$s$script)?"
300  . "(?:$s$region)?"
301  . "(?:$s$variantList)?"
302  . "(?:$s$extensionList)?"
303  . "(?:$s$privateUse)?)";
304 
305  # Here is the final breakdown, with capturing groups for each of these components
306  # The variants, extensions, legacy, and private-use may have interior '-'
307 
308  $root = "^(?:$langtag|$privateUse|$legacy)$";
309 
310  return preg_match( "/$root/", strtolower( $code ) );
311  }
312 }
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 bcp47ToInternal( $code)
Convert standardized BCP 47 codes to the internal names used by MediaWiki and returned by Language::g...
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 IANA 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