[!!!][TASK] Removed deprecated language handling code
[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 $hash = md5($fileReference . $languageKey . $charset);
83 $this->errorMode = $errorMode;
84
85 // Check if the default language is processed before processing other language
86 if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
87 $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
88 }
89 // If the content is parsed (local cache), use it
90 if ($this->store->hasData($fileReference, $languageKey)) {
91 return $this->store->getData($fileReference);
92 }
93
94 // If the content is in cache (system cache), use it
95 $data = $this->cacheInstance->get($hash);
96 if ($data !== false) {
97 $this->store->setData($fileReference, $languageKey, $data);
98 return $this->store->getData($fileReference);
99 }
100
101 try {
102 $this->store->setConfiguration($fileReference, $languageKey, $charset);
103 /** @var $parser \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface */
104 $parser = $this->store->getParserInstance($fileReference);
105 // Get parsed data
106 $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey, $charset);
107 } catch (Exception\FileNotFoundException $exception) {
108 // Source localization file not found, set empty data as there could be an override
109 $this->store->setData($fileReference, $languageKey, array());
110 $LOCAL_LANG = $this->store->getData($fileReference);
111 }
112
113 // Override localization
114 if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
115 $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
116 }
117
118 // Save parsed data in cache
119 $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
120
121 // Cache processed data
122 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
123
124 return $this->store->getData($fileReference);
125 }
126
127 /**
128 * Override localization file
129 *
130 * This method merges the content of the override file with the default file
131 *
132 * @param string $fileReference
133 * @param string $languageKey
134 * @param string $charset
135 * @param int $errorMode
136 * @param array $LOCAL_LANG
137 * @return void
138 */
139 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG)
140 {
141 $overrides = array();
142 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
143 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
144 foreach ($this->store->getSupportedExtensions() as $extension) {
145 if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
146 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
147 } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
148 $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
149 }
150 }
151 if (!empty($overrides)) {
152 foreach ($overrides as $overrideFile) {
153 $languageOverrideFileName = GeneralUtility::getFileAbsFileName($overrideFile);
154 ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, true));
155 }
156 }
157 }
158 }