[TASK] Update copyright year to 2013
[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 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 * @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 * Supported TYPO3 locales
107 *
108 * @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0
109 * @var array
110 */
111 protected $locales = array();
112
113 /**
114 * Mapping with codes used by TYPO3 4.5 and below
115 *
116 * @var array
117 */
118 protected $isoReverseMapping = array(
119 'bs' => 'ba',
120 // Bosnian
121 'cs' => 'cz',
122 // Czech
123 'da' => 'dk',
124 // Danish
125 'el' => 'gr',
126 // Greek
127 'fr_CA' => 'qc',
128 // French (Canada)
129 'gl' => 'ga',
130 // Galician
131 'ja' => 'jp',
132 // Japanese
133 'ka' => 'ge',
134 // Georgian
135 'kl' => 'gl',
136 // Greenlandic
137 'ko' => 'kr',
138 // Korean
139 'ms' => 'my',
140 // Malay
141 'pt_BR' => 'br',
142 // Portuguese (Brazil)
143 'sl' => 'si',
144 // Slovenian
145 'sv' => 'se',
146 // Swedish
147 'uk' => 'ua',
148 // Ukrainian
149 'vi' => 'vn',
150 // Vietnamese
151 'zh' => 'hk',
152 // Chinese (China)
153 'zh_CN' => 'ch',
154 // Chinese (Simplified)
155 'zh_HK' => 'hk'
156 );
157
158 /**
159 * @var array
160 */
161 protected $isoMapping;
162
163 /**
164 * Dependencies for locales
165 *
166 * @var array
167 */
168 protected $localeDependencies;
169
170 /**
171 * Initializes the languages.
172 *
173 * @return void
174 */
175 static public function initialize() {
176 /** @var $instance \TYPO3\CMS\Core\Localization\Locales */
177 $instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
178 $instance->isoMapping = array_flip($instance->isoReverseMapping);
179 // Allow user-defined locales
180 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'])) {
181 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] as $locale => $name) {
182 if (!isset($instance->languages[$locale])) {
183 $instance->languages[$locale] = $name;
184 }
185 }
186 }
187 // Initializes the locale dependencies with TYPO3 supported locales
188 $instance->localeDependencies = array();
189 foreach ($instance->languages as $locale => $name) {
190 if (strlen($locale) == 5) {
191 $instance->localeDependencies[$locale] = array(substr($locale, 0, 2));
192 }
193 }
194 // Merge user-provided locale dependencies
195 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'])) {
196 $instance->localeDependencies = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($instance->localeDependencies, $GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']);
197 }
198 /** @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0 */
199 $instance->locales = array_keys($instance->languages);
200 /** @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0 */
201 define('TYPO3_languages', implode('|', $instance->getLocales()));
202 }
203
204 /**
205 * Returns the locales.
206 *
207 * @return array
208 */
209 public function getLocales() {
210 return array_keys($this->languages);
211 }
212
213 /**
214 * Returns the supported languages indexed by their corresponding locale.
215 *
216 * @return array
217 */
218 public function getLanguages() {
219 return $this->languages;
220 }
221
222 /**
223 * Returns the mapping between TYPO3 (old) language codes and ISO codes.
224 *
225 * @return array
226 */
227 public function getIsoMapping() {
228 return $this->isoMapping;
229 }
230
231 /**
232 * Returns the locales as referenced by the TER and TYPO3 localization files.
233 *
234 * @return array
235 * @deprecated since TYPO3 4.6
236 */
237 public function getTerLocales() {
238 return $this->convertToTerLocales(array_keys($this->languages));
239 }
240
241 /**
242 * Returns the dependencies of a given locale, if any.
243 *
244 * @param string $locale
245 * @return array
246 */
247 public function getLocaleDependencies($locale) {
248 $dependencies = array();
249 if (isset($this->localeDependencies[$locale])) {
250 $dependencies = $this->localeDependencies[$locale];
251 // Search for dependencies recursively
252 $localeDependencies = $dependencies;
253 foreach ($localeDependencies as $dependency) {
254 if (isset($this->localeDependencies[$dependency])) {
255 $dependencies = array_merge($dependencies, $this->getLocaleDependencies($dependency));
256 }
257 }
258 }
259 return $dependencies;
260 }
261
262 /**
263 * Returns the dependencies of a given locale using TER compatible locale codes.
264 *
265 * @param string $locale
266 * @return array
267 * @deprecated since TYPO3 4.6
268 */
269 public function getTerLocaleDependencies($locale) {
270 $terLocale = isset($this->isoMapping[$locale]) ? $this->isoMapping[$locale] : $locale;
271 return $this->convertToTerLocales($this->getLocaleDependencies($terLocale));
272 }
273
274 /**
275 * Converts an array of ISO locale codes into their TER equivalent.
276 *
277 * @param array $locales
278 * @return array
279 * @deprecated since TYPO3 4.6
280 */
281 protected function convertToTerLocales(array $locales) {
282 $terLocales = array();
283 foreach ($locales as $locale) {
284 $terLocales[] = isset($this->isoReverseMapping[$locale]) ? $this->isoReverseMapping[$locale] : $locale;
285 }
286 return $terLocales;
287 }
288
289 }
290
291
292 ?>