TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 = [];
41 
50  protected static $LOCAL_LANG_UNSET = [];
51 
57  protected static $languageKey = 'default';
58 
64  protected static $alternativeLanguageKeys = [];
65 
69  protected static $configurationManager = null;
70 
81  public static function translate($key, $extensionName, $arguments = null)
82  {
83  $value = null;
84  if (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 (!empty(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'])
90  || isset(self::$LOCAL_LANG_UNSET[$extensionName][self::$languageKey][$key])
91  ) {
92  // Local language translation for key exists
93  $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'];
94  } elseif (!empty(self::$alternativeLanguageKeys)) {
95  $languages = array_reverse(self::$alternativeLanguageKeys);
96  foreach ($languages as $language) {
97  if (!empty(self::$LOCAL_LANG[$extensionName][$language][$key][0]['target'])
98  || isset(self::$LOCAL_LANG_UNSET[$extensionName][$language][$key])
99  ) {
100  // Alternative language translation for key exists
101  $value = self::$LOCAL_LANG[$extensionName][$language][$key][0]['target'];
102  break;
103  }
104  }
105  }
106  if ($value === null && (!empty(self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'])
107  || isset(self::$LOCAL_LANG_UNSET[$extensionName]['default'][$key]))
108  ) {
109  // Default language translation for key exists
110  // No charset conversion because default is English and thereby ASCII
111  $value = self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'];
112  }
113  }
114  if (is_array($arguments) && $value !== null) {
115  return vsprintf($value, $arguments);
116  } else {
117  return $value;
118  }
119  }
120 
129  protected static function translateFileReference($key)
130  {
131  if (TYPO3_MODE === 'FE') {
132  $value = self::getTypoScriptFrontendController()->sL($key);
133  return $value !== false ? $value : null;
134  } elseif (is_object($GLOBALS['LANG'])) {
135  $value = self::getLanguageService()->sL($key);
136  return $value !== '' ? $value : null;
137  } else {
138  return $key;
139  }
140  }
141 
149  protected static function initializeLocalization($extensionName)
150  {
151  if (isset(self::$LOCAL_LANG[$extensionName])) {
152  return;
153  }
154  $locallangPathAndFilename = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xlf';
155  self::setLanguageKeys();
156 
158  $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
159 
160  self::$LOCAL_LANG[$extensionName] = $languageFactory->getParsedData($locallangPathAndFilename, self::$languageKey, 'utf-8');
161  foreach (self::$alternativeLanguageKeys as $language) {
162  $tempLL = $languageFactory->getParsedData($locallangPathAndFilename, $language, 'utf-8');
163  if (self::$languageKey !== 'default' && isset($tempLL[$language])) {
164  self::$LOCAL_LANG[$extensionName][$language] = $tempLL[$language];
165  }
166  }
167  self::loadTypoScriptLabels($extensionName);
168  }
169 
176  protected static function setLanguageKeys()
177  {
178  self::$languageKey = 'default';
179  self::$alternativeLanguageKeys = [];
180  if (TYPO3_MODE === 'FE') {
181  if (isset(self::getTypoScriptFrontendController()->config['config']['language'])) {
182  self::$languageKey = self::getTypoScriptFrontendController()->config['config']['language'];
183  if (isset(self::getTypoScriptFrontendController()->config['config']['language_alt'])) {
184  self::$alternativeLanguageKeys[] = self::getTypoScriptFrontendController()->config['config']['language_alt'];
185  } else {
187  $locales = GeneralUtility::makeInstance(Locales::class);
188  if (in_array(self::$languageKey, $locales->getLocales())) {
189  foreach ($locales->getLocaleDependencies(self::$languageKey) as $language) {
190  self::$alternativeLanguageKeys[] = $language;
191  }
192  }
193  }
194  }
195  } elseif (!empty($GLOBALS['BE_USER']->uc['lang'])) {
196  self::$languageKey = $GLOBALS['BE_USER']->uc['lang'];
197  } elseif (!empty(self::getLanguageService()->lang)) {
198  self::$languageKey = self::getLanguageService()->lang;
199  }
200  }
201 
210  protected static function loadTypoScriptLabels($extensionName)
211  {
212  $configurationManager = static::getConfigurationManager();
213  $frameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
214  if (!is_array($frameworkConfiguration['_LOCAL_LANG'])) {
215  return;
216  }
217  self::$LOCAL_LANG_UNSET[$extensionName] = [];
218  foreach ($frameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
219  if (!(is_array($labels) && isset(self::$LOCAL_LANG[$extensionName][$languageKey]))) {
220  continue;
221  }
222  foreach ($labels as $labelKey => $labelValue) {
223  if (is_string($labelValue)) {
224  self::$LOCAL_LANG[$extensionName][$languageKey][$labelKey][0]['target'] = $labelValue;
225  if ($labelValue === '') {
226  self::$LOCAL_LANG_UNSET[$extensionName][$languageKey][$labelKey] = '';
227  }
228  } elseif (is_array($labelValue)) {
229  $labelValue = self::flattenTypoScriptLabelArray($labelValue, $labelKey);
230  foreach ($labelValue as $key => $value) {
231  self::$LOCAL_LANG[$extensionName][$languageKey][$key][0]['target'] = $value;
232  if ($value === '') {
233  self::$LOCAL_LANG_UNSET[$extensionName][$languageKey][$key] = '';
234  }
235  }
236  }
237  }
238  }
239  }
240 
252  protected static function flattenTypoScriptLabelArray(array $labelValues, $parentKey = '')
253  {
254  $result = [];
255  foreach ($labelValues as $key => $labelValue) {
256  if (!empty($parentKey)) {
257  $key = $parentKey . '.' . $key;
258  }
259  if (is_array($labelValue)) {
260  $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key);
261  $result = array_merge($result, $labelValue);
262  } else {
263  $result[$key] = $labelValue;
264  }
265  }
266  return $result;
267  }
268 
274  protected static function getConfigurationManager()
275  {
276  if (!is_null(static::$configurationManager)) {
277  return static::$configurationManager;
278  }
279  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
280  $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
281  static::$configurationManager = $configurationManager;
282  return $configurationManager;
283  }
284 
288  protected static function getTypoScriptFrontendController()
289  {
290  return $GLOBALS['TSFE'];
291  }
292 
296  protected static function getLanguageService()
297  {
298  return $GLOBALS['LANG'];
299  }
300 }
static isFirstPartOfStr($str, $partStr)
static translate($key, $extensionName, $arguments=null)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
static flattenTypoScriptLabelArray(array $labelValues, $parentKey= '')