[BUGFIX] locallangXMLOverride is broken since the switch to XLIFF
[Packages/TYPO3.CMS.git] / t3lib / l10n / class.t3lib_l10n_factory.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Provides a language parser factory.
30 *
31 * @package TYPO3
32 * @subpackage t3lib
33 * @author Dominique Feyer <dfeyer@reelpeek.net>
34 */
35 class t3lib_l10n_Factory implements t3lib_Singleton {
36
37 /**
38 * @var t3lib_cache_frontend_StringFrontend
39 */
40 protected $cacheInstance;
41
42 /**
43 * @var integer
44 */
45 protected $errorMode;
46
47 /**
48 * @var t3lib_l10n_Store
49 */
50 public $store;
51
52 /**
53 * Class constructor
54 */
55 public function __construct() {
56 $this->initialize();
57 }
58
59 protected function initialize() {
60 $this->store = t3lib_div::makeInstance('t3lib_l10n_Store');
61
62 $this->initializeCache();
63 }
64
65 /**
66 * Initialize cache instance to be ready to use
67 *
68 * @return void
69 */
70 protected function initializeCache() {
71 $this->cacheInstance = $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n');
72 }
73
74 /**
75 * Returns parsed data from a given file and language key.
76 *
77 * @param string $fileReference Input is a file-reference (see t3lib_div::getFileAbsFileName). That file is expected to be a supported locallang file format
78 * @param string $languageKey Language key
79 * @param string $charset Character set (option); if not set, determined by the language key
80 * @param int $errorMode Error mode (when file could not be found): 0 - syslog entry, 1 - do nothing, 2 - throw an exception$
81 * @param bool $isLocalizationOverride TRUE if $fileReference is a localization override
82 * @return array|bool
83 */
84 public function getParsedData($fileReference, $languageKey, $charset, $errorMode, $isLocalizationOverride = FALSE) {
85 try {
86 $hash = md5($fileReference . $languageKey . $charset);
87 $this->errorMode = $errorMode;
88
89 // English is the default language
90 $languageKey = ($languageKey === 'en') ? 'default' : $languageKey;
91
92 // Check if the default language is processed before processing other language
93 if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
94 $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
95 }
96
97 // If the content is parsed (local cache), use it
98 if ($this->store->hasData($fileReference, $languageKey)) {
99 return $this->store->getData($fileReference);
100 }
101
102 // If the content is in cache (system cache), use it
103 if ($this->cacheInstance->has($hash)) {
104 // Load data from the caching framework
105 $this->store->setData($fileReference, $languageKey, $this->cacheInstance->get($hash));
106
107 return $this->store->getData($fileReference, $languageKey);
108 }
109
110 $this->store->setConfiguration($fileReference, $languageKey, $charset);
111
112 /** @var $parser t3lib_l10n_parser */
113 $parser = $this->store->getParserInstance($fileReference);
114
115 // Get parsed data
116 $LOCAL_LANG = $parser->getParsedData(
117 $this->store->getAbsoluteFileReference($fileReference),
118 $languageKey,
119 $charset
120 );
121
122 // Override localization
123 if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
124 $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
125 }
126
127 // Save parsed data in cache
128 $this->store->setData(
129 $fileReference,
130 $languageKey,
131 $LOCAL_LANG[$languageKey]
132 );
133
134 // Cache processed data
135 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
136 } catch (t3lib_l10n_exception_FileNotFound $exception) {
137 // Source localization file not found
138 $this->store->setData($fileReference, $languageKey, array());
139 }
140
141 return $this->store->getData($fileReference);
142 }
143
144 /**
145 * Override localization file
146 *
147 * This method merges the content of the override file with the default file
148 *
149 * @param string $fileReference
150 * @param string $languageKey
151 * @param string $charset
152 * @param integer $errorMode
153 * @param array $LOCAL_LANG
154 * @return void
155 */
156 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG) {
157 $overrides = array();
158 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
159
160 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
161 foreach ($this->store->getSupportedExtensions() as $extension) {
162 if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
163 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
164 } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
165 $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
166 }
167 }
168
169 if (count($overrides) > 0) {
170 foreach ($overrides as $overrideFile) {
171 $languageOverrideFileName = t3lib_div::getFileAbsFileName($overrideFile);
172 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule(
173 $LOCAL_LANG,
174 $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, TRUE)
175 );
176 }
177 }
178 }
179
180 }
181
182
183 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/l10n/class.t3lib_l10n_factory.php'])) {
184 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/l10n/class.t3lib_l10n_factory.php']);
185 }
186
187 ?>