[TASK] Remove superfluous parenthesis in sysexts
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / LocalizationUtility.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009 Sebastian Kurf├╝rst <sebastian@typo3.org>
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 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Localization helper which should be used to fetch localized labels.
28 *
29 * @package Extbase
30 * @subpackage Utility
31 * @version $ID:$
32 * @api
33 */
34 class LocalizationUtility {
35
36 /**
37 * @var string
38 */
39 static protected $locallangPath = 'Resources/Private/Language/';
40
41 /**
42 * Local Language content
43 *
44 * @var string
45 */
46 static protected $LOCAL_LANG = array();
47
48 /**
49 * Local Language content charset for individual labels (overriding)
50 *
51 * @var string
52 */
53 static protected $LOCAL_LANG_charset = array();
54
55 /**
56 * Key of the language to use
57 *
58 * @var string
59 */
60 static protected $languageKey = 'default';
61
62 /**
63 * Pointer to alternative fall-back language to use
64 *
65 * @var string
66 */
67 static protected $alternativeLanguageKey = '';
68
69 /**
70 * Returns the localized label of the LOCAL_LANG key, $key.
71 *
72 * @param string $key The key from the LOCAL_LANG array for which to return the value.
73 * @param string $extensionName The name of the extension
74 * @param array $arguments the arguments of the extension, being passed over to vsprintf
75 * @return string|NULL The value from LOCAL_LANG or NULL if no translation was found.
76 * @author Christopher Hlubek <hlubek@networkteam.com>
77 * @author Bastian Waidelich <bastian@typo3.org>
78 * @author Sebastian Kurfuerst <sebastian@typo3.org>
79 * @api
80 * @todo : If vsprintf gets a malformed string, it returns FALSE! Should we throw an exception there?
81 */
82 static public function translate($key, $extensionName, $arguments = NULL) {
83 $value = NULL;
84 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($key, 'LLL:')) {
85 $value = self::translateFileReference($key);
86 } else {
87 self::initializeLocalization($extensionName);
88 // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
89 if (isset(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key])) {
90 if (is_array(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key])) {
91 // TYPO3 >= 4.6
92 $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'];
93 } else {
94 // TYPO3 < 4.6
95 $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key];
96 }
97 if (isset(self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key])) {
98 $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key]);
99 }
100 } elseif (self::$alternativeLanguageKey !== '' && isset(self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key])) {
101 if (is_array(self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key])) {
102 // TYPO3 >= 4.6
103 $value = self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key][0]['target'];
104 } else {
105 // TYPO3 < 4.6
106 $value = self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key];
107 }
108 if (isset(self::$LOCAL_LANG_charset[$extensionName][self::$alternativeLanguageKey][$key])) {
109 $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][self::$alternativeLanguageKey][$key]);
110 }
111 } elseif (isset(self::$LOCAL_LANG[$extensionName]['default'][$key])) {
112 // No charset conversion because default is English and thereby ASCII
113 if (is_array(self::$LOCAL_LANG[$extensionName]['default'][$key])) {
114 // TYPO3 >= 4.6
115 $value = self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'];
116 } else {
117 // TYPO3 < 4.6
118 $value = self::$LOCAL_LANG[$extensionName]['default'][$key];
119 }
120 }
121 }
122 if (is_array($arguments) && $value !== NULL) {
123 return vsprintf($value, $arguments);
124 } else {
125 return $value;
126 }
127 }
128
129 /**
130 * Returns the localized label of the LOCAL_LANG key, $key.
131 *
132 * @param string $key The language key including the path to a custom locallang file ("LLL:path:key").
133 * @return string The value from LOCAL_LANG or NULL if no translation was found.
134 * @see language::sL()
135 * @see tslib_fe::sL()
136 * @author Bastian Waidelich <bastian@typo3.org>
137 */
138 static protected function translateFileReference($key) {
139 if (TYPO3_MODE === 'FE') {
140 $value = $GLOBALS['TSFE']->sL($key);
141 return $value !== FALSE ? $value : NULL;
142 } elseif (is_object($GLOBALS['LANG'])) {
143 $value = $GLOBALS['LANG']->sL($key);
144 return $value !== '' ? $value : NULL;
145 } else {
146 return $key;
147 }
148 }
149
150 /**
151 * Loads local-language values by looking for a "locallang.php" (or "locallang.xml") file in the plugin resources directory and if found includes it.
152 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
153 *
154 * @param string $extensionName
155 * @return void
156 * @author Christopher Hlubek <hlubek@networkteam.com>
157 * @author Bastian Waidelich <bastian@typo3.org>
158 */
159 static protected function initializeLocalization($extensionName) {
160 if (isset(self::$LOCAL_LANG[$extensionName])) {
161 return;
162 }
163 $locallangPathAndFilename = 'EXT:' . \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xml';
164 self::setLanguageKeys();
165 $renderCharset = TYPO3_MODE === 'FE' ? $GLOBALS['TSFE']->renderCharset : $GLOBALS['LANG']->charSet;
166 self::$LOCAL_LANG[$extensionName] = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($locallangPathAndFilename, self::$languageKey, $renderCharset);
167 if (self::$alternativeLanguageKey !== '') {
168 $alternativeLocalLang = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($locallangPathAndFilename, self::$alternativeLanguageKey);
169 self::$LOCAL_LANG[$extensionName] = array_merge(self::$LOCAL_LANG[$extensionName], $alternativeLocalLang);
170 }
171 self::loadTypoScriptLabels($extensionName);
172 }
173
174 /**
175 * Sets the currently active language/language_alt keys.
176 * Default values are "default" for language key and "" for language_alt key.
177 *
178 * @return void
179 * @author Christopher Hlubek <hlubek@networkteam.com>
180 * @author Bastian Waidelich <bastian@typo3.org>
181 */
182 protected function setLanguageKeys() {
183 self::$languageKey = 'default';
184 self::$alternativeLanguageKey = '';
185 if (TYPO3_MODE === 'FE') {
186 if (isset($GLOBALS['TSFE']->config['config']['language'])) {
187 self::$languageKey = $GLOBALS['TSFE']->config['config']['language'];
188 if (isset($GLOBALS['TSFE']->config['config']['language_alt'])) {
189 self::$alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
190 }
191 }
192 } elseif (strlen($GLOBALS['BE_USER']->uc['lang']) > 0) {
193 self::$languageKey = $GLOBALS['BE_USER']->uc['lang'];
194 }
195 }
196
197 /**
198 * Overwrites labels that are set via typoscript.
199 * TS locallang labels have to be configured like:
200 * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value
201 *
202 * @param string $extensionName
203 * @return void
204 * @author Christopher Hlubek <hlubek@networkteam.com>
205 * @author Bastian Waidelich <bastian@typo3.org>
206 */
207 protected function loadTypoScriptLabels($extensionName) {
208 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
209 $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
210 $frameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
211 if (!is_array($frameworkConfiguration['_LOCAL_LANG'])) {
212 return;
213 }
214 foreach ($frameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
215 if (!(is_array($labels) && isset(self::$LOCAL_LANG[$extensionName][$languageKey]))) {
216 continue;
217 }
218 foreach ($labels as $labelKey => $labelValue) {
219 if (is_string($labelValue)) {
220 self::$LOCAL_LANG[$extensionName][$languageKey][$labelKey] = $labelValue;
221 // 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
222 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) && strlen($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) > 0) {
223 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
224 } elseif (is_object($GLOBALS['LANG'])) {
225 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['LANG']->csConvObj->charSetArray[$languageKey];
226 } else {
227 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['TSFE']->csConvObj->charSetArray[$languageKey];
228 }
229 } elseif (is_array($labelValue)) {
230 $labelValue = self::flattenTypoScriptLabelArray($labelValue, $labelKey);
231 self::$LOCAL_LANG[$extensionName][$languageKey] = array_merge(self::$LOCAL_LANG[$extensionName][$languageKey], $labelValue);
232 }
233 }
234 }
235 }
236
237 /**
238 * Flatten TypoScript label array; converting a hierarchical array into a flat
239 * array with the keys separated by dots.
240 *
241 * Example Input: array('k1' => array('subkey1' => 'val1'))
242 * Example Output: array('k1.subkey1' => 'val1')
243 *
244 * @param array $labelValues Hierarchical array of labels
245 * @param string $parentKey the name of the parent key in the recursion; is only needed for recursion.
246 * @return array flattened array of labels.
247 */
248 protected function flattenTypoScriptLabelArray(array $labelValues, $parentKey = '') {
249 $result = array();
250 foreach ($labelValues as $key => $labelValue) {
251 if (!empty($parentKey)) {
252 $key = $parentKey . '.' . $key;
253 }
254 if (is_array($labelValue)) {
255 $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key);
256 $result = array_merge($result, $labelValue);
257 } else {
258 $result[$key] = $labelValue;
259 }
260 }
261 return $result;
262 }
263
264 /**
265 * Converts a string from the specified character set to the current.
266 * The current charset is defined by the TYPO3 mode.
267 *
268 * @param string $value string to be converted
269 * @param string $charset The source charset
270 * @return string converted string
271 */
272 protected function convertCharset($value, $charset) {
273 if (TYPO3_MODE === 'FE') {
274 return $GLOBALS['TSFE']->csConv($value, $charset);
275 } else {
276 $convertedValue = $GLOBALS['LANG']->csConvObj->conv($value, $GLOBALS['LANG']->csConvObj->parse_charset($charset), $GLOBALS['LANG']->charSet, 1);
277 return $convertedValue !== NULL ? $convertedValue : $value;
278 }
279 }
280
281 }
282
283
284 ?>