[TASK] Cleanup of TYPO3\CMS\Core\Localization and Charset
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / Locales.php
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
17 use TYPO3\CMS\Core\Utility\ArrayUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Locales.
22 *
23 * Defining backend system languages
24 * When adding new keys, remember to:
25 * - Add character encoding for lang. key in \TYPO3\CMS\Core\Charset\CharsetConverter
26 * (default for new languages is "utf-8")
27 * - Add mappings for language in \TYPO3\CMS\Core\Charset\CharsetConverter
28 * (TYPO3/ISO, language/script, script/charset)
29 * - Update 'setup' extension labels (sysext/setup/mod/locallang.xlf)
30 * That's it!
31 *
32 * @author Xavier Perseguers <typo3@perseguers.ch>
33 */
34 class Locales implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * Supported TYPO3 languages with locales
38 *
39 * @var array
40 */
41 protected $languages = array(
42 'default' => 'English',
43 'af' => 'Afrikaans',
44 'ar' => 'Arabic',
45 'bs' => 'Bosnian',
46 'bg' => 'Bulgarian',
47 'ca' => 'Catalan',
48 'ch' => 'Chinese (Simpl.)',
49 'cs' => 'Czech',
50 'da' => 'Danish',
51 'de' => 'German',
52 'el' => 'Greek',
53 'eo' => 'Esperanto',
54 'es' => 'Spanish',
55 'et' => 'Estonian',
56 'eu' => 'Basque',
57 'fa' => 'Persian',
58 'fi' => 'Finnish',
59 'fo' => 'Faroese',
60 'fr' => 'French',
61 'fr_CA' => 'French (Canada)',
62 'gl' => 'Galician',
63 'he' => 'Hebrew',
64 'hi' => 'Hindi',
65 'hr' => 'Croatian',
66 'hu' => 'Hungarian',
67 'is' => 'Icelandic',
68 'it' => 'Italian',
69 'ja' => 'Japanese',
70 'ka' => 'Georgian',
71 'kl' => 'Greenlandic',
72 'km' => 'Khmer',
73 'ko' => 'Korean',
74 'lt' => 'Lithuanian',
75 'lv' => 'Latvian',
76 'ms' => 'Malay',
77 'nl' => 'Dutch',
78 'no' => 'Norwegian',
79 'pl' => 'Polish',
80 'pt' => 'Portuguese',
81 'pt_BR' => 'Brazilian Portuguese',
82 'ro' => 'Romanian',
83 'ru' => 'Russian',
84 'sk' => 'Slovak',
85 'sl' => 'Slovenian',
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
96 /**
97 * Reversed mapping with codes used by TYPO3 4.5 and below
98 *
99 * @var array
100 */
101 protected $isoReverseMapping = array(
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'
121 );
122
123 /**
124 * Mapping with codes used by TYPO3 4.5 and below
125 *
126 * @var array
127 */
128 protected $isoMapping;
129
130 /**
131 * Dependencies for locales
132 *
133 * @var array
134 */
135 protected $localeDependencies;
136
137 /**
138 * Initializes the languages.
139 *
140 * @return void
141 */
142 static public function initialize() {
143 /** @var $instance Locales */
144 $instance = GeneralUtility::makeInstance(Locales::class);
145 $instance->isoMapping = array_flip($instance->isoReverseMapping);
146 // Allow user-defined locales
147 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'])) {
148 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] as $locale => $name) {
149 if (!isset($instance->languages[$locale])) {
150 $instance->languages[$locale] = $name;
151 }
152 }
153 }
154 // Initializes the locale dependencies with TYPO3 supported locales
155 $instance->localeDependencies = array();
156 foreach ($instance->languages as $locale => $name) {
157 if (strlen($locale) === 5) {
158 $instance->localeDependencies[$locale] = array(substr($locale, 0, 2));
159 }
160 }
161 // Merge user-provided locale dependencies
162 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'])) {
163 ArrayUtility::mergeRecursiveWithOverrule($instance->localeDependencies, $GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']);
164 }
165 }
166
167 /**
168 * Returns the locales.
169 *
170 * @return array
171 */
172 public function getLocales() {
173 return array_keys($this->languages);
174 }
175
176 /**
177 * Returns the supported languages indexed by their corresponding locale.
178 *
179 * @return array
180 */
181 public function getLanguages() {
182 return $this->languages;
183 }
184
185 /**
186 * Returns the mapping between TYPO3 (old) language codes and ISO codes.
187 *
188 * @return array
189 */
190 public function getIsoMapping() {
191 return $this->isoMapping;
192 }
193
194 /**
195 * Returns the dependencies of a given locale, if any.
196 *
197 * @param string $locale
198 * @return array
199 */
200 public function getLocaleDependencies($locale) {
201 $dependencies = array();
202 if (isset($this->localeDependencies[$locale])) {
203 $dependencies = $this->localeDependencies[$locale];
204 // Search for dependencies recursively
205 $localeDependencies = $dependencies;
206 foreach ($localeDependencies as $dependency) {
207 if (isset($this->localeDependencies[$dependency])) {
208 $dependencies = array_merge($dependencies, $this->getLocaleDependencies($dependency));
209 }
210 }
211 }
212 return $dependencies;
213 }
214
215 }