MediaWiki  master
LanguageCode.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\Bcp47Code\Bcp47Code;
22 use Wikimedia\Bcp47Code\Bcp47CodeValue;
23 
30 class LanguageCode {
42  private const DEPRECATED_LANGUAGE_CODE_MAPPING = [
43  // Note that als is actually a valid ISO 639 code (Tosk Albanian), but it
44  // was previously used in MediaWiki for Alsatian, which comes under gsw
45  'als' => 'gsw', // T25215
46  'bat-smg' => 'sgs', // T27522
47  'be-x-old' => 'be-tarask', // T11823
48  'fiu-vro' => 'vro', // T31186
49  'roa-rup' => 'rup', // T17988
50  'zh-classical' => 'lzh', // T30443
51  'zh-min-nan' => 'nan', // T30442
52  'zh-yue' => 'yue', // T30441
53  ];
54 
81  private const NON_STANDARD_LANGUAGE_CODE_MAPPING = [
82  // All codes returned by LanguageNameUtils::getLanguageNames() validated
83  // against IANA registry at
84  // https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
85  // with help of validator at
86  // http://schneegans.de/lv/
87  'cbk-zam' => 'cbk', // T124657
88  'de-formal' => 'de-x-formal',
89  'eml' => 'egl', // T36217
90  'en-rtl' => 'en-x-rtl',
91  'es-formal' => 'es-x-formal',
92  'hu-formal' => 'hu-x-formal',
93  'map-bms' => 'jv-x-bms', // [[en:Banyumasan_dialect]] T125073
94  'mo' => 'ro-Cyrl-MD', // T125073
95  'nrm' => 'nrf', // [[en:Norman_language]] T25216
96  'nl-informal' => 'nl-x-informal',
97  'roa-tara' => 'nap-x-tara', // [[en:Tarantino_dialect]]
98  'simple' => 'en-simple',
99  'sr-ec' => 'sr-Cyrl', // T117845
100  'sr-el' => 'sr-Latn', // T117845
101 
102  // Although these next codes aren't *wrong* per se, including
103  // both the script and the country code helps compatibility with
104  // other BCP 47 users. Note that MW also uses
105  // `kk-Arab`/`kk-Cyrl`/`kk-Latn`, `zh-Hans`/`zh-Hant`,
106  // without a country code, and those should be left alone.
107  // `kk` has the Suppress-Script: Cyrl field, so `kk-KZ` won't be mapped
108  // to `kk-Cyrl-KZ`.
109  // (See getVariantsFallbacks() in KkConverter.php for Arab/Cyrl/Latn id.)
110  // (See getVariantsFallbacks() in ZhConverter.php for Hans/Hant id.)
111  'crh-ro' => 'crh-Latn-RO',
112  'kk-cn' => 'kk-Arab-CN',
113  'kk-tr' => 'kk-Latn-TR',
114  'zh-cn' => 'zh-Hans-CN',
115  'zh-sg' => 'zh-Hans-SG',
116  'zh-my' => 'zh-Hans-MY',
117  'zh-tw' => 'zh-Hant-TW',
118  'zh-hk' => 'zh-Hant-HK',
119  'zh-mo' => 'zh-Hant-MO',
120  ];
121 
134  public static function getDeprecatedCodeMapping() {
135  return self::DEPRECATED_LANGUAGE_CODE_MAPPING;
136  }
137 
150  public static function getNonstandardLanguageCodeMapping() {
151  static $result = [];
152  if ( $result ) {
153  return $result;
154  }
155  foreach ( self::DEPRECATED_LANGUAGE_CODE_MAPPING as $code => $ignore ) {
156  $result[$code] = self::bcp47( $code );
157  }
158  foreach ( self::NON_STANDARD_LANGUAGE_CODE_MAPPING as $code => $ignore ) {
159  $result[$code] = self::bcp47( $code );
160  }
161  return $result;
162  }
163 
174  public static function replaceDeprecatedCodes( $code ) {
175  return self::DEPRECATED_LANGUAGE_CODE_MAPPING[$code] ?? $code;
176  }
177 
188  public static function bcp47( $code ) {
189  $code = self::replaceDeprecatedCodes( strtolower( $code ) );
190  if ( isset( self::NON_STANDARD_LANGUAGE_CODE_MAPPING[$code] ) ) {
191  $code = self::NON_STANDARD_LANGUAGE_CODE_MAPPING[$code];
192  }
193  $codeSegment = explode( '-', $code );
194  $codeBCP = [];
195  foreach ( $codeSegment as $segNo => $seg ) {
196  // when previous segment is x, it is a private segment and should be lc
197  if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
198  $codeBCP[$segNo] = strtolower( $seg );
199  // ISO 3166 country code
200  } elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
201  $codeBCP[$segNo] = strtoupper( $seg );
202  // ISO 15924 script code
203  } elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
204  $codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
205  // Use lowercase for other cases
206  } else {
207  $codeBCP[$segNo] = strtolower( $seg );
208  }
209  }
210  $langCode = implode( '-', $codeBCP );
211  return $langCode;
212  }
213 
232  public static function bcp47ToInternal( string $code ): string {
233  static $invertedLookup = [];
234  if ( !$invertedLookup ) {
235  // There should never be two different entries in
236  // NON_STANDARD_LANGUAGE_CODE_MAPPING which map *different*
237  // internal codes to the same external BCP-47 code. That is,
238  // BCP-47 should preserve all the information from the internal
239  // code (discussed further above)[*]. But note the converse isn't
240  // true: multiple BCP-47 codes can alias to the same internal code:
241  // BCP-47 internal
242  // zh-Hans-CN => zh-cn (in NON_STANDARD_LANGUAGE_CODE_MAPPING)
243  // zh-Hans => zh-hans (not in " )
244  // zh-CN => zh-cn (not in " )
245  //
246  // [*] eml/egl are the "exception that proves the rule": `egl` *is*
247  // (prematurely?) defined as an internal code, but only
248  // eml.wikipedia.org exists, and it defines its language as `eml`;
249  // for internal purposes `egl` should map back into `eml` until
250  // `eml` is deprecated (aka an `eml => egl` entry is added to
251  // DEPRECATED_LANGUAGE_CODE_MAPPING): T36217.
252  foreach ( self::NON_STANDARD_LANGUAGE_CODE_MAPPING as $internal => $bcp47 ) {
253  $invertedLookup[strtolower( $bcp47 )] = $internal;
254  }
255  // We deliberately do *not* use DEPRECATED_LANGUAGE_CODE_MAPPING
256  // here: deprecated codes are no longer valid mediawiki internal
257  // codes and we should never return them.
258  }
259  // Internal codes are all lowercase. This also achieves
260  // case-insensitivity in the lookup.
261  $code = strtolower( $code );
262  return $invertedLookup[$code] ?? $code;
263  }
264 
277  public static function normalizeNonstandardCodeAndWarn( string $code ): Bcp47Code {
278  $compatMap = self::getNonstandardLanguageCodeMapping();
279  if ( isset( $compatMap[strtolower( $code )] ) ) {
280  // Backward compatibility, since clients may have been
281  // sending us non-standards-compliant
282  // "mediawiki internal language codes"; eventually we'll
283  // emit a logged warning here.
284  $code = $compatMap[strtolower( $code )];
285  }
286  return new Bcp47CodeValue( $code );
287  }
288 
304  public static function isWellFormedLanguageTag( string $code, bool $lenient = false ): bool {
305  $alpha = '[a-z]';
306  $digit = '[0-9]';
307  $alphanum = '[a-z0-9]';
308  $x = 'x'; # private use singleton
309  $singleton = '[a-wy-z]'; # other singleton
310  $s = $lenient ? '[-_]' : '-';
311 
312  $language = "$alpha{2,8}|$alpha{2,3}$s$alpha{3}";
313  $script = "$alpha{4}"; # ISO 15924
314  $region = "(?:$alpha{2}|$digit{3})"; # ISO 3166-1 alpha-2 or UN M.49
315  $variant = "(?:$alphanum{5,8}|$digit$alphanum{3})";
316  $extension = "$singleton(?:$s$alphanum{2,8})+";
317  $privateUse = "$x(?:$s$alphanum{1,8})+";
318 
319  # Define certain legacy language tags (marked as “Type: grandfathered” in BCP 47),
320  # since otherwise the regex is pretty useless.
321  # Since these are limited, this is safe even later changes to the registry --
322  # the only oddity is that it might change the type of the tag, and thus
323  # the results from the capturing groups.
324  # https://www.iana.org/assignments/language-subtag-registry
325 
326  $legacy = "en{$s}GB{$s}oed"
327  . "|i{$s}(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)"
328  . "|no{$s}(?:bok|nyn)"
329  . "|sgn{$s}(?:BE{$s}(?:fr|nl)|CH{$s}de)"
330  . "|zh{$s}min{$s}nan";
331 
332  $variantList = "$variant(?:$s$variant)*";
333  $extensionList = "$extension(?:$s$extension)*";
334 
335  $langtag = "(?:($language)"
336  . "(?:$s$script)?"
337  . "(?:$s$region)?"
338  . "(?:$s$variantList)?"
339  . "(?:$s$extensionList)?"
340  . "(?:$s$privateUse)?)";
341 
342  # Here is the final breakdown, with capturing groups for each of these components
343  # The variants, extensions, legacy, and private-use may have interior '-'
344 
345  $root = "^(?:$langtag|$privateUse|$legacy)$";
346 
347  return preg_match( "/$root/", strtolower( $code ) );
348  }
349 }
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 normalizeNonstandardCodeAndWarn(string $code)
We want to eventually require valid BCP-47 codes on HTTP and HTML APIs (where the standards require i...
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 bcp47ToInternal(string $code)
Convert standardized BCP 47 codes to the internal names used by MediaWiki and returned by Language::g...
static isWellFormedLanguageTag(string $code, bool $lenient=false)
Returns true if a language code string is a well-formed language tag according to RFC 5646.