‪TYPO3CMS  9.5
Locales.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
29 {
35  protected ‪$languages = [
36  'default' => 'English',
37  'af' => 'Afrikaans',
38  'ar' => 'Arabic',
39  'bs' => 'Bosnian',
40  'bg' => 'Bulgarian',
41  'ca' => 'Catalan',
42  'ch' => 'Chinese (Simple)',
43  'cs' => 'Czech',
44  'da' => 'Danish',
45  'de' => 'German',
46  'el' => 'Greek',
47  'eo' => 'Esperanto',
48  'es' => 'Spanish',
49  'et' => 'Estonian',
50  'eu' => 'Basque',
51  'fa' => 'Persian',
52  'fi' => 'Finnish',
53  'fo' => 'Faroese',
54  'fr' => 'French',
55  'fr_CA' => 'French (Canada)',
56  'gl' => 'Galician',
57  'he' => 'Hebrew',
58  'hi' => 'Hindi',
59  'hr' => 'Croatian',
60  'hu' => 'Hungarian',
61  'is' => 'Icelandic',
62  'it' => 'Italian',
63  'ja' => 'Japanese',
64  'ka' => 'Georgian',
65  'kl' => 'Greenlandic',
66  'km' => 'Khmer',
67  'ko' => 'Korean',
68  'lt' => 'Lithuanian',
69  'lv' => 'Latvian',
70  'mi' => 'Maori',
71  'mk' => 'Macedonian',
72  'ms' => 'Malay',
73  'nl' => 'Dutch',
74  'no' => 'Norwegian',
75  'pl' => 'Polish',
76  'pt' => 'Portuguese',
77  'pt_BR' => 'Brazilian Portuguese',
78  'ro' => 'Romanian',
79  'ru' => 'Russian',
80  'rw' => 'Kinyarwanda',
81  'sk' => 'Slovak',
82  'sl' => 'Slovenian',
83  'sq' => 'Albanian',
84  'sr' => 'Serbian',
85  'sv' => 'Swedish',
86  'th' => 'Thai',
87  'tr' => 'Turkish',
88  'uk' => 'Ukrainian',
89  'vi' => 'Vietnamese',
90  'zh' => 'Chinese (Trad)'
91  ];
92 
98  protected ‪$isoReverseMapping = [
99  'bs' => 'ba', // Bosnian
100  'cs' => 'cz', // Czech
101  'da' => 'dk', // Danish
102  'el' => 'gr', // Greek
103  'fr_CA' => 'qc', // French (Canada)
104  'gl' => 'ga', // Galician
105  'ja' => 'jp', // Japanese
106  'ka' => 'ge', // Georgian
107  'kl' => 'gl', // Greenlandic
108  'ko' => 'kr', // Korean
109  'ms' => 'my', // Malay
110  'pt_BR' => 'br', // Portuguese (Brazil)
111  'sl' => 'si', // Slovenian
112  'sv' => 'se', // Swedish
113  'uk' => 'ua', // Ukrainian
114  'vi' => 'vn', // Vietnamese
115  'zh' => 'hk', // Chinese (China)
116  'zh_CN' => 'ch', // Chinese (Simplified)
117  'zh_HK' => 'hk', // Chinese (Simplified Hong Kong)
118  'zh_Hans_CN' => 'ch' // Chinese (Simplified Han)
119  ];
120 
126  protected ‪$isoMapping;
127 
133  protected ‪$localeDependencies;
134 
139  public static function ‪initialize(): ‪Locales
140  {
142  $instance = GeneralUtility::makeInstance(self::class);
143  $instance->isoMapping = array_flip($instance->isoReverseMapping);
144  // Allow user-defined locales
145  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] ?? [] as $locale => $name) {
146  if (!isset($instance->languages[$locale])) {
147  $instance->languages[$locale] = $name;
148  }
149  }
150  // Initializes the locale dependencies with TYPO3 supported locales
151  $instance->localeDependencies = [];
152  foreach ($instance->languages as $locale => $name) {
153  if (strlen($locale) === 5) {
154  $instance->localeDependencies[$locale] = [substr($locale, 0, 2)];
155  }
156  }
157  // Merge user-provided locale dependencies
158  ‪ArrayUtility::mergeRecursiveWithOverrule($instance->localeDependencies, ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'] ?? []);
159  return $instance;
160  }
161 
167  public function ‪getLocales()
168  {
169  return array_keys($this->languages);
170  }
171 
177  public function ‪getLanguages()
178  {
179  return ‪$this->languages;
180  }
181 
187  public function ‪getIsoMapping()
188  {
189  return ‪$this->isoMapping;
190  }
191 
198  public function ‪getLocaleDependencies($locale)
199  {
200  $dependencies = [];
201  if (isset($this->localeDependencies[$locale])) {
202  $dependencies = $this->localeDependencies[$locale];
203  // Search for dependencies recursively
204  ‪$localeDependencies = $dependencies;
205  foreach (‪$localeDependencies as $dependency) {
206  if (isset($this->localeDependencies[$dependency])) {
207  $dependencies = array_merge($dependencies, $this->‪getLocaleDependencies($dependency));
208  }
209  }
210  }
211  return $dependencies;
212  }
213 
221  public function ‪getPreferredClientLanguage($languageCodesList)
222  {
223  $allLanguageCodesFromLocales = ['en' => 'default'];
224  foreach ($this->isoReverseMapping as $isoLang => $typo3Lang) {
225  $isoLang = str_replace('_', '-', $isoLang);
226  $allLanguageCodesFromLocales[$isoLang] = $typo3Lang;
227  }
228  foreach ($this->‪getLocales() as $locale) {
229  $locale = str_replace('_', '-', $locale);
230  $allLanguageCodesFromLocales[$locale] = $locale;
231  }
232  $selectedLanguage = 'default';
233  $preferredLanguages = GeneralUtility::trimExplode(',', $languageCodesList);
234  // Order the preferred languages after they key
235  $sortedPreferredLanguages = [];
236  foreach ($preferredLanguages as $preferredLanguage) {
237  $quality = 1.0;
238  if (strpos($preferredLanguage, ';q=') !== false) {
239  list($preferredLanguage, $quality) = explode(';q=', $preferredLanguage);
240  }
241  $sortedPreferredLanguages[$preferredLanguage] = $quality;
242  }
243  // Loop through the languages, with the highest priority first
244  arsort($sortedPreferredLanguages, SORT_NUMERIC);
245  foreach ($sortedPreferredLanguages as $preferredLanguage => $quality) {
246  if (isset($allLanguageCodesFromLocales[$preferredLanguage])) {
247  $selectedLanguage = $allLanguageCodesFromLocales[$preferredLanguage];
248  break;
249  }
250  // Strip the country code from the end
251  list($preferredLanguage, ) = explode('-', $preferredLanguage);
252  if (isset($allLanguageCodesFromLocales[$preferredLanguage])) {
253  $selectedLanguage = $allLanguageCodesFromLocales[$preferredLanguage];
254  break;
255  }
256  }
257  if (!$selectedLanguage || $selectedLanguage === 'en') {
258  $selectedLanguage = 'default';
259  }
260  return $selectedLanguage;
261  }
262 }
‪TYPO3\CMS\Core\Localization\Locales\initialize
‪static Locales initialize()
Definition: Locales.php:135
‪TYPO3\CMS\Core\Localization\Locales\$isoMapping
‪array $isoMapping
Definition: Locales.php:123
‪TYPO3\CMS\Core\Localization\Locales\$localeDependencies
‪array $localeDependencies
Definition: Locales.php:129
‪TYPO3\CMS\Core\Localization\Locales\getLocaleDependencies
‪array getLocaleDependencies($locale)
Definition: Locales.php:194
‪TYPO3\CMS\Core\Localization\Locales
Definition: Locales.php:29
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Core\Localization
‪TYPO3\CMS\Core\Localization\Locales\getIsoMapping
‪array getIsoMapping()
Definition: Locales.php:183
‪TYPO3\CMS\Core\Localization\Locales\getPreferredClientLanguage
‪string getPreferredClientLanguage($languageCodesList)
Definition: Locales.php:217
‪TYPO3\CMS\Core\Localization\Locales\getLanguages
‪array getLanguages()
Definition: Locales.php:173
‪TYPO3\CMS\Core\Localization\Locales\$isoReverseMapping
‪array $isoReverseMapping
Definition: Locales.php:96
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Localization\Locales\$languages
‪array $languages
Definition: Locales.php:34
‪TYPO3\CMS\Core\Localization\Locales\getLocales
‪array getLocales()
Definition: Locales.php:163