TYPO3 CMS  TYPO3_6-2
LocalizationUtility.php
Go to the documentation of this file.
1 <?php
3 
22 
26  static protected $locallangPath = 'Resources/Private/Language/';
27 
33  static protected $LOCAL_LANG = array();
34 
43  static protected $LOCAL_LANG_UNSET = array();
44 
50  static protected $LOCAL_LANG_charset = array();
51 
57  static protected $languageKey = 'default';
58 
64  static protected $alternativeLanguageKeys = array();
65 
69  static protected $configurationManager = NULL;
70 
81  static public function translate($key, $extensionName, $arguments = NULL) {
82  $value = NULL;
83  if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($key, 'LLL:')) {
84  $value = self::translateFileReference($key);
85  } else {
86  self::initializeLocalization($extensionName);
87  // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
88  if (!empty(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'])
89  || isset(self::$LOCAL_LANG_UNSET[$extensionName][self::$languageKey][$key])
90  ) {
91  // Local language translation for key exists
92  $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'];
93  if (!empty(self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key])) {
94  $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key]);
95  }
96  } elseif (count(self::$alternativeLanguageKeys)) {
97  $languages = array_reverse(self::$alternativeLanguageKeys);
98  foreach ($languages as $language) {
99  if (!empty(self::$LOCAL_LANG[$extensionName][$language][$key][0]['target'])
100  || isset(self::$LOCAL_LANG_UNSET[$extensionName][$language][$key])
101  ) {
102  // Alternative language translation for key exists
103  $value = self::$LOCAL_LANG[$extensionName][$language][$key][0]['target'];
104  if (!empty(self::$LOCAL_LANG_charset[$extensionName][$language][$key])) {
105  $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][$language][$key]);
106  }
107  break;
108  }
109  }
110  }
111  if ($value === NULL && (!empty(self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'])
112  || isset(self::$LOCAL_LANG_UNSET[$extensionName]['default'][$key]))
113  ) {
114  // Default language translation for key exists
115  // No charset conversion because default is English and thereby ASCII
116  $value = self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'];
117  }
118  }
119  if (is_array($arguments) && $value !== NULL) {
120  return vsprintf($value, $arguments);
121  } else {
122  return $value;
123  }
124  }
125 
134  static protected function translateFileReference($key) {
135  if (TYPO3_MODE === 'FE') {
136  $value = $GLOBALS['TSFE']->sL($key);
137  return $value !== FALSE ? $value : NULL;
138  } elseif (is_object($GLOBALS['LANG'])) {
139  $value = $GLOBALS['LANG']->sL($key);
140  return $value !== '' ? $value : NULL;
141  } else {
142  return $key;
143  }
144  }
145 
153  static protected function initializeLocalization($extensionName) {
154  if (isset(self::$LOCAL_LANG[$extensionName])) {
155  return;
156  }
157  $locallangPathAndFilename = 'EXT:' . \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xml';
158  self::setLanguageKeys();
159  $renderCharset = TYPO3_MODE === 'FE' ? $GLOBALS['TSFE']->renderCharset : $GLOBALS['LANG']->charSet;
160  self::$LOCAL_LANG[$extensionName] = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($locallangPathAndFilename, self::$languageKey, $renderCharset);
161  foreach (self::$alternativeLanguageKeys as $language) {
162  $tempLL = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($locallangPathAndFilename, $language, $renderCharset);
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  static protected function setLanguageKeys() {
177  self::$languageKey = 'default';
178  self::$alternativeLanguageKeys = array();
179  if (TYPO3_MODE === 'FE') {
180  if (isset($GLOBALS['TSFE']->config['config']['language'])) {
181  self::$languageKey = $GLOBALS['TSFE']->config['config']['language'];
182  if (isset($GLOBALS['TSFE']->config['config']['language_alt'])) {
183  self::$alternativeLanguageKeys[] = $GLOBALS['TSFE']->config['config']['language_alt'];
184  } else {
186  $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
187  if (in_array(self::$languageKey, $locales->getLocales())) {
188  foreach ($locales->getLocaleDependencies(self::$languageKey) as $language) {
189  self::$alternativeLanguageKeys[] = $language;
190  }
191  }
192  }
193  }
194  } elseif (strlen($GLOBALS['BE_USER']->uc['lang']) > 0) {
195  self::$languageKey = $GLOBALS['BE_USER']->uc['lang'];
196  }
197  }
198 
207  static protected function loadTypoScriptLabels($extensionName) {
208  $configurationManager = static::getConfigurationManager();
209  $frameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
210  if (!is_array($frameworkConfiguration['_LOCAL_LANG'])) {
211  return;
212  }
213  self::$LOCAL_LANG_UNSET[$extensionName] = array();
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][0]['target'] = $labelValue;
221  if ($labelValue === '') {
222  self::$LOCAL_LANG_UNSET[$extensionName][$languageKey][$labelKey] = '';
223  }
224  if (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  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  static protected function flattenTypoScriptLabelArray(array $labelValues, $parentKey = '') {
254  $result = array();
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 
277  static protected function convertCharset($value, $charset) {
278  if (TYPO3_MODE === 'FE') {
279  return $GLOBALS['TSFE']->csConv($value, $charset);
280  } else {
281  $convertedValue = $GLOBALS['LANG']->csConvObj->conv($value, $GLOBALS['LANG']->csConvObj->parse_charset($charset), $GLOBALS['LANG']->charSet, 1);
282  return $convertedValue !== NULL ? $convertedValue : $value;
283  }
284  }
285 
291  static protected function getConfigurationManager() {
292  if (!is_null(static::$configurationManager)) {
293  return static::$configurationManager;
294  }
295  $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
296  $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
297  static::$configurationManager = $configurationManager;
298  return $configurationManager;
299  }
300 }
const TYPO3_MODE
Definition: init.php:40
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
static flattenTypoScriptLabelArray(array $labelValues, $parentKey='')
static translate($key, $extensionName, $arguments=NULL)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
$locales
Definition: be_users.php:6