‪TYPO3CMS  11.5
Locales.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
30 {
36  protected ‪$languages = [
37  'default' => 'English',
38  'af' => 'Afrikaans',
39  'ar' => 'Arabic',
40  'bs' => 'Bosnian',
41  'bg' => 'Bulgarian',
42  'ca' => 'Catalan',
43  'ch' => 'Chinese (Simple)',
44  'cs' => 'Czech',
45  'cy' => 'Welsh',
46  'da' => 'Danish',
47  'de' => 'German',
48  'el' => 'Greek',
49  'eo' => 'Esperanto',
50  'es' => 'Spanish',
51  'et' => 'Estonian',
52  'eu' => 'Basque',
53  'fa' => 'Persian',
54  'fi' => 'Finnish',
55  'fo' => 'Faroese',
56  'fr' => 'French',
57  'fr_CA' => 'French (Canada)',
58  'gl' => 'Galician',
59  'he' => 'Hebrew',
60  'hi' => 'Hindi',
61  'hr' => 'Croatian',
62  'hu' => 'Hungarian',
63  'is' => 'Icelandic',
64  'it' => 'Italian',
65  'ja' => 'Japanese',
66  'ka' => 'Georgian',
67  'kl' => 'Greenlandic',
68  'km' => 'Khmer',
69  'ko' => 'Korean',
70  'lt' => 'Lithuanian',
71  'lv' => 'Latvian',
72  'mi' => 'Maori',
73  'mk' => 'Macedonian',
74  'ms' => 'Malay',
75  'nl' => 'Dutch',
76  'no' => 'Norwegian',
77  'pl' => 'Polish',
78  'pt' => 'Portuguese',
79  'pt_BR' => 'Brazilian Portuguese',
80  'ro' => 'Romanian',
81  'ru' => 'Russian',
82  'rw' => 'Kinyarwanda',
83  'sk' => 'Slovak',
84  'sl' => 'Slovenian',
85  'sn' => 'Shona (Bantu)',
86  'sq' => 'Albanian',
87  'sr' => 'Serbian',
88  'sv' => 'Swedish',
89  'th' => 'Thai',
90  'tr' => 'Turkish',
91  'uk' => 'Ukrainian',
92  'vi' => 'Vietnamese',
93  'zh' => 'Chinese (Trad)',
94  ];
95 
101  protected ‪$isoReverseMapping = [
102  'bs' => 'ba', // Bosnian
103  'cs' => 'cz', // Czech
104  'da' => 'dk', // Danish
105  'el' => 'gr', // Greek
106  'fr_CA' => 'qc', // French (Canada)
107  'gl' => 'ga', // Galician
108  'ja' => 'jp', // Japanese
109  'ka' => 'ge', // Georgian
110  'kl' => 'gl', // Greenlandic
111  'ko' => 'kr', // Korean
112  'ms' => 'my', // Malay
113  'pt_BR' => 'br', // Portuguese (Brazil)
114  'sl' => 'si', // Slovenian
115  'sv' => 'se', // Swedish
116  'uk' => 'ua', // Ukrainian
117  'vi' => 'vn', // Vietnamese
118  'zh' => 'hk', // Chinese (China)
119  'zh_CN' => 'ch', // Chinese (Simplified)
120  'zh_HK' => 'hk', // Chinese (Simplified Hong Kong)
121  'zh_Hans_CN' => 'ch', // Chinese (Simplified Han)
122  ];
123 
130  protected ‪$localeDependencies = [
131  'pt_BR' => ['pt'],
132  'fr_CA' => ['fr'],
133  ];
134 
135  public function ‪__construct()
136  {
137  // Allow user-defined locales
138  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] ?? [] as $locale => $name) {
139  if (!isset($this->languages[$locale])) {
140  $this->languages[$locale] = $name;
141  }
142  // Initializes the locale dependencies with TYPO3 supported locales
143  if (strlen($locale) === 5) {
144  $this->localeDependencies[$locale] = [substr($locale, 0, 2)];
145  }
146  }
147  // Merge user-provided locale dependencies
148  if (is_array(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'] ?? null)) {
149  $this->localeDependencies = array_replace_recursive(
150  $this->localeDependencies,
151  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']
152  );
153  }
154  }
155 
161  public function ‪getLocales()
162  {
163  return array_keys($this->languages);
164  }
165 
171  public function ‪getLanguages()
172  {
173  return ‪$this->languages;
174  }
175 
181  public function ‪getIsoMapping()
182  {
183  return array_flip($this->isoReverseMapping);
184  }
185 
192  public function ‪getLocaleDependencies($locale)
193  {
194  $dependencies = [];
195  if (isset($this->localeDependencies[$locale])) {
196  $dependencies = $this->localeDependencies[$locale];
197  // Search for dependencies recursively
198  ‪$localeDependencies = $dependencies;
199  foreach (‪$localeDependencies as $dependency) {
200  if (isset($this->localeDependencies[$dependency])) {
201  $dependencies = array_merge($dependencies, $this->‪getLocaleDependencies($dependency));
202  }
203  }
204  }
205  return $dependencies;
206  }
207 
215  public function ‪getPreferredClientLanguage($languageCodesList)
216  {
217  $allLanguageCodesFromLocales = ['en' => 'default'];
218  foreach ($this->isoReverseMapping as $isoLang => $typo3Lang) {
219  $isoLang = str_replace('_', '-', $isoLang);
220  $allLanguageCodesFromLocales[$isoLang] = $typo3Lang;
221  }
222  foreach ($this->‪getLocales() as $locale) {
223  $locale = str_replace('_', '-', $locale);
224  $allLanguageCodesFromLocales[$locale] = $locale;
225  }
226  $selectedLanguage = 'default';
227  $preferredLanguages = ‪GeneralUtility::trimExplode(',', $languageCodesList);
228  // Order the preferred languages after they key
229  $sortedPreferredLanguages = [];
230  foreach ($preferredLanguages as $preferredLanguage) {
231  $quality = 1.0;
232  if (str_contains($preferredLanguage, ';q=')) {
233  [$preferredLanguage, $quality] = explode(';q=', $preferredLanguage);
234  }
235  $sortedPreferredLanguages[$preferredLanguage] = $quality;
236  }
237  // Loop through the languages, with the highest priority first
238  arsort($sortedPreferredLanguages, SORT_NUMERIC);
239  foreach ($sortedPreferredLanguages as $preferredLanguage => $quality) {
240  if (isset($allLanguageCodesFromLocales[$preferredLanguage])) {
241  $selectedLanguage = $allLanguageCodesFromLocales[$preferredLanguage];
242  break;
243  }
244  // Strip the country code from the end
245  [$preferredLanguage] = explode('-', $preferredLanguage);
246  if (isset($allLanguageCodesFromLocales[$preferredLanguage])) {
247  $selectedLanguage = $allLanguageCodesFromLocales[$preferredLanguage];
248  break;
249  }
250  }
251  if (!$selectedLanguage || $selectedLanguage === 'en') {
252  $selectedLanguage = 'default';
253  }
254  return $selectedLanguage;
255  }
256 
264  public static function ‪setSystemLocaleFromSiteLanguage(SiteLanguage $siteLanguage): bool
265  {
266  $locale = $siteLanguage->getLocale();
267  // No locale was given, so return false
268  if (!$locale) {
269  return false;
270  }
271  return ‪self::setLocale($locale, $locale);
272  }
273 
283  protected static function ‪setLocale(string $locale, string $localeStringForTrigger): bool
284  {
285  $incomingLocale = $locale;
286  $availableLocales = ‪GeneralUtility::trimExplode(',', $locale, true);
287  // If LC_NUMERIC is set e.g. to 'de_DE' PHP parses float values locale-aware resulting in strings with comma
288  // as decimal point which causes problems with value conversions - so we set all locale types except LC_NUMERIC
289  // @see https://bugs.php.net/bug.php?id=53711
290  $locale = setlocale(LC_COLLATE, ...$availableLocales);
291  if ($locale) {
292  // As str_* methods are locale aware and turkish has no upper case I
293  // Class autoloading and other checks depending on case changing break with turkish locale LC_CTYPE
294  // @see http://bugs.php.net/bug.php?id=35050
295  if (strpos($locale, 'tr') !== 0) {
296  setlocale(LC_CTYPE, ...$availableLocales);
297  }
298  setlocale(LC_MONETARY, ...$availableLocales);
299  setlocale(LC_TIME, ...$availableLocales);
300  } else {
301  // Retry again without the "utf-8" POSIX platform suffix if this is given.
302  if (str_contains($incomingLocale, '.')) {
303  [$localeWithoutModifier] = explode('.', $incomingLocale);
304  return ‪self::setLocale($localeWithoutModifier, $incomingLocale);
305  }
306  if ($localeStringForTrigger === $locale) {
307  GeneralUtility::makeInstance(LogManager::class)
308  ->getLogger(__CLASS__)
309  ->error('Locale "' . htmlspecialchars($localeStringForTrigger) . '" not found.');
310  } else {
311  GeneralUtility::makeInstance(LogManager::class)
312  ->getLogger(__CLASS__)
313  ->error('Locale "' . htmlspecialchars($localeStringForTrigger) . '" and "' . htmlspecialchars($incomingLocale) . '" not found.');
314  }
315  return false;
316  }
317  return true;
318  }
319 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Core\Localization\Locales\setSystemLocaleFromSiteLanguage
‪static bool setSystemLocaleFromSiteLanguage(SiteLanguage $siteLanguage)
Definition: Locales.php:261
‪TYPO3\CMS\Core\Localization\Locales\setLocale
‪static setLocale(string $locale, string $localeStringForTrigger)
Definition: Locales.php:280
‪TYPO3\CMS\Core\Localization\Locales\$localeDependencies
‪array $localeDependencies
Definition: Locales.php:127
‪TYPO3\CMS\Core\Localization\Locales\getLocaleDependencies
‪array getLocaleDependencies($locale)
Definition: Locales.php:189
‪TYPO3\CMS\Core\Localization\Locales
Definition: Locales.php:30
‪TYPO3\CMS\Core\Localization\Locales\__construct
‪__construct()
Definition: Locales.php:132
‪TYPO3\CMS\Core\Localization
Definition: CacheWarmer.php:18
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:26
‪TYPO3\CMS\Core\Localization\Locales\getIsoMapping
‪array getIsoMapping()
Definition: Locales.php:178
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\getLocale
‪string getLocale()
Definition: SiteLanguage.php:206
‪TYPO3\CMS\Core\Localization\Locales\getPreferredClientLanguage
‪string getPreferredClientLanguage($languageCodesList)
Definition: Locales.php:212
‪TYPO3\CMS\Core\Localization\Locales\getLanguages
‪array getLanguages()
Definition: Locales.php:168
‪TYPO3\CMS\Core\Localization\Locales\$isoReverseMapping
‪array $isoReverseMapping
Definition: Locales.php:99
‪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\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Localization\Locales\$languages
‪array $languages
Definition: Locales.php:35
‪TYPO3\CMS\Core\Localization\Locales\getLocales
‪array getLocales()
Definition: Locales.php:158