Merge "[Korean] Add 2 additional bookstoreList"
[lhc/web/wiklou.git] / languages / 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 remapped these particular codes at some point.
35 *
36 * @var array Mapping from language code to 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 codes to BCP 47 codes
75 *
76 * @since 1.32
77 * @see https://meta.wikimedia.org/wiki/Special_language_codes
78 * @see https://phabricator.wikimedia.org/T125073
79 */
80 private static $nonstandardLanguageCodeMapping = [
81 // All codes returned by Language::fetchLanguageNames() validated
82 // against IANA registry at
83 // https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
84 // with help of validator at
85 // http://schneegans.de/lv/
86 'cbk-zam' => 'cbk', // T124657
87 'de-formal' => 'de-x-formal',
88 'eml' => 'egl', // T36217
89 'en-rtl' => 'en-x-rtl',
90 'es-formal' => 'es-x-formal',
91 'hu-formal' => 'hu-x-formal',
92 'map-bms' => 'jv-x-bms', // [[en:Banyumasan_dialect]] T125073
93 'mo' => 'ro-Cyrl-MD', // T125073
94 'nrm' => 'nrf', // [[en:Norman_language]] T25216
95 'nl-informal' => 'nl-x-informal',
96 'roa-tara' => 'nap-x-tara', // [[en:Tarantino_dialect]]
97 'simple' => 'en-simple',
98 'sr-ec' => 'sr-Cyrl', // T117845
99 'sr-el' => 'sr-Latn', // T117845
100
101 // Although these next codes aren't *wrong* per se, including
102 // both the script and the country code helps compatibility with
103 // other BCP 47 users. Note that MW also uses `zh-Hans`/`zh-Hant`,
104 // without a country code, and those should be left alone.
105 // (See $variantfallbacks in LanguageZh.php for Hans/Hant id.)
106 'zh-cn' => 'zh-Hans-CN',
107 'zh-sg' => 'zh-Hans-SG',
108 'zh-my' => 'zh-Hans-MY',
109 'zh-tw' => 'zh-Hant-TW',
110 'zh-hk' => 'zh-Hant-HK',
111 'zh-mo' => 'zh-Hant-MO',
112 ];
113
114 /**
115 * Returns a mapping of deprecated language codes that were used in previous
116 * versions of MediaWiki to up-to-date, current language codes.
117 *
118 * This array is merged into $wgDummyLanguageCodes in Setup.php, along with
119 * the fake language codes 'qqq' and 'qqx', which are used internally by
120 * MediaWiki's localisation system.
121 *
122 * @return string[]
123 *
124 * @since 1.29
125 */
126 public static function getDeprecatedCodeMapping() {
127 return self::$deprecatedLanguageCodeMapping;
128 }
129
130 /**
131 * Returns a mapping of non-standard language codes used by
132 * (current and previous version of) MediaWiki, mapped to standard
133 * BCP 47 names.
134 *
135 * This array is exported to JavaScript to ensure
136 * mediawiki.language.bcp47 stays in sync with LanguageCode::bcp47().
137 *
138 * @return string[]
139 *
140 * @since 1.32
141 */
142 public static function getNonstandardLanguageCodeMapping() {
143 $result = [];
144 foreach ( self::$deprecatedLanguageCodeMapping as $code => $ignore ) {
145 $result[$code] = self::bcp47( $code );
146 }
147 foreach ( self::$nonstandardLanguageCodeMapping as $code => $ignore ) {
148 $result[$code] = self::bcp47( $code );
149 }
150 return $result;
151 }
152
153 /**
154 * Replace deprecated language codes that were used in previous
155 * versions of MediaWiki to up-to-date, current language codes.
156 * Other values will returned unchanged.
157 *
158 * @param string $code Old language code
159 * @return string New language code
160 *
161 * @since 1.30
162 */
163 public static function replaceDeprecatedCodes( $code ) {
164 if ( isset( self::$deprecatedLanguageCodeMapping[$code] ) ) {
165 return self::$deprecatedLanguageCodeMapping[$code];
166 }
167 return $code;
168 }
169
170 /**
171 * Get the normalised IETF language tag
172 * See unit test for examples.
173 * See mediawiki.language.bcp47 for the JavaScript implementation.
174 *
175 * @param string $code The language code.
176 * @return string A language code complying with BCP 47 standards.
177 *
178 * @since 1.31
179 */
180 public static function bcp47( $code ) {
181 $code = self::replaceDeprecatedCodes( strtolower( $code ) );
182 if ( isset( self::$nonstandardLanguageCodeMapping[$code] ) ) {
183 $code = self::$nonstandardLanguageCodeMapping[$code];
184 }
185 $codeSegment = explode( '-', $code );
186 $codeBCP = [];
187 foreach ( $codeSegment as $segNo => $seg ) {
188 // when previous segment is x, it is a private segment and should be lc
189 if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
190 $codeBCP[$segNo] = strtolower( $seg );
191 // ISO 3166 country code
192 } elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
193 $codeBCP[$segNo] = strtoupper( $seg );
194 // ISO 15924 script code
195 } elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
196 $codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
197 // Use lowercase for other cases
198 } else {
199 $codeBCP[$segNo] = strtolower( $seg );
200 }
201 }
202 $langCode = implode( '-', $codeBCP );
203 return $langCode;
204 }
205 }