1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Language
*/
/**
* Methods for dealing with language codes.
* @todo Move some of the code-related static methods out of Language into this class
*
* @since 1.29
* @ingroup Language
*/
class LanguageCode {
/**
* Mapping of deprecated language codes that were used in previous
* versions of MediaWiki to up-to-date, current language codes.
* These may or may not be valid BCP 47 codes; they are included here
* because MediaWiki renamed these particular codes at some point.
*
* @var array Mapping from deprecated MediaWiki-internal language code
* to replacement MediaWiki-internal language code.
*
* @since 1.30
* @see https://meta.wikimedia.org/wiki/Special_language_codes
*/
private static $deprecatedLanguageCodeMapping = [
// Note that als is actually a valid ISO 639 code (Tosk Albanian), but it
// was previously used in MediaWiki for Alsatian, which comes under gsw
'als' => 'gsw', // T25215
'bat-smg' => 'sgs', // T27522
'be-x-old' => 'be-tarask', // T11823
'fiu-vro' => 'vro', // T31186
'roa-rup' => 'rup', // T17988
'zh-classical' => 'lzh', // T30443
'zh-min-nan' => 'nan', // T30442
'zh-yue' => 'yue', // T30441
];
/**
* Mapping of non-standard language codes used in MediaWiki to
* standardized BCP 47 codes. These are not deprecated (yet?):
* IANA may eventually recognize the subtag, in which case the `-x-`
* infix could be removed, or else we could rename the code in
* MediaWiki, in which case they'd move up to the above mapping
* of deprecated codes.
*
* As a rule, we preserve all distinctions made by MediaWiki
* internally. For example, `de-formal` becomes `de-x-formal`
* instead of just `de` because MediaWiki distinguishes `de-formal`
* from `de` (for example, for interface translations). Similarly,
* BCP 47 indicates that `kk-Cyrl` SHOULD not be used because it
* "typically does not add information", but in our case MediaWiki
* LanguageConverter distinguishes `kk` (render content in a mix of
* Kurdish variants) from `kk-Cyrl` (convert content to be uniformly
* Cyrillic). As the BCP 47 requirement is a SHOULD not a MUST,
* `kk-Cyrl` is a valid code, although some validators may emit
* a warning note.
*
* @var array Mapping from nonstandard MediaWiki-internal codes to
* BCP 47 codes
*
* @since 1.32
* @see https://meta.wikimedia.org/wiki/Special_language_codes
* @see https://phabricator.wikimedia.org/T125073
*/
private static $nonstandardLanguageCodeMapping = [
// All codes returned by Language::fetchLanguageNames() validated
// against IANA registry at
// https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
// with help of validator at
// http://schneegans.de/lv/
'cbk-zam' => 'cbk', // T124657
'de-formal' => 'de-x-formal',
'eml' => 'egl', // T36217
'en-rtl' => 'en-x-rtl',
'es-formal' => 'es-x-formal',
'hu-formal' => 'hu-x-formal',
'map-bms' => 'jv-x-bms', // [[en:Banyumasan_dialect]] T125073
'mo' => 'ro-Cyrl-MD', // T125073
'nrm' => 'nrf', // [[en:Norman_language]] T25216
'nl-informal' => 'nl-x-informal',
'roa-tara' => 'nap-x-tara', // [[en:Tarantino_dialect]]
'simple' => 'en-simple',
'sr-ec' => 'sr-Cyrl', // T117845
'sr-el' => 'sr-Latn', // T117845
// Although these next codes aren't *wrong* per se, including
// both the script and the country code helps compatibility with
// other BCP 47 users. Note that MW also uses `zh-Hans`/`zh-Hant`,
// without a country code, and those should be left alone.
// (See $variantfallbacks in LanguageZh.php for Hans/Hant id.)
'zh-cn' => 'zh-Hans-CN',
'zh-sg' => 'zh-Hans-SG',
'zh-my' => 'zh-Hans-MY',
'zh-tw' => 'zh-Hant-TW',
'zh-hk' => 'zh-Hant-HK',
'zh-mo' => 'zh-Hant-MO',
];
/**
* Returns a mapping of deprecated language codes that were used in previous
* versions of MediaWiki to up-to-date, current language codes.
*
* This array is merged into $wgDummyLanguageCodes in Setup.php, along with
* the fake language codes 'qqq' and 'qqx', which are used internally by
* MediaWiki's localisation system.
*
* @return string[]
*
* @since 1.29
*/
public static function getDeprecatedCodeMapping() {
return self::$deprecatedLanguageCodeMapping;
}
/**
* Returns a mapping of non-standard language codes used by
* (current and previous version of) MediaWiki, mapped to standard
* BCP 47 names.
*
* This array is exported to JavaScript to ensure
* mediawiki.language.bcp47 stays in sync with LanguageCode::bcp47().
*
* @return string[]
*
* @since 1.32
*/
public static function getNonstandardLanguageCodeMapping() {
$result = [];
foreach ( self::$deprecatedLanguageCodeMapping as $code => $ignore ) {
$result[$code] = self::bcp47( $code );
}
foreach ( self::$nonstandardLanguageCodeMapping as $code => $ignore ) {
$result[$code] = self::bcp47( $code );
}
return $result;
}
/**
* Replace deprecated language codes that were used in previous
* versions of MediaWiki to up-to-date, current language codes.
* Other values will returned unchanged.
*
* @param string $code Old language code
* @return string New language code
*
* @since 1.30
*/
public static function replaceDeprecatedCodes( $code ) {
return self::$deprecatedLanguageCodeMapping[$code] ?? $code;
}
/**
* Get the normalised IETF language tag
* See unit test for examples.
* See mediawiki.language.bcp47 for the JavaScript implementation.
*
* @param string $code The language code.
* @return string A language code complying with BCP 47 standards.
*
* @since 1.31
*/
public static function bcp47( $code ) {
$code = self::replaceDeprecatedCodes( strtolower( $code ) );
if ( isset( self::$nonstandardLanguageCodeMapping[$code] ) ) {
$code = self::$nonstandardLanguageCodeMapping[$code];
}
$codeSegment = explode( '-', $code );
$codeBCP = [];
foreach ( $codeSegment as $segNo => $seg ) {
// when previous segment is x, it is a private segment and should be lc
if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
$codeBCP[$segNo] = strtolower( $seg );
// ISO 3166 country code
} elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
$codeBCP[$segNo] = strtoupper( $seg );
// ISO 15924 script code
} elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
$codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
// Use lowercase for other cases
} else {
$codeBCP[$segNo] = strtolower( $seg );
}
}
$langCode = implode( '-', $codeBCP );
return $langCode;
}
}
|