91a1dd39e3e4ec1396f55154ec381b1102001e0a
[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\StringFrontend
28 */
29 protected $cacheInstance;
30
31 /**
32 * @var int
33 */
34 protected $errorMode;
35
36 /**
37 * @var \TYPO3\CMS\Core\Localization\LanguageStore
38 */
39 public $store;
40
41 /**
42 * Class constructor
43 */
44 public function __construct()
45 {
46 $this->initialize();
47 }
48
49 /**
50 * Initialize
51 *
52 * @return void
53 */
54 protected function initialize()
55 {
56 $this->store = GeneralUtility::makeInstance(LanguageStore::class);
57 $this->initializeCache();
58 }
59
60 /**
61 * Initialize cache instance to be ready to use
62 *
63 * @return void
64 */
65 protected function initializeCache()
66 {
67 $this->cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n');
68 }
69
70 /**
71 * Returns parsed data from a given file and language key.
72 *
73 * @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
74 * @param string $languageKey Language key
75 * @param string $charset Character set (option); if not set, determined by the language key
76 * @param int $errorMode Error mode (when file could not be found): 0 - syslog entry, 1 - do nothing, 2 - throw an exception$
77 * @param bool $isLocalizationOverride TRUE if $fileReference is a localization override
78 * @return array|bool
79 */
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);
121 /** @var $parser \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface */
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, array());
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
145 /**
146 * Override localization file
147 *
148 * This method merges the content of the override file with the default file
149 *
150 * @param string $fileReference
151 * @param string $languageKey
152 * @param string $charset
153 * @param int $errorMode
154 * @param array $LOCAL_LANG
155 * @return void
156 */
157 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG)
158 {
159 $overrides = array();
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 }