Split some Language methods to LanguageNameUtils
[lhc/web/wiklou.git] / includes / language / LanguageCode.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Language
20 */
21
22 /**
23 * Methods for dealing with language codes.
24 *
25 * @since 1.29
26 * @ingroup Language
27 */
28 class LanguageCode {
29 /**
30 * Mapping of deprecated language codes that were used in previous
31 * versions of MediaWiki to up-to-date, current language codes.
32 * These may or may not be valid BCP 47 codes; they are included here
33 * because MediaWiki renamed these particular codes at some point.
34 *
35 * @var array Mapping from deprecated MediaWiki-internal language code
36 * to replacement MediaWiki-internal language code.
37 *
38 * @since 1.30
39 * @see https://meta.wikimedia.org/wiki/Special_language_codes
40 */
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
54 /**
55 * Mapping of non-standard language codes used in MediaWiki to
56 * standardized BCP 47 codes. These are not deprecated (yet?):
57 * IANA may eventually recognize the subtag, in which case the `-x-`
58 * infix could be removed, or else we could rename the code in
59 * MediaWiki, in which case they'd move up to the above mapping
60 * of deprecated codes.
61 *
62 * As a rule, we preserve all distinctions made by MediaWiki
63 * internally. For example, `de-formal` becomes `de-x-formal`
64 * instead of just `de` because MediaWiki distinguishes `de-formal`
65 * from `de` (for example, for interface translations). Similarly,
66 * BCP 47 indicates that `kk-Cyrl` SHOULD not be used because it
67 * "typically does not add information", but in our case MediaWiki
68 * LanguageConverter distinguishes `kk` (render content in a mix of
69 * Kurdish variants) from `kk-Cyrl` (convert content to be uniformly
70 * Cyrillic). As the BCP 47 requirement is a SHOULD not a MUST,
71 * `kk-Cyrl` is a valid code, although some validators may emit
72 * a warning note.
73 *
74 * @var array Mapping from nonstandard MediaWiki-internal codes to
75 * BCP 47 codes
76 *
77 * @since 1.32
78 * @see https://meta.wikimedia.org/wiki/Special_language_codes
79 * @see https://phabricator.wikimedia.org/T125073
80 */
81 private static $nonstandardLanguageCodeMapping = [
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
115 /**
116 * Returns a mapping of deprecated language codes that were used in previous
117 * versions of MediaWiki to up-to-date, current language codes.
118 *
119 * This array is merged into $wgDummyLanguageCodes in Setup.php, along with
120 * the fake language codes 'qqq' and 'qqx', which are used internally by
121 * MediaWiki's localisation system.
122 *
123 * @return string[]
124 *
125 * @since 1.29
126 */
127 public static function getDeprecatedCodeMapping() {
128 return self::$deprecatedLanguageCodeMapping;
129 }
130
131 /**
132 * Returns a mapping of non-standard language codes used by
133 * (current and previous version of) MediaWiki, mapped to standard
134 * BCP 47 names.
135 *
136 * This array is exported to JavaScript to ensure
137 * mediawiki.language.bcp47 stays in sync with LanguageCode::bcp47().
138 *
139 * @return string[]
140 *
141 * @since 1.32
142 */
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
154 /**
155 * Replace deprecated language codes that were used in previous
156 * versions of MediaWiki to up-to-date, current language codes.
157 * Other values will returned unchanged.
158 *
159 * @param string $code Old language code
160 * @return string New language code
161 *
162 * @since 1.30
163 */
164 public static function replaceDeprecatedCodes( $code ) {
165 return self::$deprecatedLanguageCodeMapping[$code] ?? $code;
166 }
167
168 /**
169 * Get the normalised IETF language tag
170 * See unit test for examples.
171 * See mediawiki.language.bcp47 for the JavaScript implementation.
172 *
173 * @param string $code The language code.
174 * @return string A language code complying with BCP 47 standards.
175 *
176 * @since 1.31
177 */
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 }