MediaWiki  master
LanguageCode.php
Go to the documentation of this file.
1 <?php
28 class LanguageCode {
40  private const DEPRECATED_LANGUAGE_CODE_MAPPING = [
41  // Note that als is actually a valid ISO 639 code (Tosk Albanian), but it
42  // was previously used in MediaWiki for Alsatian, which comes under gsw
43  'als' => 'gsw', // T25215
44  'bat-smg' => 'sgs', // T27522
45  'be-x-old' => 'be-tarask', // T11823
46  'fiu-vro' => 'vro', // T31186
47  'roa-rup' => 'rup', // T17988
48  'zh-classical' => 'lzh', // T30443
49  'zh-min-nan' => 'nan', // T30442
50  'zh-yue' => 'yue', // T30441
51  ];
52 
79  private const NON_STANDARD_LANGUAGE_CODE_MAPPING = [
80  // All codes returned by Language::fetchLanguageNames() validated
81  // against IANA registry at
82  // https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
83  // with help of validator at
84  // http://schneegans.de/lv/
85  'cbk-zam' => 'cbk', // T124657
86  'de-formal' => 'de-x-formal',
87  'eml' => 'egl', // T36217
88  'en-rtl' => 'en-x-rtl',
89  'es-formal' => 'es-x-formal',
90  'hu-formal' => 'hu-x-formal',
91  'map-bms' => 'jv-x-bms', // [[en:Banyumasan_dialect]] T125073
92  'mo' => 'ro-Cyrl-MD', // T125073
93  'nrm' => 'nrf', // [[en:Norman_language]] T25216
94  'nl-informal' => 'nl-x-informal',
95  'roa-tara' => 'nap-x-tara', // [[en:Tarantino_dialect]]
96  'simple' => 'en-simple',
97  'sr-ec' => 'sr-Cyrl', // T117845
98  'sr-el' => 'sr-Latn', // T117845
99 
100  // Although these next codes aren't *wrong* per se, including
101  // both the script and the country code helps compatibility with
102  // other BCP 47 users. Note that MW also uses `zh-Hans`/`zh-Hant`,
103  // without a country code, and those should be left alone.
104  // (See $variantfallbacks in LanguageZh.php for Hans/Hant id.)
105  'zh-cn' => 'zh-Hans-CN',
106  'zh-sg' => 'zh-Hans-SG',
107  'zh-my' => 'zh-Hans-MY',
108  'zh-tw' => 'zh-Hant-TW',
109  'zh-hk' => 'zh-Hant-HK',
110  'zh-mo' => 'zh-Hant-MO',
111  ];
112 
125  public static function getDeprecatedCodeMapping() {
126  return self::DEPRECATED_LANGUAGE_CODE_MAPPING;
127  }
128 
141  public static function getNonstandardLanguageCodeMapping() {
142  $result = [];
143  foreach ( self::DEPRECATED_LANGUAGE_CODE_MAPPING as $code => $ignore ) {
144  $result[$code] = self::bcp47( $code );
145  }
146  foreach ( self::NON_STANDARD_LANGUAGE_CODE_MAPPING as $code => $ignore ) {
147  $result[$code] = self::bcp47( $code );
148  }
149  return $result;
150  }
151 
162  public static function replaceDeprecatedCodes( $code ) {
163  return self::DEPRECATED_LANGUAGE_CODE_MAPPING[$code] ?? $code;
164  }
165 
176  public static function bcp47( $code ) {
177  $code = self::replaceDeprecatedCodes( strtolower( $code ) );
178  if ( isset( self::NON_STANDARD_LANGUAGE_CODE_MAPPING[$code] ) ) {
179  $code = self::NON_STANDARD_LANGUAGE_CODE_MAPPING[$code];
180  }
181  $codeSegment = explode( '-', $code );
182  $codeBCP = [];
183  foreach ( $codeSegment as $segNo => $seg ) {
184  // when previous segment is x, it is a private segment and should be lc
185  if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
186  $codeBCP[$segNo] = strtolower( $seg );
187  // ISO 3166 country code
188  } elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
189  $codeBCP[$segNo] = strtoupper( $seg );
190  // ISO 15924 script code
191  } elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
192  $codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
193  // Use lowercase for other cases
194  } else {
195  $codeBCP[$segNo] = strtolower( $seg );
196  }
197  }
198  $langCode = implode( '-', $codeBCP );
199  return $langCode;
200  }
201 }
LanguageCode\replaceDeprecatedCodes
static replaceDeprecatedCodes( $code)
Replace deprecated language codes that were used in previous versions of MediaWiki to up-to-date,...
Definition: LanguageCode.php:162
LanguageCode\getDeprecatedCodeMapping
static getDeprecatedCodeMapping()
Returns a mapping of deprecated language codes that were used in previous versions of MediaWiki to up...
Definition: LanguageCode.php:125
LanguageCode\bcp47
static bcp47( $code)
Get the normalised IETF language tag See unit test for examples.
Definition: LanguageCode.php:176
LanguageCode
Methods for dealing with language codes.
Definition: LanguageCode.php:28
LanguageCode\getNonstandardLanguageCodeMapping
static getNonstandardLanguageCodeMapping()
Returns a mapping of non-standard language codes used by (current and previous version of) MediaWiki,...
Definition: LanguageCode.php:141