[BUGFIX] LocalizationFactory use new trigger_error call
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / LocalizationFactory.php
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
17 use TYPO3\CMS\Core\Cache\CacheManager;
18 use TYPO3\CMS\Core\Utility\ArrayUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Provides a language parser factory.
23 */
24 class LocalizationFactory implements \TYPO3\CMS\Core\SingletonInterface
25 {
26 /**
27 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
28 */
29 protected $cacheInstance;
30
31 /**
32 * @var \TYPO3\CMS\Core\Localization\LanguageStore
33 */
34 public $store;
35
36 /**
37 * Class constructor
38 */
39 public function __construct()
40 {
41 $this->store = GeneralUtility::makeInstance(LanguageStore::class);
42 $this->initializeCache();
43 }
44
45 /**
46 * Initialize cache instance to be ready to use
47 */
48 protected function initializeCache()
49 {
50 $this->cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n');
51 }
52
53 /**
54 * Returns parsed data from a given file and language key.
55 *
56 * @param string $fileReference Input is a file-reference (see \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName). That file is expected to be a supported locallang file format
57 * @param string $languageKey Language key
58 * @param string $charset Character set (option); not in use anymore
59 * @param int $errorMode Error mode (when file could not be found): not in use anymore
60 * @param bool $isLocalizationOverride TRUE if $fileReference is a localization override
61 * @return array|bool
62 */
63 public function getParsedData($fileReference, $languageKey, $charset = '', $errorMode = null, $isLocalizationOverride = false)
64 {
65 // @deprecated since TYPO3 v9, will be removed with TYPO3 v10
66 // this is a fallback to convert references to old 'lang' locallang files to the new location
67 if (strpos($fileReference, 'EXT:lang/Resources/Private/Language/') === 0) {
68 $mapping = [
69 'lang/Resources/Private/Language/locallang_alt_intro.xlf' => 'about/Resources/Private/Language/Modules/locallang_alt_intro.xlf',
70 'lang/Resources/Private/Language/locallang_alt_doc.xlf' => 'backend/Resources/Private/Language/locallang_alt_doc.xlf',
71 'lang/Resources/Private/Language/locallang_login.xlf' => 'backend/Resources/Private/Language/locallang_login.xlf',
72 'lang/Resources/Private/Language/locallang_common.xlf' => 'core/Resources/Private/Language/locallang_common.xlf',
73 'lang/Resources/Private/Language/locallang_core.xlf' => 'core/Resources/Private/Language/locallang_core.xlf',
74 'lang/Resources/Private/Language/locallang_general.xlf' => 'core/Resources/Private/Language/locallang_general.xlf',
75 'lang/Resources/Private/Language/locallang_misc.xlf' => 'core/Resources/Private/Language/locallang_misc.xlf',
76 'lang/Resources/Private/Language/locallang_mod_web_list.xlf' => 'core/Resources/Private/Language/locallang_mod_web_list.xlf',
77 'lang/Resources/Private/Language/locallang_tca.xlf' => 'core/Resources/Private/Language/locallang_tca.xlf',
78 'lang/Resources/Private/Language/locallang_tsfe.xlf' => 'core/Resources/Private/Language/locallang_tsfe.xlf',
79 'lang/Resources/Private/Language/locallang_wizards.xlf' => 'core/Resources/Private/Language/locallang_wizards.xlf',
80 'lang/Resources/Private/Language/locallang_browse_links.xlf' => 'recordlist/Resources/Private/Language/locallang_browse_links.xlf',
81 'lang/Resources/Private/Language/locallang_tcemain.xlf' => 'workspaces/Resources/Private/Language/locallang_tcemain.xlf',
82 ];
83 $filePath = substr($fileReference, 4);
84 trigger_error('There is a reference to "' . $fileReference . '", which has been moved to "EXT:' . $mapping[$filePath] . '". This fallback will be removed with TYPO3 v10.', E_USER_DEPRECATED);
85 $fileReference = 'EXT:' . $mapping[$filePath];
86 }
87
88 $hash = md5($fileReference . $languageKey);
89
90 // Check if the default language is processed before processing other language
91 if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
92 $this->getParsedData($fileReference, 'default');
93 }
94 // If the content is parsed (local cache), use it
95 if ($this->store->hasData($fileReference, $languageKey)) {
96 return $this->store->getData($fileReference);
97 }
98
99 // If the content is in cache (system cache), use it
100 $data = $this->cacheInstance->get($hash);
101 if ($data !== false) {
102 $this->store->setData($fileReference, $languageKey, $data);
103 return $this->store->getData($fileReference);
104 }
105
106 try {
107 $this->store->setConfiguration($fileReference, $languageKey);
108 /** @var $parser \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface */
109 $parser = $this->store->getParserInstance($fileReference);
110 // Get parsed data
111 $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey);
112 } catch (Exception\FileNotFoundException $exception) {
113 // Source localization file not found, set empty data as there could be an override
114 $this->store->setData($fileReference, $languageKey, []);
115 $LOCAL_LANG = $this->store->getData($fileReference);
116 }
117
118 // Override localization
119 if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
120 $this->localizationOverride($fileReference, $languageKey, $LOCAL_LANG);
121 }
122
123 // Save parsed data in cache
124 $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
125
126 // Cache processed data
127 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
128
129 return $this->store->getData($fileReference);
130 }
131
132 /**
133 * Override localization file
134 *
135 * This method merges the content of the override file with the default file
136 *
137 * @param string $fileReference
138 * @param string $languageKey
139 * @param array $LOCAL_LANG
140 */
141 protected function localizationOverride($fileReference, $languageKey, array &$LOCAL_LANG)
142 {
143 $overrides = [];
144 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
145 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
146 foreach ($this->store->getSupportedExtensions() as $extension) {
147 if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
148 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
149 } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
150 $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
151 }
152 }
153 if (!empty($overrides)) {
154 foreach ($overrides as $overrideFile) {
155 $languageOverrideFileName = GeneralUtility::getFileAbsFileName($overrideFile);
156 ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, null, null, true));
157 }
158 }
159 }
160 }