MediaWiki REL1_37
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 Language::fetchLanguageNames() 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}
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.