‪TYPO3CMS  ‪main
Locales.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
28 
36 {
42  protected array ‪$languages = [
43  'default' => 'English',
44  'af' => 'Afrikaans',
45  'ar' => 'Arabic',
46  'bs' => 'Bosnian',
47  'bg' => 'Bulgarian',
48  'ca' => 'Catalan',
49  'ch' => 'Chinese (Simple)',
50  'cs' => 'Czech',
51  'cy' => 'Welsh',
52  'da' => 'Danish',
53  'de' => 'German',
54  'el' => 'Greek',
55  'eo' => 'Esperanto',
56  'es' => 'Spanish',
57  'et' => 'Estonian',
58  'eu' => 'Basque',
59  'fa' => 'Persian',
60  'fi' => 'Finnish',
61  'fo' => 'Faroese',
62  'fr' => 'French',
63  'fr_CA' => 'French (Canada)',
64  'gl' => 'Galician',
65  'he' => 'Hebrew',
66  'hi' => 'Hindi',
67  'hr' => 'Croatian',
68  'hu' => 'Hungarian',
69  'is' => 'Icelandic',
70  'it' => 'Italian',
71  'ja' => 'Japanese',
72  'ka' => 'Georgian',
73  'kl' => 'Greenlandic',
74  'km' => 'Khmer',
75  'ko' => 'Korean',
76  'lb' => 'Luxembourgish',
77  'lt' => 'Lithuanian',
78  'lv' => 'Latvian',
79  'mi' => 'Maori',
80  'mk' => 'Macedonian',
81  'ms' => 'Malay',
82  'nl' => 'Dutch',
83  'no' => 'Norwegian',
84  'pl' => 'Polish',
85  'pt' => 'Portuguese',
86  'pt_BR' => 'Brazilian Portuguese',
87  'ro' => 'Romanian',
88  'ru' => 'Russian',
89  'rw' => 'Kinyarwanda',
90  'sk' => 'Slovak',
91  'sl' => 'Slovenian',
92  'sn' => 'Shona (Bantu)',
93  'sq' => 'Albanian',
94  'sr' => 'Serbian',
95  'sv' => 'Swedish',
96  'th' => 'Thai',
97  'tr' => 'Turkish',
98  'uk' => 'Ukrainian',
99  'vi' => 'Vietnamese',
100  'zh' => 'Chinese (Traditional)',
101  'zh_CN' => 'Chinese (Simplified)',
102  'zh_HK' => 'Chinese (Simplified Hong Kong)',
103  'zh_Hans_CN' => 'Chinese (Simplified Han)',
104  ];
105 
120  protected array ‪$localeDependencies = [
121  'lb' => ['de'],
122  ];
123 
124  public function ‪__construct()
125  {
126  // Allow user-defined locales
127  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] ?? [] as $locale => $name) {
128  if (!is_string($locale) || $locale === '') {
129  continue;
130  }
131  if (!isset($this->‪languages[$locale])) {
132  $this->‪languages[$locale] = $name;
133  }
134  }
135  // Merge user-provided locale dependencies
136  if (is_array(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'] ?? null)) {
137  $this->localeDependencies = array_replace_recursive(
138  $this->localeDependencies,
139  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']
140  );
141  }
142  }
143 
144  public function ‪createLocale(string $localeKey, array $alternativeDependencies = null): ‪Locale
145  {
146  if (strpos($localeKey, '.')) {
147  [$sanitizedLocaleKey] = explode('.', $localeKey);
148  }
149  // Find the requested language in this list based on the $languageKey
150  // Language is found. Configure it:
151  if ($localeKey === 'en' || $this->‪isValidLanguageKey($sanitizedLocaleKey ?? $localeKey)) {
152  return new ‪Locale($localeKey, $alternativeDependencies ?? $this->getLocaleDependencies($sanitizedLocaleKey ?? $localeKey));
153  }
154  return new ‪Locale();
155  }
156 
161  public function getLocales(): array
162  {
163  return array_keys($this->‪languages);
164  }
165 
166  public function ‪isValidLanguageKey(string $locale): bool
167  {
168  // "en" implicitly equals "default", so this is OK
169  if ($locale === 'en' || $locale === 'default') {
170  return true;
171  }
172  if (!isset($this->‪languages[$locale])) {
173  // the given locale is not found in the current locales, let us see if
174  // the base language (iso-639-1) is in the list of supported locales.
175  if (str_contains($locale, '_')) {
176  [$baseIsoCodeLanguageKey] = explode('_', $locale);
177  return $this->‪isValidLanguageKey($baseIsoCodeLanguageKey);
178  }
179  if (str_contains($locale, '-')) {
180  [$baseIsoCodeLanguageKey] = explode('-', $locale);
181  return $this->‪isValidLanguageKey($baseIsoCodeLanguageKey);
182  }
183  return false;
184  }
185  return true;
186  }
187 
192  public function getLanguages(): array
193  {
195  }
196 
201  public function getActiveLanguages(): array
202  {
203  return array_merge(
204  ['default'],
205  array_filter(array_values(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'] ?? []))
206  );
207  }
208 
209  public function isLanguageKeyAvailable(string $languageKey): bool
210  {
211  return in_array($languageKey, $this->getActiveLanguages()) || is_dir(‪Environment::getLabelsPath() . '/' . $languageKey);
212  }
213 
219  public function getLocaleDependencies(string $locale): array
220  {
221  $dependencies = [];
222  if (isset($this->localeDependencies[$locale])) {
223  $dependencies = $this->localeDependencies[$locale];
224  // Search for dependencies recursively
225  ‪$localeDependencies = $dependencies;
226  foreach (‪$localeDependencies as $dependency) {
227  if (isset($this->localeDependencies[$dependency])) {
228  $dependencies = array_merge($dependencies, $this->getLocaleDependencies($dependency));
229  }
230  }
231  }
232  // Use automatic dependency resolving.
233  // "de_AT" automatically has a dependency on "de".
234  // but only do this if the actual "de_AT" does not have a custom dependency already defined in
235  // $this->localeDependencies
236  if ($dependencies === [] && str_contains($locale, '_')) {
237  [$languageIsoCode] = explode('_', $locale);
238  // "en" = "default" is always implicitly the default fallback dependency
239  if ($languageIsoCode !== 'en') {
240  $dependencies[] = $languageIsoCode;
241  $dependencies = array_merge($dependencies, $this->getLocaleDependencies($languageIsoCode));
242  }
243  } elseif ($dependencies === [] && str_contains($locale, '-')) {
244  [$languageIsoCode] = explode('-', $locale);
245  // "en" = "default" is always implicitly the default fallback dependency
246  if ($languageIsoCode !== 'en') {
247  $dependencies[] = $languageIsoCode;
248  $dependencies = array_merge($dependencies, $this->getLocaleDependencies($languageIsoCode));
249  }
250  }
251  return array_unique($dependencies);
252  }
253 
261  public function getPreferredClientLanguage(string $languageCodesList): string
262  {
263  $allLanguageCodesFromLocales = ['en' => 'default'];
264  foreach ($this->‪languages as $locale => $localeTitle) {
265  $locale = str_replace('_', '-', $locale);
266  $allLanguageCodesFromLocales[$locale] = $locale;
267  }
268  ‪$selectedLanguage = 'default';
270  // Order the preferred languages after they key
272  foreach (‪$preferredLanguages as $preferredLanguage) {
273  $quality = 1.0;
274  if (str_contains($preferredLanguage, ';q=')) {
275  [$preferredLanguage, $quality] = explode(';q=', $preferredLanguage);
276  }
277  ‪$sortedPreferredLanguages[$preferredLanguage] = $quality;
278  }
279  // Loop through the languages, with the highest priority first
280  arsort(‪$sortedPreferredLanguages, SORT_NUMERIC);
281  foreach (‪$sortedPreferredLanguages as $preferredLanguage => $quality) {
282  if (isset($allLanguageCodesFromLocales[$preferredLanguage])) {
283  ‪$selectedLanguage = $allLanguageCodesFromLocales[$preferredLanguage];
284  break;
285  }
286  // Strip the country code from the end
287  [$preferredLanguage] = explode('-', $preferredLanguage);
288  if (isset($allLanguageCodesFromLocales[$preferredLanguage])) {
289  ‪$selectedLanguage = $allLanguageCodesFromLocales[$preferredLanguage];
290  break;
291  }
292  }
293  if (!‪$selectedLanguage || ‪$selectedLanguage === 'en') {
294  ‪$selectedLanguage = 'default';
295  }
296  return str_replace('-', '_', ‪$selectedLanguage);
297  }
298 
305  public static function ‪setSystemLocaleFromSiteLanguage(‪SiteLanguage $siteLanguage): bool
306  {
307  $locale = $siteLanguage->‪getLocale()->posixFormatted();
308  if ($locale === '') {
309  return false;
310  }
311  return ‪self::setLocale($locale, $locale);
312  }
313 
323  protected static function ‪setLocale(string $locale, string $localeStringForTrigger): bool
324  {
325  $incomingLocale = $locale;
326  $availableLocales = ‪GeneralUtility::trimExplode(',', $locale, true);
327  // If LC_NUMERIC is set e.g. to 'de_DE' PHP parses float values locale-aware resulting in strings with comma
328  // as decimal point which causes problems with value conversions - so we set all locale types except LC_NUMERIC
329  // @see https://bugs.php.net/bug.php?id=53711
330  $locale = setlocale(LC_COLLATE, ...$availableLocales);
331  if ($locale) {
332  // As str_* methods are locale aware and turkish has no upper case I
333  // Class autoloading and other checks depending on case changing break with turkish locale LC_CTYPE
334  // @see http://bugs.php.net/bug.php?id=35050
335  if (!str_starts_with($locale, 'tr')) {
336  setlocale(LC_CTYPE, ...$availableLocales);
337  }
338  setlocale(LC_MONETARY, ...$availableLocales);
339  setlocale(LC_TIME, ...$availableLocales);
340  } else {
341  // Retry again without the "utf-8" POSIX platform suffix if this is given.
342  if (str_contains($incomingLocale, '.')) {
343  [$localeWithoutModifier] = explode('.', $incomingLocale);
344  return ‪self::setLocale($localeWithoutModifier, $incomingLocale);
345  }
346  if ($localeStringForTrigger === $locale) {
347  GeneralUtility::makeInstance(LogManager::class)
348  ->getLogger(__CLASS__)
349  ->error('Locale "' . htmlspecialchars($localeStringForTrigger) . '" not found.');
350  } else {
351  GeneralUtility::makeInstance(LogManager::class)
352  ->getLogger(__CLASS__)
353  ->error('Locale "' . htmlspecialchars($localeStringForTrigger) . '" and "' . htmlspecialchars($incomingLocale) . '" not found.');
354  }
355  return false;
356  }
357  return true;
358  }
359 
360  public function ‪createLocaleFromRequest(?ServerRequestInterface $request): ‪Locale
361  {
362  $languageServiceFactory = GeneralUtility::makeInstance(LanguageServiceFactory::class);
363  if ($request !== null && ‪ApplicationType::fromRequest($request)->isFrontend()) {
364  // @todo: the string conversion is needed for the time being, as long as SiteLanguage does not contain
365  // the full locale with all fallbacks, then getTypo3Language() also needs to be removed.
366  $localeString = (string)($request->getAttribute('language')?->getTypo3Language()
367  ?? $request->getAttribute('site')->getDefaultLanguage()->getTypo3Language());
368  return $this->‪createLocale($localeString);
369  }
370  return $languageServiceFactory->createFromUserPreferences(‪$GLOBALS['BE_USER'] ?? null)->getLocale();
371  }
372 
374  {
375  if ($user && ($user->user['lang'] ?? false)) {
376  return $this->‪createLocale($user->user['lang']);
377  }
378  return $this->‪createLocale('en');
379  }
380 }
‪TYPO3\CMS\Core\Localization\Locales\$sortedPreferredLanguages
‪$sortedPreferredLanguages
Definition: Locales.php:271
‪TYPO3\CMS\Core\Localization\Locales\setSystemLocaleFromSiteLanguage
‪static bool setSystemLocaleFromSiteLanguage(SiteLanguage $siteLanguage)
Definition: Locales.php:305
‪TYPO3\CMS\Core\Localization\Locales\$preferredLanguages
‪$preferredLanguages
Definition: Locales.php:269
‪TYPO3\CMS\Core\Localization\Locales\setLocale
‪static setLocale(string $locale, string $localeStringForTrigger)
Definition: Locales.php:323
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\getLocale
‪getLocale()
Definition: SiteLanguage.php:174
‪TYPO3\CMS\Core\Core\Environment\getLabelsPath
‪static getLabelsPath()
Definition: Environment.php:233
‪TYPO3\CMS\Core\Localization\Locales\$localeDependencies
‪array $localeDependencies
Definition: Locales.php:120
‪TYPO3\CMS\Core\Localization\Locales
Definition: Locales.php:36
‪TYPO3\CMS\Core\Localization\Locales\__construct
‪__construct()
Definition: Locales.php:124
‪TYPO3\CMS\Core\Localization
Definition: CacheWarmer.php:18
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Core\Localization\Locales\$selectedLanguage
‪foreach($this->languages as $locale=> $localeTitle) $selectedLanguage
Definition: Locales.php:268
‪TYPO3\CMS\Core\Localization\Locales\createLocaleFromRequest
‪createLocaleFromRequest(?ServerRequestInterface $request)
Definition: Locales.php:360
‪TYPO3\CMS\Core\Localization\Locales\createLocale
‪createLocale(string $localeKey, array $alternativeDependencies=null)
Definition: Locales.php:144
‪TYPO3\CMS\Core\Localization\Locales\languages
‪array< non-empty-string, function getLanguages():array { return $this-> languages
Definition: Locales.php:194
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Localization\Locale
Definition: Locale.php:30
‪TYPO3\CMS\Core\Localization\Locales\isValidLanguageKey
‪array< int, getLocales():array { return array_keys( $this->languages);} public function isValidLanguageKey(string $locale):bool { if( $locale==='en'||$locale==='default') { return true;} if(!isset( $this->languages[ $locale])) { if(str_contains( $locale, '_')) {[ $baseIsoCodeLanguageKey]=explode( '_', $locale);return $this-> isValidLanguageKey($baseIsoCodeLanguageKey)
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:67
‪TYPO3\CMS\Core\Localization\Locales\createLocaleFromUserPreferences
‪createLocaleFromUserPreferences(?AbstractUserAuthentication $user)
Definition: Locales.php:373
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Localization\Locales\$languages
‪array $languages
Definition: Locales.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:64
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:56