MediaWiki  1.34.0
LanguageCode.php
Go to the documentation of this file.
1 <?php
28 class LanguageCode {
41  private static $deprecatedLanguageCodeMapping = [
42  // Note that als is actually a valid ISO 639 code (Tosk Albanian), but it
43  // was previously used in MediaWiki for Alsatian, which comes under gsw
44  'als' => 'gsw', // T25215
45  'bat-smg' => 'sgs', // T27522
46  'be-x-old' => 'be-tarask', // T11823
47  'fiu-vro' => 'vro', // T31186
48  'roa-rup' => 'rup', // T17988
49  'zh-classical' => 'lzh', // T30443
50  'zh-min-nan' => 'nan', // T30442
51  'zh-yue' => 'yue', // T30441
52  ];
53 
82  // All codes returned by Language::fetchLanguageNames() 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 `zh-Hans`/`zh-Hant`,
105  // without a country code, and those should be left alone.
106  // (See $variantfallbacks in LanguageZh.php for Hans/Hant id.)
107  'zh-cn' => 'zh-Hans-CN',
108  'zh-sg' => 'zh-Hans-SG',
109  'zh-my' => 'zh-Hans-MY',
110  'zh-tw' => 'zh-Hant-TW',
111  'zh-hk' => 'zh-Hant-HK',
112  'zh-mo' => 'zh-Hant-MO',
113  ];
114 
127  public static function getDeprecatedCodeMapping() {
129  }
130 
143  public static function getNonstandardLanguageCodeMapping() {
144  $result = [];
145  foreach ( self::$deprecatedLanguageCodeMapping as $code => $ignore ) {
146  $result[$code] = self::bcp47( $code );
147  }
148  foreach ( self::$nonstandardLanguageCodeMapping as $code => $ignore ) {
149  $result[$code] = self::bcp47( $code );
150  }
151  return $result;
152  }
153 
164  public static function replaceDeprecatedCodes( $code ) {
165  return self::$deprecatedLanguageCodeMapping[$code] ?? $code;
166  }
167 
178  public static function bcp47( $code ) {
179  $code = self::replaceDeprecatedCodes( strtolower( $code ) );
180  if ( isset( self::$nonstandardLanguageCodeMapping[$code] ) ) {
181  $code = self::$nonstandardLanguageCodeMapping[$code];
182  }
183  $codeSegment = explode( '-', $code );
184  $codeBCP = [];
185  foreach ( $codeSegment as $segNo => $seg ) {
186  // when previous segment is x, it is a private segment and should be lc
187  if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
188  $codeBCP[$segNo] = strtolower( $seg );
189  // ISO 3166 country code
190  } elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
191  $codeBCP[$segNo] = strtoupper( $seg );
192  // ISO 15924 script code
193  } elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
194  $codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
195  // Use lowercase for other cases
196  } else {
197  $codeBCP[$segNo] = strtolower( $seg );
198  }
199  }
200  $langCode = implode( '-', $codeBCP );
201  return $langCode;
202  }
203 }
LanguageCode\replaceDeprecatedCodes
static replaceDeprecatedCodes( $code)
Replace deprecated language codes that were used in previous versions of MediaWiki to up-to-date,...
Definition: LanguageCode.php:164
LanguageCode\getDeprecatedCodeMapping
static getDeprecatedCodeMapping()
Returns a mapping of deprecated language codes that were used in previous versions of MediaWiki to up...
Definition: LanguageCode.php:127
LanguageCode\$nonstandardLanguageCodeMapping
static array $nonstandardLanguageCodeMapping
Mapping of non-standard language codes used in MediaWiki to standardized BCP 47 codes.
Definition: LanguageCode.php:81
LanguageCode\$deprecatedLanguageCodeMapping
static array $deprecatedLanguageCodeMapping
Mapping of deprecated language codes that were used in previous versions of MediaWiki to up-to-date,...
Definition: LanguageCode.php:41
LanguageCode\bcp47
static bcp47( $code)
Get the normalised IETF language tag See unit test for examples.
Definition: LanguageCode.php:178
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:143