Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[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 (is_object($GLOBALS['LANG'])) {
120 $value = $GLOBALS['LANG']->sL($key);
121 return $value !== '' ? $value : NULL;
122 }
123 return $GLOBALS['TSFE']->sL($key);
124 }
125
126
127 /**
128 * Loads local-language values by looking for a "locallang.php" (or "locallang.xml") file in the plugin resources directory and if found includes it.
129 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
130 *
131 * @return void
132 * @author Christopher Hlubek <hlubek@networkteam.com>
133 * @author Bastian Waidelich <bastian@typo3.org>
134 */
135 protected static function initializeLocalization($extensionName) {
136 if (isset(self::$LOCAL_LANG[$extensionName])) {
137 return;
138 }
139 $locallangPathAndFilename = t3lib_extMgm::extPath(t3lib_div::camelCaseToLowerCaseUnderscored($extensionName), self::$locallangPath . 'locallang.php');
140
141 self::setLanguageKeys();
142
143 $renderCharset = (TYPO3_MODE === 'FE' ? $GLOBALS['TSFE']->renderCharset : $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']);
144 self::$LOCAL_LANG[$extensionName] = t3lib_div::readLLfile($locallangPathAndFilename, self::$languageKey, $renderCharset);
145 if (self::$alternativeLanguageKey !== '') {
146 $alternativeLocalLang = t3lib_div::readLLfile($locallangPathAndFilename, self::$alternativeLanguageKey);
147 self::$LOCAL_LANG[$extensionName] = array_merge(self::$LOCAL_LANG[$extensionName], $alternativeLocalLang);
148 }
149 self::loadTypoScriptLabels($extensionName);
150 }
151
152 /**
153 * Sets the currently active language/language_alt keys.
154 * Default values are "default" for language key and "" for language_alt key.
155 *
156 * @return void
157 * @author Christopher Hlubek <hlubek@networkteam.com>
158 * @author Bastian Waidelich <bastian@typo3.org>
159 */
160 protected function setLanguageKeys() {
161 self::$languageKey = 'default';
162 self::$alternativeLanguageKey = '';
163 if (TYPO3_MODE === 'FE') {
164 if (isset($GLOBALS['TSFE']->config['config']['language'])) {
165 self::$languageKey = $GLOBALS['TSFE']->config['config']['language'];
166 if (isset($GLOBALS['TSFE']->config['config']['language_alt'])) {
167 self::$alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
168 }
169 }
170 } elseif (strlen($GLOBALS['BE_USER']->uc['lang']) > 0) {
171 self::$languageKey = $GLOBALS['BE_USER']->uc['lang'];
172 }
173 }
174
175 /**
176 * Overwrites labels that are set via typoscript.
177 * TS locallang labels have to be configured like:
178 * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value
179 *
180 * @return void
181 * @author Christopher Hlubek <hlubek@networkteam.com>
182 * @author Bastian Waidelich <bastian@typo3.org>
183 */
184 protected function loadTypoScriptLabels($extensionName) {
185 $extbaseFrameworkConfiguration = Tx_Extbase_Dispatcher::getExtbaseFrameworkConfiguration();
186 if (!is_array($extbaseFrameworkConfiguration['_LOCAL_LANG'])) {
187 return;
188 }
189 foreach ($extbaseFrameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
190 if (!is_array($labels)) {
191 continue;
192 }
193 foreach($labels as $labelKey => $labelValue) {
194 if (is_string($labelValue)) {
195 self::$LOCAL_LANG[$extensionName][$languageKey][$labelKey] = $labelValue;
196 // 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
197 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) && strlen($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) > 0) {
198 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
199 } elseif (is_object($GLOBALS['LANG'])) {
200 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['LANG']->csConvObj->charSetArray[$languageKey];
201 } else {
202 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['TSFE']->csConvObj->charSetArray[$languageKey];
203 }
204 }
205 }
206 }
207 }
208
209 /**
210 * Converts a string to the specified charset
211 *
212 * @param string $value string to be converted
213 * @param string $charset charset
214 * @return string converted string
215 */
216 protected function convertCharset($value, $charset) {
217 if (TYPO3_MODE === 'FE') {
218 return $GLOBALS['TSFE']->csConv($value, $charset);
219 } else {
220 $covertedValue = $GLOBALS['LANG']->csConvObj->conv($value, $GLOBALS['LANG']->csConvObj->parse_charset($charset), $GLOBALS['LANG']->renderCharset, 1);
221 return $covertedValue !== NULL ? $covertedValue : $value;
222 }
223 }
224 }
225 ?>