TYPO3 CMS  TYPO3_7-6
LocalizationUtility.php
Go to the documentation of this file.
1 <?php
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  $renderCharset = TYPO3_MODE === 'FE' ? self::getTypoScriptFrontendController()->renderCharset : self::getLanguageService()->charSet;
157 
159  $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
160 
161  self::$LOCAL_LANG[$extensionName] = $languageFactory->getParsedData($locallangPathAndFilename, self::$languageKey, $renderCharset);
162  foreach (self::$alternativeLanguageKeys as $language) {
163  $tempLL = $languageFactory->getParsedData($locallangPathAndFilename, $language, $renderCharset);
164  if (self::$languageKey !== 'default' && isset($tempLL[$language])) {
165  self::$LOCAL_LANG[$extensionName][$language] = $tempLL[$language];
166  }
167  }
168  self::loadTypoScriptLabels($extensionName);
169  }
170 
177  protected static function setLanguageKeys()
178  {
179  self::$languageKey = 'default';
180  self::$alternativeLanguageKeys = [];
181  if (TYPO3_MODE === 'FE') {
182  if (isset(self::getTypoScriptFrontendController()->config['config']['language'])) {
183  self::$languageKey = self::getTypoScriptFrontendController()->config['config']['language'];
184  if (isset(self::getTypoScriptFrontendController()->config['config']['language_alt'])) {
185  self::$alternativeLanguageKeys[] = self::getTypoScriptFrontendController()->config['config']['language_alt'];
186  } else {
188  $locales = GeneralUtility::makeInstance(Locales::class);
189  if (in_array(self::$languageKey, $locales->getLocales())) {
190  foreach ($locales->getLocaleDependencies(self::$languageKey) as $language) {
191  self::$alternativeLanguageKeys[] = $language;
192  }
193  }
194  }
195  }
196  } elseif (!empty($GLOBALS['BE_USER']->uc['lang'])) {
197  self::$languageKey = $GLOBALS['BE_USER']->uc['lang'];
198  } elseif (!empty(self::getLanguageService()->lang)) {
199  self::$languageKey = self::getLanguageService()->lang;
200  }
201  }
202 
211  protected static function loadTypoScriptLabels($extensionName)
212  {
213  $configurationManager = static::getConfigurationManager();
214  $frameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
215  if (!is_array($frameworkConfiguration['_LOCAL_LANG'])) {
216  return;
217  }
218  self::$LOCAL_LANG_UNSET[$extensionName] = [];
219  foreach ($frameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
220  if (!(is_array($labels) && isset(self::$LOCAL_LANG[$extensionName][$languageKey]))) {
221  continue;
222  }
223  foreach ($labels as $labelKey => $labelValue) {
224  if (is_string($labelValue)) {
225  self::$LOCAL_LANG[$extensionName][$languageKey][$labelKey][0]['target'] = $labelValue;
226  if ($labelValue === '') {
227  self::$LOCAL_LANG_UNSET[$extensionName][$languageKey][$labelKey] = '';
228  }
229  } elseif (is_array($labelValue)) {
230  $labelValue = self::flattenTypoScriptLabelArray($labelValue, $labelKey);
231  foreach ($labelValue as $key => $value) {
232  self::$LOCAL_LANG[$extensionName][$languageKey][$key][0]['target'] = $value;
233  if ($value === '') {
234  self::$LOCAL_LANG_UNSET[$extensionName][$languageKey][$key] = '';
235  }
236  }
237  }
238  }
239  }
240  }
241 
253  protected static function flattenTypoScriptLabelArray(array $labelValues, $parentKey = '')
254  {
255  $result = [];
256  foreach ($labelValues as $key => $labelValue) {
257  if (!empty($parentKey)) {
258  $key = $parentKey . '.' . $key;
259  }
260  if (is_array($labelValue)) {
261  $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key);
262  $result = array_merge($result, $labelValue);
263  } else {
264  $result[$key] = $labelValue;
265  }
266  }
267  return $result;
268  }
269 
275  protected static function getConfigurationManager()
276  {
277  if (!is_null(static::$configurationManager)) {
278  return static::$configurationManager;
279  }
280  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
281  $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
282  static::$configurationManager = $configurationManager;
283  return $configurationManager;
284  }
285 
289  protected static function getTypoScriptFrontendController()
290  {
291  return $GLOBALS['TSFE'];
292  }
293 
297  protected static function getLanguageService()
298  {
299  return $GLOBALS['LANG'];
300  }
301 }
static translate($key, $extensionName, $arguments=null)
static isFirstPartOfStr($str, $partStr)
static flattenTypoScriptLabelArray(array $labelValues, $parentKey='')
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
$locales
Definition: be_users.php:6