2 /***************************************************************
5 * (c) 2011 Xavier Perseguers <typo3@perseguers.ch>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
31 * Defining backend system languages
32 * When adding new keys, remember to:
33 * - Add character encoding for lang. key in t3lib/class.t3lib_cs.php (default for new languages is "utf-8")
34 * - Add mappings for language in t3lib/class.t3lib_cs.php (TYPO3/ISO, language/script, script/charset)
35 * - Update 'setup' extension labels (sysext/setup/mod/locallang.xlf)
40 * @author Xavier Perseguers <typo3@perseguers.ch>
42 class t3lib_l10n_Locales
implements t3lib_Singleton
{
45 * Supported TYPO3 languages with locales
48 protected $languages = array(
49 'default' => 'English',
55 'ch' => 'Chinese (Simpl.)',
68 'fr_CA' => 'French (Canada)',
78 'kl' => 'Greenlandic',
88 'pt_BR' => 'Brazilian Portuguese',
100 'zh' => 'Chinese (Trad.)',
104 * Supported TYPO3 locales
105 * @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0
108 protected $locales = array();
111 * Mapping with codes used by TYPO3 4.5 and below
114 protected $isoReverseMapping = array(
115 'bs' => 'ba', // Bosnian
116 'cs' => 'cz', // Czech
117 'da' => 'dk', // Danish
118 'el' => 'gr', // Greek
119 'fr_CA' => 'qc', // French (Canada)
120 'gl' => 'ga', // Galician
121 'ja' => 'jp', // Japanese
122 'ka' => 'ge', // Georgian
123 'kl' => 'gl', // Greenlandic
124 'ko' => 'kr', // Korean
125 'ms' => 'my', // Malay
126 'pt_BR' => 'br', // Portuguese (Brazil)
127 'sl' => 'si', // Slovenian
128 'sv' => 'se', // Swedish
129 'uk' => 'ua', // Ukrainian
130 'vi' => 'vn', // Vietnamese
131 'zh' => 'hk', // Chinese (China)
132 'zh_CN' => 'ch', // Chinese (Simplified)
133 'zh_HK' => 'hk', // Chinese (China)
139 protected $isoMapping;
142 * Dependencies for locales
145 protected $localeDependencies;
148 * Initializes the languages.
153 public static function initialize() {
154 /** @var $instance t3lib_l10n_Locales */
155 $instance = t3lib_div
::makeInstance('t3lib_l10n_Locales');
156 $instance->isoMapping
= array_flip($instance->isoReverseMapping
);
158 // Allow user-defined locales
159 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'])) {
160 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['user'] as $locale => $name) {
161 if (!isset($instance->languages
[$locale])) {
162 $instance->languages
[$locale] = $name;
167 // Initializes the locale dependencies with TYPO3 supported locales
168 $instance->localeDependencies
= array();
169 foreach ($instance->languages
as $locale => $name) {
170 if (strlen($locale) == 5) {
171 $instance->localeDependencies
[$locale] = array(substr($locale, 0, 2));
174 // Merge user-provided locale dependencies
175 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies'])) {
176 $instance->localeDependencies
= t3lib_div
::array_merge_recursive_overrule($instance->localeDependencies
, $GLOBALS['TYPO3_CONF_VARS']['SYS']['localization']['locales']['dependencies']);
180 * @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0
182 $instance->locales
= array_keys($instance->languages
);
185 * @deprecated since TYPO3 4.6, will be removed in TYPO3 6.0
187 define('TYPO3_languages', implode('|', $instance->getLocales()));
191 * Returns the locales.
195 public function getLocales() {
196 return array_keys($this->languages
);
200 * Returns the supported languages indexed by their corresponding locale.
204 public function getLanguages() {
205 return $this->languages
;
209 * Returns the mapping between TYPO3 (old) language codes and ISO codes.
213 public function getIsoMapping() {
214 return $this->isoMapping
;
218 * Returns the locales as referenced by the TER and TYPO3 localization files.
221 * @deprecated since TYPO3 4.6
223 public function getTerLocales() {
224 return $this->convertToTerLocales(array_keys($this->languages
));
228 * Returns the dependencies of a given locale, if any.
230 * @param string $locale
233 public function getLocaleDependencies($locale) {
234 $dependencies = array();
235 if (isset($this->localeDependencies
[$locale])) {
236 $dependencies = $this->localeDependencies
[$locale];
238 // Search for dependencies recursively
239 $localeDependencies = $dependencies;
240 foreach ($localeDependencies as $dependency) {
241 if (isset($this->localeDependencies
[$dependency])) {
242 $dependencies = array_merge($dependencies, $this->getLocaleDependencies($dependency));
246 return $dependencies;
250 * Returns the dependencies of a given locale using TER compatible locale codes.
252 * @param string $locale
254 * @deprecated since TYPO3 4.6
256 public function getTerLocaleDependencies($locale) {
257 $terLocale = isset($this->isoMapping
[$locale])
258 ?
$this->isoMapping
[$locale]
260 return $this->convertToTerLocales($this->getLocaleDependencies($terLocale));
264 * Converts an array of ISO locale codes into their TER equivalent.
266 * @param array $locales
268 * @deprecated since TYPO3 4.6
270 protected function convertToTerLocales(array $locales) {
271 $terLocales = array();
272 foreach ($locales as $locale) {
273 $terLocales[] = isset($this->isoReverseMapping
[$locale]) ?
$this->isoReverseMapping
[$locale] : $locale;
281 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['t3lib/l10n/class.t3lib_l10n_locales.php'])) {
282 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['t3lib/l10n/class.t3lib_l10n_locales.php']);