TYPO3 CMS  TYPO3_7-6
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->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 CMS 7, will be removed with CMS 8
83  // this is a fallback to convert references to old 'cms' locallang files to the new location
84  if (strpos($fileReference, 'EXT:cms/') === 0) {
85  $mapping = [
86  'cms/web_info/loallang.xlf' => 'frontend/Resources/Private/Language/locallang_webinfo.xlf',
87  'cms/locallang_ttc.xlf' => 'frontend/Resources/Private/Language/locallang_ttc.xlf',
88  'cms/locallang_tca.xlf' => 'frontend/Resources/Private/Language/locallang_tca.xlf',
89  'cms/layout/locallang_db_new_content_el.xlf' => 'backend/Resources/Private/Language/locallang_db_new_content_el.xlf',
90  'cms/layout/locallang.xlf' => 'backend/Resources/Private/Language/locallang_layout.xlf',
91  'cms/layout/locallang_mod.xlf' => 'backend/Resources/Private/Language/locallang_mod.xlf',
92  'cms/locallang_csh_webinfo.xlf' => 'frontend/Resources/Private/Language/locallang_csh_webinfo.xlf',
93  'cms/locallang_csh_weblayout.xlf' => 'frontend/Resources/Private/Language/locallang_csh_weblayout.xlf',
94  ];
95  $filePath = substr($fileReference, 4);
96  GeneralUtility::deprecationLog('There is a reference to "' . $fileReference . '", which has been moved to "EXT:' . $mapping[$filePath] . '". This fallback will be removed with CMS 8.');
97  $fileReference = 'EXT:' . $mapping[$filePath];
98  }
99 
100  $hash = md5($fileReference . $languageKey . $charset);
101  $this->errorMode = $errorMode;
102 
103  // Check if the default language is processed before processing other language
104  if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
105  $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
106  }
107  // If the content is parsed (local cache), use it
108  if ($this->store->hasData($fileReference, $languageKey)) {
109  return $this->store->getData($fileReference);
110  }
111 
112  // If the content is in cache (system cache), use it
113  $data = $this->cacheInstance->get($hash);
114  if ($data !== false) {
115  $this->store->setData($fileReference, $languageKey, $data);
116  return $this->store->getData($fileReference);
117  }
118 
119  try {
120  $this->store->setConfiguration($fileReference, $languageKey, $charset);
122  $parser = $this->store->getParserInstance($fileReference);
123  // Get parsed data
124  $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey, $charset);
125  } catch (Exception\FileNotFoundException $exception) {
126  // Source localization file not found, set empty data as there could be an override
127  $this->store->setData($fileReference, $languageKey, []);
128  $LOCAL_LANG = $this->store->getData($fileReference);
129  }
130 
131  // Override localization
132  if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
133  $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
134  }
135 
136  // Save parsed data in cache
137  $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
138 
139  // Cache processed data
140  $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
141 
142  return $this->store->getData($fileReference);
143  }
144 
157  protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG)
158  {
159  $overrides = [];
160  $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
161  $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
162  foreach ($this->store->getSupportedExtensions() as $extension) {
163  if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
164  $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
165  } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
166  $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
167  }
168  }
169  if (!empty($overrides)) {
170  foreach ($overrides as $overrideFile) {
171  $languageOverrideFileName = GeneralUtility::getFileAbsFileName($overrideFile);
172  ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, true));
173  }
174  }
175  }
176 }
localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']