TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
LocalizationFactory.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Localization;
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->initialize();
47  }
48 
54  protected function initialize()
55  {
56  $this->store = GeneralUtility::makeInstance(LanguageStore::class);
57  $this->initializeCache();
58  }
59 
65  protected function initializeCache()
66  {
67  $this->cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n');
68  }
69 
80  public function getParsedData($fileReference, $languageKey, $charset = '', $errorMode = 0, $isLocalizationOverride = false)
81  {
82  // @deprecated since TYPO3 v8, will be removed with TYPO3 v9
83  // this is a fallback to convert references to old 'lang' locallang files to the new location
84  if (strpos($fileReference, 'EXT:lang/locallang_') === 0) {
85  $mapping = [
86  'lang/locallang_alt_doc.xlf' => 'lang/Resources/Private/Language/locallang_alt_doc.xlf',
87  'lang/locallang_alt_intro.xlf' => 'lang/Resources/Private/Language/locallang_alt_intro.xlf',
88  'lang/locallang_browse_links.xlf' => 'lang/Resources/Private/Language/locallang_browse_links.xlf',
89  'lang/locallang_common.xlf' => 'lang/Resources/Private/Language/locallang_common.xlf',
90  'lang/locallang_core.xlf' => 'lang/Resources/Private/Language/locallang_core.xlf',
91  'lang/locallang_general.xlf' => 'lang/Resources/Private/Language/locallang_general.xlf',
92  'lang/locallang_login.xlf' => 'lang/Resources/Private/Language/locallang_login.xlf',
93  'lang/locallang_misc.xlf' => 'lang/Resources/Private/Language/locallang_misc.xlf',
94  'lang/locallang_mod_admintools.xlf' => 'lang/Resources/Private/Language/locallang_mod_admintools.xlf',
95  'lang/locallang_mod_file_list.xlf' => 'lang/Resources/Private/Language/locallang_mod_file_list.xlf',
96  'lang/locallang_mod_file.xlf' => 'lang/Resources/Private/Language/locallang_mod_file.xlf',
97  'lang/locallang_mod_help_about.xlf' => 'lang/Resources/Private/Language/locallang_mod_help_about.xlf',
98  'lang/locallang_mod_help_cshmanual.xlf' => 'lang/Resources/Private/Language/locallang_mod_help_cshmanual.xlf',
99  'lang/locallang_mod_help.xlf' => 'lang/Resources/Private/Language/locallang_mod_help.xlf',
100  'lang/locallang_mod_system.xlf' => 'lang/Resources/Private/Language/locallang_mod_system.xlf',
101  'lang/locallang_mod_usertools.xlf' => 'lang/Resources/Private/Language/locallang_mod_usertools.xlf',
102  'lang/locallang_mod_user_ws.xlf' => 'lang/Resources/Private/Language/locallang_mod_user_ws.xlf',
103  'lang/locallang_mod_web_func.xlf' => 'lang/Resources/Private/Language/locallang_mod_web_func.xlf',
104  'lang/locallang_mod_web_info.xlf' => 'lang/Resources/Private/Language/locallang_mod_web_info.xlf',
105  'lang/locallang_mod_web_list.xlf' => 'lang/Resources/Private/Language/locallang_mod_web_list.xlf',
106  'lang/locallang_mod_web.xlf' => 'lang/Resources/Private/Language/locallang_mod_web.xlf',
107  'lang/locallang_show_rechis.xlf' => 'lang/Resources/Private/Language/locallang_show_rechis.xlf',
108  'lang/locallang_t3lib_fullsearch.xlf' => 'lang/Resources/Private/Language/locallang_t3lib_fullsearch.xlf',
109  'lang/locallang_tca.xlf' => 'lang/Resources/Private/Language/locallang_tca.xlf',
110  'lang/locallang_tcemain.xlf' => 'lang/Resources/Private/Language/locallang_tcemain.xlf',
111  'lang/locallang_tsfe.xlf' => 'lang/Resources/Private/Language/locallang_tsfe.xlf',
112  'lang/locallang_tsparser.xlf' => 'lang/Resources/Private/Language/locallang_tsparser.xlf',
113  'lang/locallang_view_help.xlf' => 'lang/Resources/Private/Language/locallang_view_help.xlf',
114  'lang/locallang_wizards.xlf' => 'lang/Resources/Private/Language/locallang_wizards.xlf',
115  ];
116  $filePath = substr($fileReference, 4);
117  GeneralUtility::deprecationLog('There is a reference to "' . $fileReference . '", which has been moved to "EXT:' . $mapping[$filePath] . '". This fallback will be removed with TYPO3 v9.');
118  $fileReference = 'EXT:' . $mapping[$filePath];
119  }
120 
121  $hash = md5($fileReference . $languageKey . $charset);
122  $this->errorMode = $errorMode;
123 
124  // Check if the default language is processed before processing other language
125  if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
126  $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
127  }
128  // If the content is parsed (local cache), use it
129  if ($this->store->hasData($fileReference, $languageKey)) {
130  return $this->store->getData($fileReference);
131  }
132 
133  // If the content is in cache (system cache), use it
134  $data = $this->cacheInstance->get($hash);
135  if ($data !== false) {
136  $this->store->setData($fileReference, $languageKey, $data);
137  return $this->store->getData($fileReference);
138  }
139 
140  try {
141  $this->store->setConfiguration($fileReference, $languageKey, $charset);
143  $parser = $this->store->getParserInstance($fileReference);
144  // Get parsed data
145  $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey, $charset);
146  } catch (Exception\FileNotFoundException $exception) {
147  // Source localization file not found, set empty data as there could be an override
148  $this->store->setData($fileReference, $languageKey, []);
149  $LOCAL_LANG = $this->store->getData($fileReference);
150  }
151 
152  // Override localization
153  if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
154  $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
155  }
156 
157  // Save parsed data in cache
158  $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
159 
160  // Cache processed data
161  $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
162 
163  return $this->store->getData($fileReference);
164  }
165 
178  protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG)
179  {
180  $overrides = [];
181  $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
182  $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
183  foreach ($this->store->getSupportedExtensions() as $extension) {
184  if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
185  $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
186  } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
187  $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
188  }
189  }
190  if (!empty($overrides)) {
191  foreach ($overrides as $overrideFile) {
192  $languageOverrideFileName = GeneralUtility::getFileAbsFileName($overrideFile);
193  ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, true));
194  }
195  }
196  }
197 }
localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG)
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']
static makeInstance($className,...$constructorArguments)
static getFileAbsFileName($filename, $_=null, $_2=null)