8f230cb536400590ecc7d995becfd1cf032ef70d
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / Localization.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Sebastian Kurf├╝rst <sebastian@typo3.org>
6 * All rights reserved
7 *
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.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Localization helper which should be used to fetch localized labels.
27 *
28 * @package Extbase
29 * @subpackage Utility
30 * @version $ID:$
31 * @api
32 */
33 class Tx_Extbase_Utility_Localization {
34
35 /**
36 * @var string
37 */
38 protected static $locallangPath = 'Resources/Private/Language/';
39
40 /**
41 * Local Language content
42 *
43 * @var string
44 **/
45 protected static $LOCAL_LANG = array();
46
47 /**
48 * Local Language content charset for individual labels (overriding)
49 *
50 * @var string
51 **/
52 protected static $LOCAL_LANG_charset = array();
53
54 /**
55 * Key of the language to use
56 *
57 * @var string
58 **/
59 protected static $languageKey = 'default';
60
61 /**
62 * Pointer to alternative fall-back language to use
63 *
64 * @var string
65 **/
66 protected static $alternativeLanguageKey = '';
67
68 /**
69 * Returns the localized label of the LOCAL_LANG key, $key.
70 *
71 * @param string $key The key from the LOCAL_LANG array for which to return the value.
72 * @param string $extensionName The name of the extension
73 * @param array $arguments the arguments of the extension, being passed over to vsprintf
74 * @return string The value from LOCAL_LANG or NULL if no translation was found.
75 * @author Christopher Hlubek <hlubek@networkteam.com>
76 * @author Bastian Waidelich <bastian@typo3.org>
77 * @author Sebastian Kurfuerst <sebastian@typo3.org>
78 * @api
79 * @todo: If vsprintf gets a malformed string, it returns FALSE! Should we throw an exception there?
80 */
81 public function translate($key, $extensionName, $arguments = NULL) {
82 if (t3lib_div::isFirstPartOfStr($key, 'LLL:')) {
83 $value = self::translateFileReference($key);
84 } else {
85 self::initializeLocalization($extensionName);
86
87 // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
88 if (isset(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key])) {
89 $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key];
90 if (isset(self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key])) {
91 $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key]);
92 }
93 } elseif (self::$alternativeLanguageKey !== '' && isset(self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key])) {
94 $value = self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key];
95 if (isset(self::$LOCAL_LANG_charset[$extensionName][self::$alternativeLanguageKey][$key])) {
96 $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][self::$alternativeLanguageKey][$key]);
97 }
98 } elseif (isset(self::$LOCAL_LANG[$extensionName]['default'][$key])) {
99 $value = self::$LOCAL_LANG[$extensionName]['default'][$key]; // No charset conversion because default is english and thereby ASCII
100 }
101 }
102 if (is_array($arguments)) {
103 return vsprintf($value, $arguments);
104 } else {
105 return $value;
106 }
107 }
108
109 /**
110 * Returns the localized label of the LOCAL_LANG key, $key.
111 *
112 * @param string $key The language key including the path to a custom locallang file ("LLL:path:key").
113 * @return string The value from LOCAL_LANG or NULL if no translation was found.
114 * @see language::sL()
115 * @see tslib_fe::sL()
116 * @author Bastian Waidelich <bastian@typo3.org>
117 */
118 protected function translateFileReference($key) {
119 if (TYPO3_MODE === 'FE') {
120 return $GLOBALS['TSFE']->sL($key);
121 } elseif (is_object($GLOBALS['LANG'])) {
122 $value = $GLOBALS['LANG']->sL($key);
123 return $value !== '' ? $value : NULL;
124 } else {
125 return $key;
126 }
127 }
128
129 /**
130 * Loads local-language values by looking for a "locallang.php" (or "locallang.xml") file in the plugin resources directory and if found includes it.
131 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
132 *
133 * @return void
134 * @author Christopher Hlubek <hlubek@networkteam.com>
135 * @author Bastian Waidelich <bastian@typo3.org>
136 */
137 protected static function initializeLocalization($extensionName) {
138 if (isset(self::$LOCAL_LANG[$extensionName])) {
139 return;
140 }
141 $locallangPathAndFilename = 'EXT:' . t3lib_div::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xml';
142
143 self::setLanguageKeys();
144
145 $renderCharset = (TYPO3_MODE === 'FE' ? $GLOBALS['TSFE']->renderCharset : $GLOBALS['LANG']->charSet);
146 self::$LOCAL_LANG[$extensionName] = t3lib_div::readLLfile($locallangPathAndFilename, self::$languageKey, $renderCharset);
147 if (self::$alternativeLanguageKey !== '') {
148 $alternativeLocalLang = t3lib_div::readLLfile($locallangPathAndFilename, self::$alternativeLanguageKey);
149 self::$LOCAL_LANG[$extensionName] = array_merge(self::$LOCAL_LANG[$extensionName], $alternativeLocalLang);
150 }
151 self::loadTypoScriptLabels($extensionName);
152 }
153
154 /**
155 * Sets the currently active language/language_alt keys.
156 * Default values are "default" for language key and "" for language_alt key.
157 *
158 * @return void
159 * @author Christopher Hlubek <hlubek@networkteam.com>
160 * @author Bastian Waidelich <bastian@typo3.org>
161 */
162 protected function setLanguageKeys() {
163 self::$languageKey = 'default';
164 self::$alternativeLanguageKey = '';
165 if (TYPO3_MODE === 'FE') {
166 if (isset($GLOBALS['TSFE']->config['config']['language'])) {
167 self::$languageKey = $GLOBALS['TSFE']->config['config']['language'];
168 if (isset($GLOBALS['TSFE']->config['config']['language_alt'])) {
169 self::$alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
170 }
171 }
172 } elseif (strlen($GLOBALS['BE_USER']->uc['lang']) > 0) {
173 self::$languageKey = $GLOBALS['BE_USER']->uc['lang'];
174 }
175 }
176
177 /**
178 * Overwrites labels that are set via typoscript.
179 * TS locallang labels have to be configured like:
180 * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value
181 *
182 * @return void
183 * @author Christopher Hlubek <hlubek@networkteam.com>
184 * @author Bastian Waidelich <bastian@typo3.org>
185 */
186 protected function loadTypoScriptLabels($extensionName) {
187 $extbaseFrameworkConfiguration = Tx_Extbase_Dispatcher::getExtbaseFrameworkConfiguration();
188 if (!is_array($extbaseFrameworkConfiguration['_LOCAL_LANG'])) {
189 return;
190 }
191 foreach ($extbaseFrameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
192 if (!is_array($labels)) {
193 continue;
194 }
195 foreach($labels as $labelKey => $labelValue) {
196 if (is_string($labelValue)) {
197 self::$LOCAL_LANG[$extensionName][$languageKey][$labelKey] = $labelValue;
198 // For labels coming from the TypoScript (database) the charset is assumed to be "forceCharset" and if that is not set, assumed to be that of the individual system languages
199 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) && strlen($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) > 0) {
200 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
201 } elseif (is_object($GLOBALS['LANG'])) {
202 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['LANG']->csConvObj->charSetArray[$languageKey];
203 } else {
204 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['TSFE']->csConvObj->charSetArray[$languageKey];
205 }
206 }
207 }
208 }
209 }
210
211 /**
212 * Converts a string from the specified character set to the current.
213 * The current charset is defined by the TYPO3 mode.
214 *
215 * @param string $value string to be converted
216 * @param string $charset The source charset
217 * @return string converted string
218 */
219 protected function convertCharset($value, $charset) {
220 if (TYPO3_MODE === 'FE') {
221 return $GLOBALS['TSFE']->csConv($value, $charset);
222 } else {
223 $convertedValue = $GLOBALS['LANG']->csConvObj->conv($value, $GLOBALS['LANG']->csConvObj->parse_charset($charset), $GLOBALS['LANG']->charSet, 1);
224 return $convertedValue !== NULL ? $convertedValue : $value;
225 }
226 }
227 }
228 ?>