TYPO3 CMS  TYPO3_8-7
LocalizationFactory.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 
20 
25 {
29  protected $cacheInstance;
30 
34  protected $errorMode;
35 
39  public $store;
40 
44  public function __construct()
45  {
46  $this->store = GeneralUtility::makeInstance(LanguageStore::class);
47  $this->initializeCache();
48  }
49 
53  protected function initializeCache()
54  {
55  $this->cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n');
56  }
57 
68  public function getParsedData($fileReference, $languageKey, $charset = '', $errorMode = 0, $isLocalizationOverride = false)
69  {
70  // @deprecated since TYPO3 v8, will be removed with TYPO3 v9
71  // this is a fallback to convert references to old 'lang' locallang files to the new location
72  if (strpos($fileReference, 'EXT:lang/locallang_') === 0) {
73  $mapping = [
74  'lang/locallang_alt_doc.xlf' => 'lang/Resources/Private/Language/locallang_alt_doc.xlf',
75  'lang/locallang_alt_intro.xlf' => 'lang/Resources/Private/Language/locallang_alt_intro.xlf',
76  'lang/locallang_browse_links.xlf' => 'lang/Resources/Private/Language/locallang_browse_links.xlf',
77  'lang/locallang_common.xlf' => 'lang/Resources/Private/Language/locallang_common.xlf',
78  'lang/locallang_core.xlf' => 'lang/Resources/Private/Language/locallang_core.xlf',
79  'lang/locallang_general.xlf' => 'lang/Resources/Private/Language/locallang_general.xlf',
80  'lang/locallang_login.xlf' => 'lang/Resources/Private/Language/locallang_login.xlf',
81  'lang/locallang_misc.xlf' => 'lang/Resources/Private/Language/locallang_misc.xlf',
82  'lang/locallang_mod_admintools.xlf' => 'lang/Resources/Private/Language/locallang_mod_admintools.xlf',
83  'lang/locallang_mod_file_list.xlf' => 'lang/Resources/Private/Language/locallang_mod_file_list.xlf',
84  'lang/locallang_mod_file.xlf' => 'lang/Resources/Private/Language/locallang_mod_file.xlf',
85  'lang/locallang_mod_help_about.xlf' => 'about/Resources/Private/Language/Modules/about.xlf',
86  'lang/locallang_mod_help_cshmanual.xlf' => 'lang/Resources/Private/Language/locallang_mod_help_cshmanual.xlf',
87  'lang/locallang_mod_help.xlf' => 'lang/Resources/Private/Language/locallang_mod_help.xlf',
88  'lang/locallang_mod_system.xlf' => 'lang/Resources/Private/Language/locallang_mod_system.xlf',
89  'lang/locallang_mod_usertools.xlf' => 'lang/Resources/Private/Language/locallang_mod_usertools.xlf',
90  'lang/locallang_mod_user_ws.xlf' => 'lang/Resources/Private/Language/locallang_mod_user_ws.xlf',
91  'lang/locallang_mod_web_func.xlf' => 'lang/Resources/Private/Language/locallang_mod_web_func.xlf',
92  'lang/locallang_mod_web_info.xlf' => 'lang/Resources/Private/Language/locallang_mod_web_info.xlf',
93  'lang/locallang_mod_web_list.xlf' => 'lang/Resources/Private/Language/locallang_mod_web_list.xlf',
94  'lang/locallang_mod_web.xlf' => 'lang/Resources/Private/Language/locallang_mod_web.xlf',
95  'lang/locallang_show_rechis.xlf' => 'lang/Resources/Private/Language/locallang_show_rechis.xlf',
96  'lang/locallang_t3lib_fullsearch.xlf' => 'lang/Resources/Private/Language/locallang_t3lib_fullsearch.xlf',
97  'lang/locallang_tca.xlf' => 'lang/Resources/Private/Language/locallang_tca.xlf',
98  'lang/locallang_tcemain.xlf' => 'lang/Resources/Private/Language/locallang_tcemain.xlf',
99  'lang/locallang_tsfe.xlf' => 'lang/Resources/Private/Language/locallang_tsfe.xlf',
100  'lang/locallang_tsparser.xlf' => 'lang/Resources/Private/Language/locallang_tsparser.xlf',
101  'lang/locallang_view_help.xlf' => 'lang/Resources/Private/Language/locallang_view_help.xlf',
102  'lang/locallang_wizards.xlf' => 'lang/Resources/Private/Language/locallang_wizards.xlf',
103  ];
104  $filePath = substr($fileReference, 4);
105  GeneralUtility::deprecationLog('There is a reference to "' . $fileReference . '", which has been moved to "EXT:' . $mapping[$filePath] . '". This fallback will be removed with TYPO3 v9.');
106  $fileReference = 'EXT:' . $mapping[$filePath];
107  }
108 
109  $hash = md5($fileReference . $languageKey . $charset);
110  $this->errorMode = $errorMode;
111 
112  // Check if the default language is processed before processing other language
113  if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
114  $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
115  }
116  // If the content is parsed (local cache), use it
117  if ($this->store->hasData($fileReference, $languageKey)) {
118  return $this->store->getData($fileReference);
119  }
120 
121  // If the content is in cache (system cache), use it
122  $data = $this->cacheInstance->get($hash);
123  if ($data !== false) {
124  $this->store->setData($fileReference, $languageKey, $data);
125  return $this->store->getData($fileReference);
126  }
127 
128  try {
129  $this->store->setConfiguration($fileReference, $languageKey, $charset);
131  $parser = $this->store->getParserInstance($fileReference);
132  // Get parsed data
133  $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey);
134  } catch (Exception\FileNotFoundException $exception) {
135  // Source localization file not found, set empty data as there could be an override
136  $this->store->setData($fileReference, $languageKey, []);
137  $LOCAL_LANG = $this->store->getData($fileReference);
138  }
139 
140  // Override localization
141  if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
142  $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
143  }
144 
145  // Save parsed data in cache
146  $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
147 
148  // Cache processed data
149  $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
150 
151  return $this->store->getData($fileReference);
152  }
153 
165  protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG)
166  {
167  $overrides = [];
168  $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
169  $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
170  foreach ($this->store->getSupportedExtensions() as $extension) {
171  if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
172  $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
173  } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
174  $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
175  }
176  }
177  if (!empty($overrides)) {
178  foreach ($overrides as $overrideFile) {
179  $languageOverrideFileName = GeneralUtility::getFileAbsFileName($overrideFile);
180  ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, true));
181  }
182  }
183  }
184 }
localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG)
static getFileAbsFileName($filename, $_=null, $_2=null)
static makeInstance($className,... $constructorArguments)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']