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