[TASK] Merge submodule dbal into core
[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-2013 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 \TYPO3\CMS\Core\Charset\CharsetConverter
35 * (default for new languages is "utf-8")
36 * - Add mappings for language in \TYPO3\CMS\Core\Charset\CharsetConverter
37 * (TYPO3/ISO, language/script, script/charset)
38 * - Update 'setup' extension labels (sysext/setup/mod/locallang.xlf)
39 * That's it!
40 *
41 * @author Xavier Perseguers <typo3@perseguers.ch>
42 */
43 class Locales implements \TYPO3\CMS\Core\SingletonInterface {
44
45 /**
46 * Supported TYPO3 languages with locales
47 *
48 * @var array
49 */
50 protected $languages = array(
51 'default' => 'English',
52 'af' => 'Afrikaans',
53 'ar' => 'Arabic',
54 'bs' => 'Bosnian',
55 'bg' => 'Bulgarian',
56 'ca' => 'Catalan',
57 'ch' => 'Chinese (Simpl.)',
58 'cs' => 'Czech',
59 'da' => 'Danish',
60 'de' => 'German',
61 'el' => 'Greek',
62 'eo' => 'Esperanto',
63 'es' => 'Spanish',
64 'et' => 'Estonian',
65 'eu' => 'Basque',
66 'fa' => 'Persian',
67 'fi' => 'Finnish',
68 'fo' => 'Faroese',
69 'fr' => 'French',
70 'fr_CA' => 'French (Canada)',
71 'gl' => 'Galician',
72 'he' => 'Hebrew',
73 'hi' => 'Hindi',
74 'hr' => 'Croatian',
75 'hu' => 'Hungarian',
76 'is' => 'Icelandic',
77 'it' => 'Italian',
78 'ja' => 'Japanese',
79 'ka' => 'Georgian',
80 'kl' => 'Greenlandic',
81 'km' => 'Khmer',
82 'ko' => 'Korean',
83 'lt' => 'Lithuanian',
84 'lv' => 'Latvian',
85 'ms' => 'Malay',
86 'nl' => 'Dutch',
87 'no' => 'Norwegian',
88 'pl' => 'Polish',
89 'pt' => 'Portuguese',
90 'pt_BR' => 'Brazilian Portuguese',
91 'ro' => 'Romanian',
92 'ru' => 'Russian',
93 'sk' => 'Slovak',
94 'sl' => 'Slovenian',
95 'sq' => 'Albanian',
96 'sr' => 'Serbian',
97 'sv' => 'Swedish',
98 'th' => 'Thai',
99 'tr' => 'Turkish',
100 'uk' => 'Ukrainian',
101 'vi' => 'Vietnamese',
102 'zh' => 'Chinese (Trad.)'
103 );
104
105 /**
106 * Mapping with codes used by TYPO3 4.5 and below
107 *
108 * @var array
109 */
110 protected $isoReverseMapping = array(
111 'bs' => 'ba',
112 // Bosnian
113 'cs' => 'cz',
114 // Czech
115 'da' => 'dk',
116 // Danish
117 'el' => 'gr',
118 // Greek
119 'fr_CA' => 'qc',
120 // French (Canada)
121 'gl' => 'ga',
122 // Galician
123 'ja' => 'jp',
124 // Japanese
125 'ka' => 'ge',
126 // Georgian
127 'kl' => 'gl',
128 // Greenlandic
129 'ko' => 'kr',
130 // Korean
131 'ms' => 'my',
132 // Malay
133 'pt_BR' => 'br',
134 // Portuguese (Brazil)
135 'sl' => 'si',
136 // Slovenian
137 'sv' => 'se',
138 // Swedish
139 'uk' => 'ua',
140 // Ukrainian
141 'vi' => 'vn',
142 // Vietnamese
143 'zh' => 'hk',
144 // Chinese (China)
145 'zh_CN' => 'ch',
146 // Chinese (Simplified)
147 'zh_HK' => 'hk'
148 );
149
150 /**
151 * @var array
152 */
153 protected $isoMapping;
154
155 /**
156 * Dependencies for locales
157 *
158 * @var array
159 */
160 protected $localeDependencies;
161
162 /**
163 * Initializes the languages.
164 *
165 * @return void
166 */
167 static public function initialize() {
168 /** @var $instance \TYPO3\CMS\Core\Localization\Locales */
169 $instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
170 $instance->isoMapping = array_flip($instance->isoReverseMapping);
171 // Allow user-defined locales
172 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'])) {
173 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] as $locale => $name) {
174 if (!isset($instance->languages[$locale])) {
175 $instance->languages[$locale] = $name;
176 }
177 }
178 }
179 // Initializes the locale dependencies with TYPO3 supported locales
180 $instance->localeDependencies = array();
181 foreach ($instance->languages as $locale => $name) {
182 if (strlen($locale) == 5) {
183 $instance->localeDependencies[$locale] = array(substr($locale, 0, 2));
184 }
185 }
186 // Merge user-provided locale dependencies
187 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'])) {
188 $instance->localeDependencies = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($instance->localeDependencies, $GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']);
189 }
190 }
191
192 /**
193 * Returns the locales.
194 *
195 * @return array
196 */
197 public function getLocales() {
198 return array_keys($this->languages);
199 }
200
201 /**
202 * Returns the supported languages indexed by their corresponding locale.
203 *
204 * @return array
205 */
206 public function getLanguages() {
207 return $this->languages;
208 }
209
210 /**
211 * Returns the mapping between TYPO3 (old) language codes and ISO codes.
212 *
213 * @return array
214 */
215 public function getIsoMapping() {
216 return $this->isoMapping;
217 }
218
219 /**
220 * Returns the locales as referenced by the TER and TYPO3 localization files.
221 *
222 * @return array
223 * @deprecated since TYPO3 4.6
224 */
225 public function getTerLocales() {
226 return $this->convertToTerLocales(array_keys($this->languages));
227 }
228
229 /**
230 * Returns the dependencies of a given locale, if any.
231 *
232 * @param string $locale
233 * @return array
234 */
235 public function getLocaleDependencies($locale) {
236 $dependencies = array();
237 if (isset($this->localeDependencies[$locale])) {
238 $dependencies = $this->localeDependencies[$locale];
239 // Search for dependencies recursively
240 $localeDependencies = $dependencies;
241 foreach ($localeDependencies as $dependency) {
242 if (isset($this->localeDependencies[$dependency])) {
243 $dependencies = array_merge($dependencies, $this->getLocaleDependencies($dependency));
244 }
245 }
246 }
247 return $dependencies;
248 }
249
250 /**
251 * Returns the dependencies of a given locale using TER compatible locale codes.
252 *
253 * @param string $locale
254 * @return array
255 * @deprecated since TYPO3 4.6
256 */
257 public function getTerLocaleDependencies($locale) {
258 $terLocale = isset($this->isoMapping[$locale]) ? $this->isoMapping[$locale] : $locale;
259 return $this->convertToTerLocales($this->getLocaleDependencies($terLocale));
260 }
261
262 /**
263 * Converts an array of ISO locale codes into their TER equivalent.
264 *
265 * @param array $locales
266 * @return array
267 * @deprecated since TYPO3 4.6
268 */
269 protected function convertToTerLocales(array $locales) {
270 $terLocales = array();
271 foreach ($locales as $locale) {
272 $terLocales[] = isset($this->isoReverseMapping[$locale]) ? $this->isoReverseMapping[$locale] : $locale;
273 }
274 return $terLocales;
275 }
276
277 }
278
279
280 ?>