TYPO3  7.6
LocalizationUtility.php
Go to the documentation of this file.
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 
22 
29 {
33  protected static $locallangPath = 'Resources/Private/Language/';
34 
40  protected static $LOCAL_LANG = array();
41 
50  protected static $LOCAL_LANG_UNSET = array();
51 
57  protected static $LOCAL_LANG_charset = array();
58 
64  protected static $languageKey = 'default';
65 
71  protected static $alternativeLanguageKeys = array();
72 
76  protected static $configurationManager = null;
77 
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 
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 
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 
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 
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 {
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 
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 
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 
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 
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 
325  protected static function getTypoScriptFrontendController()
326  {
327  return $GLOBALS['TSFE'];
328  }
329 
333  protected static function getLanguageService()
334  {
335  return $GLOBALS['LANG'];
336  }
337 }