[TASK] Move and Namespace classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / Locales.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Xavier Perseguers <typo3@perseguers.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Locales.
31 *
32 * Defining backend system languages
33 * When adding new keys, remember to:
34 * - Add character encoding for lang. key in t3lib/class.t3lib_cs.php
35 * (default for new languages is "utf-8")
36 * - Add mappings for language in t3lib/class.t3lib_cs.php (TYPO3/ISO,
37 * language/script, script/charset)
38 * - Update 'setup' extension labels (sysext/setup/mod/locallang.xlf)
39 * That's it!
40 *
41 * @package TYPO3
42 * @subpackage t3lib
43 * @author Xavier Perseguers <typo3@perseguers.ch>
44 */
45 class Locales implements \TYPO3\CMS\Core\SingletonInterface {
46
47 /**
48 * Supported TYPO3 languages with locales
49 *
50 * @var array
51 */
52 protected $languages = array(
53 'default' => 'English',
54 'af' => 'Afrikaans',
55 'ar' => 'Arabic',
56 'bs' => 'Bosnian',
57 'bg' => 'Bulgarian',
58 'ca' => 'Catalan',
59 'ch' => 'Chinese (Simpl.)',
60 'cs' => 'Czech',
61 'da' => 'Danish',
62 'de' => 'German',
63 'el' => 'Greek',
64 'eo' => 'Esperanto',
65 'es' => 'Spanish',
66 'et' => 'Estonian',
67 'eu' => 'Basque',
68 'fa' => 'Persian',
69 'fi' => 'Finnish',
70 'fo' => 'Faroese',
71 'fr' => 'French',
72 'fr_CA' => 'French (Canada)',
73 'gl' => 'Galician',
74 'he' => 'Hebrew',
75 'hi' => 'Hindi',
76 'hr' => 'Croatian',
77 'hu' => 'Hungarian',
78 'is' => 'Icelandic',
79 'it' => 'Italian',
80 'ja' => 'Japanese',
81 'ka' => 'Georgian',
82 'kl' => 'Greenlandic',
83 'km' => 'Khmer',
84 'ko' => 'Korean',
85 'lt' => 'Lithuanian',
86 'lv' => 'Latvian',
87 'ms' => 'Malay',
88 'nl' => 'Dutch',
89 'no' => 'Norwegian',
90 'pl' => 'Polish',
91 'pt' => 'Portuguese',
92 'pt_BR' => 'Brazilian Portuguese',
93 'ro' => 'Romanian',
94 'ru' => 'Russian',
95 'sk' => 'Slovak',
96 'sl' => 'Slovenian',
97 'sq' => 'Albanian',
98 'sr' => 'Serbian',
99 'sv' => 'Swedish',
100 'th' => 'Thai',
101 'tr' => 'Turkish',
102 'uk' => 'Ukrainian',
103 'vi' => 'Vietnamese',
104 'zh' => 'Chinese (Trad.)'
105 );
106
107 /**
108 * Supported TYPO3 locales
109 *
110 * @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0
111 * @var array
112 */
113 protected $locales = array();
114
115 /**
116 * Mapping with codes used by TYPO3 4.5 and below
117 *
118 * @var array
119 */
120 protected $isoReverseMapping = array(
121 'bs' => 'ba',
122 // Bosnian
123 'cs' => 'cz',
124 // Czech
125 'da' => 'dk',
126 // Danish
127 'el' => 'gr',
128 // Greek
129 'fr_CA' => 'qc',
130 // French (Canada)
131 'gl' => 'ga',
132 // Galician
133 'ja' => 'jp',
134 // Japanese
135 'ka' => 'ge',
136 // Georgian
137 'kl' => 'gl',
138 // Greenlandic
139 'ko' => 'kr',
140 // Korean
141 'ms' => 'my',
142 // Malay
143 'pt_BR' => 'br',
144 // Portuguese (Brazil)
145 'sl' => 'si',
146 // Slovenian
147 'sv' => 'se',
148 // Swedish
149 'uk' => 'ua',
150 // Ukrainian
151 'vi' => 'vn',
152 // Vietnamese
153 'zh' => 'hk',
154 // Chinese (China)
155 'zh_CN' => 'ch',
156 // Chinese (Simplified)
157 'zh_HK' => 'hk'
158 );
159
160 /**
161 * @var array
162 */
163 protected $isoMapping;
164
165 /**
166 * Dependencies for locales
167 *
168 * @var array
169 */
170 protected $localeDependencies;
171
172 /**
173 * Initializes the languages.
174 *
175 * @return void
176 */
177 static public function initialize() {
178 /** @var $instance \TYPO3\CMS\Core\Localization\Locales */
179 $instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
180 $instance->isoMapping = array_flip($instance->isoReverseMapping);
181 // Allow user-defined locales
182 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'])) {
183 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] as $locale => $name) {
184 if (!isset($instance->languages[$locale])) {
185 $instance->languages[$locale] = $name;
186 }
187 }
188 }
189 // Initializes the locale dependencies with TYPO3 supported locales
190 $instance->localeDependencies = array();
191 foreach ($instance->languages as $locale => $name) {
192 if (strlen($locale) == 5) {
193 $instance->localeDependencies[$locale] = array(substr($locale, 0, 2));
194 }
195 }
196 // Merge user-provided locale dependencies
197 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'])) {
198 $instance->localeDependencies = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($instance->localeDependencies, $GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']);
199 }
200 /** @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0 */
201 $instance->locales = array_keys($instance->languages);
202 /** @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0 */
203 define('TYPO3_languages', implode('|', $instance->getLocales()));
204 }
205
206 /**
207 * Returns the locales.
208 *
209 * @return array
210 */
211 public function getLocales() {
212 return array_keys($this->languages);
213 }
214
215 /**
216 * Returns the supported languages indexed by their corresponding locale.
217 *
218 * @return array
219 */
220 public function getLanguages() {
221 return $this->languages;
222 }
223
224 /**
225 * Returns the mapping between TYPO3 (old) language codes and ISO codes.
226 *
227 * @return array
228 */
229 public function getIsoMapping() {
230 return $this->isoMapping;
231 }
232
233 /**
234 * Returns the locales as referenced by the TER and TYPO3 localization files.
235 *
236 * @return array
237 * @deprecated since TYPO3 4.6
238 */
239 public function getTerLocales() {
240 return $this->convertToTerLocales(array_keys($this->languages));
241 }
242
243 /**
244 * Returns the dependencies of a given locale, if any.
245 *
246 * @param string $locale
247 * @return array
248 */
249 public function getLocaleDependencies($locale) {
250 $dependencies = array();
251 if (isset($this->localeDependencies[$locale])) {
252 $dependencies = $this->localeDependencies[$locale];
253 // Search for dependencies recursively
254 $localeDependencies = $dependencies;
255 foreach ($localeDependencies as $dependency) {
256 if (isset($this->localeDependencies[$dependency])) {
257 $dependencies = array_merge($dependencies, $this->getLocaleDependencies($dependency));
258 }
259 }
260 }
261 return $dependencies;
262 }
263
264 /**
265 * Returns the dependencies of a given locale using TER compatible locale codes.
266 *
267 * @param string $locale
268 * @return array
269 * @deprecated since TYPO3 4.6
270 */
271 public function getTerLocaleDependencies($locale) {
272 $terLocale = isset($this->isoMapping[$locale]) ? $this->isoMapping[$locale] : $locale;
273 return $this->convertToTerLocales($this->getLocaleDependencies($terLocale));
274 }
275
276 /**
277 * Converts an array of ISO locale codes into their TER equivalent.
278 *
279 * @param array $locales
280 * @return array
281 * @deprecated since TYPO3 4.6
282 */
283 protected function convertToTerLocales(array $locales) {
284 $terLocales = array();
285 foreach ($locales as $locale) {
286 $terLocales[] = isset($this->isoReverseMapping[$locale]) ? $this->isoReverseMapping[$locale] : $locale;
287 }
288 return $terLocales;
289 }
290
291 }
292
293
294 ?>