|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the TYPO3 CMS project. |
|
5
|
|
|
* |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
8
|
|
|
* of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please read the |
|
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* The TYPO3 project - inspiring people to share! |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace TYPO3\CMS\Core\Localization; |
|
17
|
|
|
|
|
18
|
|
|
use TYPO3\CMS\Core\Cache\CacheManager; |
|
19
|
|
|
use TYPO3\CMS\Core\Localization\Exception\FileNotFoundException; |
|
20
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
21
|
|
|
use TYPO3\CMS\Core\Utility\ArrayUtility; |
|
22
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Provides a language parser factory. |
|
26
|
|
|
*/ |
|
27
|
|
|
class LocalizationFactory implements SingletonInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $cacheInstance; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \TYPO3\CMS\Core\Localization\LanguageStore |
|
36
|
|
|
*/ |
|
37
|
|
|
public $store; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(LanguageStore $languageStore, CacheManager $cacheManager) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->store = $languageStore; |
|
42
|
|
|
$this->cacheInstance = $cacheManager->getCache('l10n'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns parsed data from a given file and language key. |
|
47
|
|
|
* |
|
48
|
|
|
* @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 |
|
49
|
|
|
* @param string $languageKey Language key |
|
50
|
|
|
* @param null $_ unused |
|
|
|
|
|
|
51
|
|
|
* @param null $__ unused |
|
|
|
|
|
|
52
|
|
|
* @param bool $isLocalizationOverride TRUE if $fileReference is a localization override |
|
53
|
|
|
* @return array|bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getParsedData($fileReference, $languageKey, $_ = null, $__ = null, $isLocalizationOverride = false) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$hash = md5($fileReference . $languageKey); |
|
58
|
|
|
|
|
59
|
|
|
// Check if the default language is processed before processing other language |
|
60
|
|
|
if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') { |
|
61
|
|
|
$this->getParsedData($fileReference, 'default'); |
|
62
|
|
|
} |
|
63
|
|
|
// If the content is parsed (local cache), use it |
|
64
|
|
|
if ($this->store->hasData($fileReference, $languageKey)) { |
|
65
|
|
|
return $this->store->getData($fileReference); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// If the content is in cache (system cache), use it |
|
69
|
|
|
$data = $this->cacheInstance->get($hash); |
|
70
|
|
|
if ($data !== false) { |
|
71
|
|
|
$this->store->setData($fileReference, $languageKey, $data); |
|
72
|
|
|
return $this->store->getData($fileReference); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
$this->store->setConfiguration($fileReference, $languageKey); |
|
77
|
|
|
/** @var \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface $parser */ |
|
78
|
|
|
$parser = $this->store->getParserInstance($fileReference); |
|
79
|
|
|
// Get parsed data |
|
80
|
|
|
$LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey); |
|
81
|
|
|
} catch (FileNotFoundException $exception) { |
|
82
|
|
|
// Source localization file not found, set empty data as there could be an override |
|
83
|
|
|
$this->store->setData($fileReference, $languageKey, []); |
|
84
|
|
|
$LOCAL_LANG = $this->store->getData($fileReference); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Override localization |
|
88
|
|
|
if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) { |
|
89
|
|
|
$this->localizationOverride($fileReference, $languageKey, $LOCAL_LANG); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Save parsed data in cache |
|
93
|
|
|
$this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]); |
|
94
|
|
|
|
|
95
|
|
|
// Cache processed data |
|
96
|
|
|
$this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey)); |
|
97
|
|
|
|
|
98
|
|
|
return $this->store->getData($fileReference); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Override localization file |
|
103
|
|
|
* |
|
104
|
|
|
* This method merges the content of the override file with the default file |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $fileReference |
|
107
|
|
|
* @param string $languageKey |
|
108
|
|
|
* @param array $LOCAL_LANG |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function localizationOverride($fileReference, $languageKey, array &$LOCAL_LANG) |
|
111
|
|
|
{ |
|
112
|
|
|
$overrides = []; |
|
113
|
|
|
$fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference); |
|
114
|
|
|
$locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']; |
|
115
|
|
|
foreach ($this->store->getSupportedExtensions() as $extension) { |
|
116
|
|
|
if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) { |
|
117
|
|
|
$overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]); |
|
118
|
|
|
} elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) { |
|
119
|
|
|
$overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
if (!empty($overrides)) { |
|
123
|
|
|
foreach ($overrides as $overrideFile) { |
|
124
|
|
|
$languageOverrideFileName = GeneralUtility::getFileAbsFileName($overrideFile); |
|
125
|
|
|
ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, null, null, true)); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|