[BUGFIX] Make sure that language key is set in extbase.
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / LocalizationUtility.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Localization\Locales;
18 use TYPO3\CMS\Core\Localization\LocalizationFactory;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
21 use TYPO3\CMS\Extbase\Object\ObjectManager;
22
23 /**
24 * Localization helper which should be used to fetch localized labels.
25 *
26 * @api
27 */
28 class LocalizationUtility
29 {
30 /**
31 * @var string
32 */
33 protected static $locallangPath = 'Resources/Private/Language/';
34
35 /**
36 * Local Language content
37 *
38 * @var array
39 */
40 protected static $LOCAL_LANG = array();
41
42 /**
43 * Contains those LL keys, which have been set to (empty) in TypoScript.
44 * This is necessary, as we cannot distinguish between a nonexisting
45 * translation and a label that has been cleared by TS.
46 * In both cases ['key'][0]['target'] is "".
47 *
48 * @var array
49 */
50 protected static $LOCAL_LANG_UNSET = array();
51
52 /**
53 * Local Language content charset for individual labels (overriding)
54 *
55 * @var array
56 */
57 protected static $LOCAL_LANG_charset = array();
58
59 /**
60 * Key of the language to use
61 *
62 * @var string
63 */
64 protected static $languageKey = 'default';
65
66 /**
67 * Pointer to alternative fall-back language to use
68 *
69 * @var array
70 */
71 protected static $alternativeLanguageKeys = array();
72
73 /**
74 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
75 */
76 protected static $configurationManager = null;
77
78 /**
79 * Returns the localized label of the LOCAL_LANG key, $key.
80 *
81 * @param string $key The key from the LOCAL_LANG array for which to return the value.
82 * @param string $extensionName The name of the extension
83 * @param array $arguments the arguments of the extension, being passed over to vsprintf
84 * @return string|NULL The value from LOCAL_LANG or NULL if no translation was found.
85 * @api
86 * @todo : If vsprintf gets a malformed string, it returns FALSE! Should we throw an exception there?
87 */
88 public static function translate($key, $extensionName, $arguments = null)
89 {
90 $value = null;
91 if (GeneralUtility::isFirstPartOfStr($key, 'LLL:')) {
92 $value = self::translateFileReference($key);
93 } else {
94 self::initializeLocalization($extensionName);
95 // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
96 if (!empty(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'])
97 || isset(self::$LOCAL_LANG_UNSET[$extensionName][self::$languageKey][$key])
98 ) {
99 // Local language translation for key exists
100 $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'];
101 if (!empty(self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key])) {
102 $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key]);
103 }
104 } elseif (!empty(self::$alternativeLanguageKeys)) {
105 $languages = array_reverse(self::$alternativeLanguageKeys);
106 foreach ($languages as $language) {
107 if (!empty(self::$LOCAL_LANG[$extensionName][$language][$key][0]['target'])
108 || isset(self::$LOCAL_LANG_UNSET[$extensionName][$language][$key])
109 ) {
110 // Alternative language translation for key exists
111 $value = self::$LOCAL_LANG[$extensionName][$language][$key][0]['target'];
112 if (!empty(self::$LOCAL_LANG_charset[$extensionName][$language][$key])) {
113 $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][$language][$key]);
114 }
115 break;
116 }
117 }
118 }
119 if ($value === null && (!empty(self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'])
120 || isset(self::$LOCAL_LANG_UNSET[$extensionName]['default'][$key]))
121 ) {
122 // Default language translation for key exists
123 // No charset conversion because default is English and thereby ASCII
124 $value = self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'];
125 }
126 }
127 if (is_array($arguments) && $value !== null) {
128 return vsprintf($value, $arguments);
129 } else {
130 return $value;
131 }
132 }
133
134 /**
135 * Returns the localized label of the LOCAL_LANG key, $key.
136 *
137 * @param string $key The language key including the path to a custom locallang file ("LLL:path:key").
138 * @return string The value from LOCAL_LANG or NULL if no translation was found.
139 * @see language::sL()
140 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::sL()
141 */
142 protected static function translateFileReference($key)
143 {
144 if (TYPO3_MODE === 'FE') {
145 $value = self::getTypoScriptFrontendController()->sL($key);
146 return $value !== false ? $value : null;
147 } elseif (is_object($GLOBALS['LANG'])) {
148 $value = self::getLanguageService()->sL($key);
149 return $value !== '' ? $value : null;
150 } else {
151 return $key;
152 }
153 }
154
155 /**
156 * Loads local-language values by looking for a "locallang.xlf" (or "locallang.xml") file in the plugin resources directory and if found includes it.
157 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.xlf" file.
158 *
159 * @param string $extensionName
160 * @return void
161 */
162 protected static function initializeLocalization($extensionName)
163 {
164 if (isset(self::$LOCAL_LANG[$extensionName])) {
165 return;
166 }
167 $locallangPathAndFilename = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xlf';
168 self::setLanguageKeys();
169 $renderCharset = TYPO3_MODE === 'FE' ? self::getTypoScriptFrontendController()->renderCharset : self::getLanguageService()->charSet;
170
171 /** @var $languageFactory LocalizationFactory */
172 $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
173
174 self::$LOCAL_LANG[$extensionName] = $languageFactory->getParsedData($locallangPathAndFilename, self::$languageKey, $renderCharset);
175 foreach (self::$alternativeLanguageKeys as $language) {
176 $tempLL = $languageFactory->getParsedData($locallangPathAndFilename, $language, $renderCharset);
177 if (self::$languageKey !== 'default' && isset($tempLL[$language])) {
178 self::$LOCAL_LANG[$extensionName][$language] = $tempLL[$language];
179 }
180 }
181 self::loadTypoScriptLabels($extensionName);
182 }
183
184 /**
185 * Sets the currently active language/language_alt keys.
186 * Default values are "default" for language key and "" for language_alt key.
187 *
188 * @return void
189 */
190 protected static function setLanguageKeys()
191 {
192 self::$languageKey = 'default';
193 self::$alternativeLanguageKeys = array();
194 if (TYPO3_MODE === 'FE') {
195 if (isset(self::getTypoScriptFrontendController()->config['config']['language'])) {
196 self::$languageKey = self::getTypoScriptFrontendController()->config['config']['language'];
197 if (isset(self::getTypoScriptFrontendController()->config['config']['language_alt'])) {
198 self::$alternativeLanguageKeys[] = self::getTypoScriptFrontendController()->config['config']['language_alt'];
199 } else {
200 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
201 $locales = GeneralUtility::makeInstance(Locales::class);
202 if (in_array(self::$languageKey, $locales->getLocales())) {
203 foreach ($locales->getLocaleDependencies(self::$languageKey) as $language) {
204 self::$alternativeLanguageKeys[] = $language;
205 }
206 }
207 }
208 }
209 } elseif (!empty($GLOBALS['BE_USER']->uc['lang'])) {
210 self::$languageKey = $GLOBALS['BE_USER']->uc['lang'];
211 } elseif (!empty(self::getLanguageService()->lang)) {
212 self::$languageKey = self::getLanguageService()->lang;
213 }
214 }
215
216 /**
217 * Overwrites labels that are set via TypoScript.
218 * TS locallang labels have to be configured like:
219 * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value
220 *
221 * @param string $extensionName
222 * @return void
223 */
224 protected static function loadTypoScriptLabels($extensionName)
225 {
226 $configurationManager = static::getConfigurationManager();
227 $frameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
228 if (!is_array($frameworkConfiguration['_LOCAL_LANG'])) {
229 return;
230 }
231 self::$LOCAL_LANG_UNSET[$extensionName] = array();
232 foreach ($frameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
233 if (!(is_array($labels) && isset(self::$LOCAL_LANG[$extensionName][$languageKey]))) {
234 continue;
235 }
236 foreach ($labels as $labelKey => $labelValue) {
237 if (is_string($labelValue)) {
238 self::$LOCAL_LANG[$extensionName][$languageKey][$labelKey][0]['target'] = $labelValue;
239 if ($labelValue === '') {
240 self::$LOCAL_LANG_UNSET[$extensionName][$languageKey][$labelKey] = '';
241 }
242 if (is_object($GLOBALS['LANG'])) {
243 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = self::getLanguageService()->csConvObj->charSetArray[$languageKey];
244 } else {
245 self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = self::getTypoScriptFrontendController()->csConvObj->charSetArray[$languageKey];
246 }
247 } elseif (is_array($labelValue)) {
248 $labelValue = self::flattenTypoScriptLabelArray($labelValue, $labelKey);
249 foreach ($labelValue as $key => $value) {
250 self::$LOCAL_LANG[$extensionName][$languageKey][$key][0]['target'] = $value;
251 if ($value === '') {
252 self::$LOCAL_LANG_UNSET[$extensionName][$languageKey][$key] = '';
253 }
254 }
255 }
256 }
257 }
258 }
259
260 /**
261 * Flatten TypoScript label array; converting a hierarchical array into a flat
262 * array with the keys separated by dots.
263 *
264 * Example Input: array('k1' => array('subkey1' => 'val1'))
265 * Example Output: array('k1.subkey1' => 'val1')
266 *
267 * @param array $labelValues Hierarchical array of labels
268 * @param string $parentKey the name of the parent key in the recursion; is only needed for recursion.
269 * @return array flattened array of labels.
270 */
271 protected static function flattenTypoScriptLabelArray(array $labelValues, $parentKey = '')
272 {
273 $result = array();
274 foreach ($labelValues as $key => $labelValue) {
275 if (!empty($parentKey)) {
276 $key = $parentKey . '.' . $key;
277 }
278 if (is_array($labelValue)) {
279 $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key);
280 $result = array_merge($result, $labelValue);
281 } else {
282 $result[$key] = $labelValue;
283 }
284 }
285 return $result;
286 }
287
288 /**
289 * Converts a string from the specified character set to the current.
290 * The current charset is defined by the TYPO3 mode.
291 *
292 * @param string $value string to be converted
293 * @param string $charset The source charset
294 * @return string converted string
295 */
296 protected static function convertCharset($value, $charset)
297 {
298 if (TYPO3_MODE === 'FE') {
299 return self::getTypoScriptFrontendController()->csConv($value, $charset);
300 } else {
301 $convertedValue = self::getLanguageService()->csConvObj->conv($value, self::getLanguageService()->csConvObj->parse_charset($charset), self::getLanguageService()->charSet, 1);
302 return $convertedValue !== null ? $convertedValue : $value;
303 }
304 }
305
306 /**
307 * Returns instance of the configuration manager
308 *
309 * @return \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
310 */
311 protected static function getConfigurationManager()
312 {
313 if (!is_null(static::$configurationManager)) {
314 return static::$configurationManager;
315 }
316 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
317 $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
318 static::$configurationManager = $configurationManager;
319 return $configurationManager;
320 }
321
322 /**
323 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
324 */
325 protected static function getTypoScriptFrontendController()
326 {
327 return $GLOBALS['TSFE'];
328 }
329
330 /**
331 * @return \TYPO3\CMS\Lang\LanguageService
332 */
333 protected static function getLanguageService()
334 {
335 return $GLOBALS['LANG'];
336 }
337 }