/
Locales.php
403 lines (385 loc) · 15.8 KB
/
Locales.php
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Localization;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Defines all available TYPO3 system languages, as they differ from actual ISO 639-1 codes.
* User-defined system languages can be added to $GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user']
*
* These system languages are used for determining the proper language labels of XLF files.
*
* When adding new keys, remember to:
* - Update 'setup' extension labels (sysext/setup/Resources/Private/Language/locallang.xlf)
* That's it!
*/
class Locales implements SingletonInterface
{
/**
* Supported TYPO3 languages with locales
*
* @var array<non-empty-string, non-empty-string>
*/
protected array $languages = [
'default' => 'English',
'af' => 'Afrikaans',
'ar' => 'Arabic',
'bs' => 'Bosnian',
'bg' => 'Bulgarian',
'ca' => 'Catalan',
'ch' => 'Chinese (Simple)',
'cs' => 'Czech',
'cy' => 'Welsh',
'da' => 'Danish',
'de' => 'German',
'el' => 'Greek',
'eo' => 'Esperanto',
'es' => 'Spanish',
'et' => 'Estonian',
'eu' => 'Basque',
'fa' => 'Persian',
'fi' => 'Finnish',
'fo' => 'Faroese',
'fr' => 'French',
'fr_CA' => 'French (Canada)',
'gl' => 'Galician',
'he' => 'Hebrew',
'hi' => 'Hindi',
'hr' => 'Croatian',
'hu' => 'Hungarian',
'is' => 'Icelandic',
'it' => 'Italian',
'ja' => 'Japanese',
'ka' => 'Georgian',
'kl' => 'Greenlandic',
'km' => 'Khmer',
'ko' => 'Korean',
'lb' => 'Luxembourgish',
'lt' => 'Lithuanian',
'lv' => 'Latvian',
'mi' => 'Maori',
'mk' => 'Macedonian',
'ms' => 'Malay',
'nl' => 'Dutch',
'no' => 'Norwegian',
'pl' => 'Polish',
'pt' => 'Portuguese',
'pt_BR' => 'Brazilian Portuguese',
'ro' => 'Romanian',
'ru' => 'Russian',
'rw' => 'Kinyarwanda',
'sk' => 'Slovak',
'sl' => 'Slovenian',
'sn' => 'Shona (Bantu)',
'sq' => 'Albanian',
'sr' => 'Serbian',
'sv' => 'Swedish',
'th' => 'Thai',
'tr' => 'Turkish',
'uk' => 'Ukrainian',
'vi' => 'Vietnamese',
'zh' => 'Chinese (Traditional)',
'zh_CN' => 'Chinese (Simplified)',
'zh_HK' => 'Chinese (Simplified Hong Kong)',
'zh_Hans_CN' => 'Chinese (Simplified Han)',
];
/**
* Reversed mapping for backward compatibility codes
*
* Key => real ISO code
* value => the value that TYPO3 understands (which is wrong, obviously)
*
* Example:
* "da" => official ISO 639-1 code
* "dk" (wrong)" => the shortcut that TYPO3 uses for danish within the system for labels.
*
* @var array<non-empty-string, non-empty-string>
* @deprecated will be removed in TYPO3 v13.0. backwards-compatibility is not needed anymore.
*/
protected array $isoReverseMapping = [
'bs' => 'ba', // Bosnian
'cs' => 'cz', // Czech
'da' => 'dk', // Danish
'el' => 'gr', // Greek
'fr_CA' => 'qc', // French (Canada)
'gl' => 'ga', // Galician
'ja' => 'jp', // Japanese
'ka' => 'ge', // Georgian
'kl' => 'gl', // Greenlandic
'ko' => 'kr', // Korean
'ms' => 'my', // Malay
'pt_BR' => 'br', // Portuguese (Brazil)
'sl' => 'si', // Slovenian
'sv' => 'se', // Swedish
'uk' => 'ua', // Ukrainian
'vi' => 'vn', // Vietnamese
'zh' => 'hk', // Chinese (China)
'zh_CN' => 'ch', // Chinese (Simplified)
'zh_HK' => 'hk', // Chinese (Simplified Hong Kong)
'zh_Hans_CN' => 'ch', // Chinese (Simplified Han)
];
/**
* Dependencies for locales.
* By default, locales with a country/region suffix such as "de_AT" will automatically have the "de"
* locale as fallback. This way TYPO3 only needs to know about the actual "base" language, however
* also allows to use country-specific languages.
* However, when a specific locale such as "lb" has a dependency to a different "de" suffix, this should
* is defined here.
* With
* $GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']
* it is possible to extend the dependency list.
*
* Example:
* If "lb" is chosen, but no label was found, a fallback to the label in "de" is used.
*/
protected array $localeDependencies = [
'lb' => ['de'],
];
public function __construct()
{
// Allow user-defined locales
foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] ?? [] as $locale => $name) {
if (!is_string($locale) || $locale === '') {
continue;
}
if (!isset($this->languages[$locale])) {
$this->languages[$locale] = $name;
}
}
// Merge user-provided locale dependencies
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'] ?? null)) {
$this->localeDependencies = array_replace_recursive(
$this->localeDependencies,
$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']
);
}
}
public function createLocale(string $localeKey): Locale
{
if (strpos($localeKey, '.')) {
[$sanitizedLocaleKey] = explode('.', $localeKey);
}
// Find the requested language in this list based on the $languageKey
// Language is found. Configure it:
if ($localeKey === 'en' || $this->isValidLanguageKey($sanitizedLocaleKey ?? $localeKey)) {
return new Locale($localeKey, $this->getLocaleDependencies($sanitizedLocaleKey ?? $localeKey));
}
return new Locale();
}
/**
* Returns the locales.
* @return array<int, non-empty-string>
*/
public function getLocales(): array
{
return array_keys($this->languages);
}
public function isValidLanguageKey(string $locale): bool
{
// "en" implicitly equals "default", so this is OK
if ($locale === 'en' || $locale === 'default') {
return true;
}
if (!isset($this->languages[$locale])) {
// the given locale is not found in the current locales, let us see if
// the base language (iso-639-1) is in the list of supported locales.
if (str_contains($locale, '_')) {
[$baseIsoCodeLanguageKey] = explode('_', $locale);
return $this->isValidLanguageKey($baseIsoCodeLanguageKey);
}
if (str_contains($locale, '-')) {
[$baseIsoCodeLanguageKey] = explode('-', $locale);
return $this->isValidLanguageKey($baseIsoCodeLanguageKey);
}
return false;
}
return true;
}
/**
* Returns the supported languages indexed by their corresponding locale.
* @return array<non-empty-string, non-empty-string>
*/
public function getLanguages(): array
{
return $this->languages;
}
/**
* Returns a list of all ISO codes / TYPO3 languages that have active language packs, but also includes "default".
* @return array<int, non-empty-string>
*/
public function getActiveLanguages(): array
{
return array_merge(
['default'],
array_filter(array_values($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'] ?? []))
);
}
/**
* Returns the mapping between TYPO3 (old) language codes and ISO codes.
*
* @return array<non-empty-string, non-empty-string>
* @deprecated will be removed in TYPO3 v13.0.
*/
public function getIsoMapping(): array
{
trigger_error('Locales->getIsoMapping() will be removed in TYPO3 v13.0. Migrate to real locales instead.', E_USER_DEPRECATED);
return array_flip($this->isoReverseMapping);
}
/**
* Returns the dependencies of a given locale, if any.
*
* @return array<int, non-empty-string>
*/
public function getLocaleDependencies(string $locale): array
{
$dependencies = [];
if (isset($this->localeDependencies[$locale])) {
$dependencies = $this->localeDependencies[$locale];
// Search for dependencies recursively
$localeDependencies = $dependencies;
foreach ($localeDependencies as $dependency) {
if (isset($this->localeDependencies[$dependency])) {
$dependencies = array_merge($dependencies, $this->getLocaleDependencies($dependency));
}
}
}
// Use automatic dependency resolving.
// "de_AT" automatically has a dependency on "de".
// but only do this if the actual "de_AT" does not have a custom dependency already defined in
// $this->localeDependencies
if ($dependencies === [] && str_contains($locale, '_')) {
[$languageIsoCode] = explode('_', $locale);
// "en" = "default" is always implicitly the default fallback dependency
if ($languageIsoCode !== 'en') {
$dependencies[] = $languageIsoCode;
$dependencies = array_merge($dependencies, $this->getLocaleDependencies($languageIsoCode));
}
} elseif ($dependencies === [] && str_contains($locale, '-')) {
[$languageIsoCode] = explode('-', $locale);
// "en" = "default" is always implicitly the default fallback dependency
if ($languageIsoCode !== 'en') {
$dependencies[] = $languageIsoCode;
$dependencies = array_merge($dependencies, $this->getLocaleDependencies($languageIsoCode));
}
}
return array_unique($dependencies);
}
/**
* Converts the language codes that we get from the client (usually HTTP_ACCEPT_LANGUAGE)
* into a TYPO3-readable language code
*
* @param string $languageCodesList List of language codes. something like 'de,en-us;q=0.9,de-de;q=0.7,es-cl;q=0.6,en;q=0.4,es;q=0.3,zh;q=0.1'
* @return non-empty-string A preferred language that TYPO3 supports, or "default" if none found
*/
public function getPreferredClientLanguage(string $languageCodesList): string
{
$allLanguageCodesFromLocales = ['en' => 'default'];
foreach ($this->languages as $locale => $localeTitle) {
$locale = str_replace('_', '-', $locale);
$allLanguageCodesFromLocales[$locale] = $locale;
}
$selectedLanguage = 'default';
$preferredLanguages = GeneralUtility::trimExplode(',', $languageCodesList);
// Order the preferred languages after they key
$sortedPreferredLanguages = [];
foreach ($preferredLanguages as $preferredLanguage) {
$quality = 1.0;
if (str_contains($preferredLanguage, ';q=')) {
[$preferredLanguage, $quality] = explode(';q=', $preferredLanguage);
}
$sortedPreferredLanguages[$preferredLanguage] = $quality;
}
// Loop through the languages, with the highest priority first
arsort($sortedPreferredLanguages, SORT_NUMERIC);
foreach ($sortedPreferredLanguages as $preferredLanguage => $quality) {
if (isset($allLanguageCodesFromLocales[$preferredLanguage])) {
$selectedLanguage = $allLanguageCodesFromLocales[$preferredLanguage];
break;
}
// Strip the country code from the end
[$preferredLanguage] = explode('-', $preferredLanguage);
if (isset($allLanguageCodesFromLocales[$preferredLanguage])) {
$selectedLanguage = $allLanguageCodesFromLocales[$preferredLanguage];
break;
}
}
if (!$selectedLanguage || $selectedLanguage === 'en') {
$selectedLanguage = 'default';
}
return str_replace('-', '_', $selectedLanguage);
}
/**
* Setting locale based on a SiteLanguage's defined locale.
* Used for frontend rendering, previously set within TSFE->settingLocale
*
* @return bool whether the locale was found on the system (and could be set properly) or not
*/
public static function setSystemLocaleFromSiteLanguage(SiteLanguage $siteLanguage): bool
{
$locale = $siteLanguage->getLocale();
// No locale was given, so return false
if (!$locale) {
return false;
}
return self::setLocale($locale, $locale);
}
/**
* Internal method, which calls itself again, in order to avoid multiple logging issues.
* The main reason for this method is that it calls itself again by trying again to set
* the locale. Due to sensible defaults, people used the locale "de_AT.utf-8" with the POSIX platform
* (see https://en.wikipedia.org/wiki/Locale_(computer_software)#POSIX_platforms) in their site configuration,
* even though the target system has "de_AT" and not "de_AT.UTF-8" defined.
* "setLocale()" is now called again without the POSIX platform suffix and is checked again if the locale
* is then available, and then logs the failed information.
*/
protected static function setLocale(string $locale, string $localeStringForTrigger): bool
{
$incomingLocale = $locale;
$availableLocales = GeneralUtility::trimExplode(',', $locale, true);
// If LC_NUMERIC is set e.g. to 'de_DE' PHP parses float values locale-aware resulting in strings with comma
// as decimal point which causes problems with value conversions - so we set all locale types except LC_NUMERIC
// @see https://bugs.php.net/bug.php?id=53711
$locale = setlocale(LC_COLLATE, ...$availableLocales);
if ($locale) {
// As str_* methods are locale aware and turkish has no upper case I
// Class autoloading and other checks depending on case changing break with turkish locale LC_CTYPE
// @see http://bugs.php.net/bug.php?id=35050
if (!str_starts_with($locale, 'tr')) {
setlocale(LC_CTYPE, ...$availableLocales);
}
setlocale(LC_MONETARY, ...$availableLocales);
setlocale(LC_TIME, ...$availableLocales);
} else {
// Retry again without the "utf-8" POSIX platform suffix if this is given.
if (str_contains($incomingLocale, '.')) {
[$localeWithoutModifier] = explode('.', $incomingLocale);
return self::setLocale($localeWithoutModifier, $incomingLocale);
}
if ($localeStringForTrigger === $locale) {
GeneralUtility::makeInstance(LogManager::class)
->getLogger(__CLASS__)
->error('Locale "' . htmlspecialchars($localeStringForTrigger) . '" not found.');
} else {
GeneralUtility::makeInstance(LogManager::class)
->getLogger(__CLASS__)
->error('Locale "' . htmlspecialchars($localeStringForTrigger) . '" and "' . htmlspecialchars($incomingLocale) . '" not found.');
}
return false;
}
return true;
}
}