TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Locales.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Localization;
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 (Simpl.)',
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  'ms' => 'Malay',
71  'nl' => 'Dutch',
72  'no' => 'Norwegian',
73  'pl' => 'Polish',
74  'pt' => 'Portuguese',
75  'pt_BR' => 'Brazilian Portuguese',
76  'ro' => 'Romanian',
77  'ru' => 'Russian',
78  'sk' => 'Slovak',
79  'sl' => 'Slovenian',
80  'sq' => 'Albanian',
81  'sr' => 'Serbian',
82  'sv' => 'Swedish',
83  'th' => 'Thai',
84  'tr' => 'Turkish',
85  'uk' => 'Ukrainian',
86  'vi' => 'Vietnamese',
87  'zh' => 'Chinese (Trad.)'
88  ];
89 
95  protected $isoReverseMapping = [
96  'bs' => 'ba', // Bosnian
97  'cs' => 'cz', // Czech
98  'da' => 'dk', // Danish
99  'el' => 'gr', // Greek
100  'fr_CA' => 'qc', // French (Canada)
101  'gl' => 'ga', // Galician
102  'ja' => 'jp', // Japanese
103  'ka' => 'ge', // Georgian
104  'kl' => 'gl', // Greenlandic
105  'ko' => 'kr', // Korean
106  'ms' => 'my', // Malay
107  'pt_BR' => 'br', // Portuguese (Brazil)
108  'sl' => 'si', // Slovenian
109  'sv' => 'se', // Swedish
110  'uk' => 'ua', // Ukrainian
111  'vi' => 'vn', // Vietnamese
112  'zh' => 'hk', // Chinese (China)
113  'zh_CN' => 'ch', // Chinese (Simplified)
114  'zh_HK' => 'hk'
115  ];
116 
122  protected $isoMapping;
123 
130 
136  public static function initialize()
137  {
139  $instance = GeneralUtility::makeInstance(self::class);
140  $instance->isoMapping = array_flip($instance->isoReverseMapping);
141  // Allow user-defined locales
142  if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'])) {
143  foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] as $locale => $name) {
144  if (!isset($instance->languages[$locale])) {
145  $instance->languages[$locale] = $name;
146  }
147  }
148  }
149  // Initializes the locale dependencies with TYPO3 supported locales
150  $instance->localeDependencies = [];
151  foreach ($instance->languages as $locale => $name) {
152  if (strlen($locale) === 5) {
153  $instance->localeDependencies[$locale] = [substr($locale, 0, 2)];
154  }
155  }
156  // Merge user-provided locale dependencies
157  if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'])) {
158  ArrayUtility::mergeRecursiveWithOverrule($instance->localeDependencies, $GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']);
159  }
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 = [];
224  foreach ($this->getIsoMapping() as $typo3Lang => $isoLang) {
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 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
getPreferredClientLanguage($languageCodesList)
Definition: Locales.php:221
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)