[BUGFIX] Invalid fallback for non-localized labels
[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 /**
60 * Initialize
61 *
62 * @return void
63 */
64 protected function initialize() {
65 $this->store = t3lib_div::makeInstance('t3lib_l10n_Store');
66
67 $this->initializeCache();
68 }
69
70 /**
71 * Initialize cache instance to be ready to use
72 *
73 * @return void
74 */
75 protected function initializeCache() {
76 $this->cacheInstance = $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n');
77 }
78
79 /**
80 * Returns parsed data from a given file and language key.
81 *
82 * @param string $fileReference Input is a file-reference (see t3lib_div::getFileAbsFileName). That file is expected to be a supported locallang file format
83 * @param string $languageKey Language key
84 * @param string $charset Character set (option); if not set, determined by the language key
85 * @param integer $errorMode Error mode (when file could not be found): 0 - syslog entry, 1 - do nothing, 2 - throw an exception$
86 * @param boolean $isLocalizationOverride TRUE if $fileReference is a localization override
87 * @return array|boolean
88 */
89 public function getParsedData($fileReference, $languageKey, $charset, $errorMode, $isLocalizationOverride = FALSE) {
90 try {
91 $hash = md5($fileReference . $languageKey . $charset);
92 $this->errorMode = $errorMode;
93
94 // English is the default language
95 $languageKey = ($languageKey === 'en') ? 'default' : $languageKey;
96
97 // Check if the default language is processed before processing other language
98 if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
99 $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
100 }
101
102 // If the content is parsed (local cache), use it
103 if ($this->store->hasData($fileReference, $languageKey)) {
104 return $this->store->getData($fileReference);
105 }
106
107 // If the content is in cache (system cache), use it
108 if ($this->cacheInstance->has($hash)) {
109 // Load data from the caching framework
110 $this->store->setData($fileReference, $languageKey, $this->cacheInstance->get($hash));
111
112 return $this->store->getData($fileReference, $languageKey);
113 }
114
115 $this->store->setConfiguration($fileReference, $languageKey, $charset);
116
117 /** @var $parser t3lib_l10n_parser */
118 $parser = $this->store->getParserInstance($fileReference);
119
120 // Get parsed data
121 $LOCAL_LANG = $parser->getParsedData(
122 $this->store->getAbsoluteFileReference($fileReference),
123 $languageKey,
124 $charset
125 );
126
127 // Override localization
128 if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
129 $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
130 }
131
132 // Save parsed data in cache
133 $this->store->setData(
134 $fileReference,
135 $languageKey,
136 $LOCAL_LANG[$languageKey]
137 );
138
139 // Cache processed data
140 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
141 } catch (t3lib_l10n_exception_FileNotFound $exception) {
142 // Source localization file not found
143 $this->store->setData($fileReference, $languageKey, array());
144 }
145
146 return $this->store->getData($fileReference);
147 }
148
149 /**
150 * Override localization file
151 *
152 * This method merges the content of the override file with the default file
153 *
154 * @param string $fileReference
155 * @param string $languageKey
156 * @param string $charset
157 * @param integer $errorMode
158 * @param array $LOCAL_LANG
159 * @return void
160 */
161 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG) {
162 $overrides = array();
163 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
164
165 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
166 foreach ($this->store->getSupportedExtensions() as $extension) {
167 if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
168 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
169 } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
170 $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
171 }
172 }
173
174 if (count($overrides) > 0) {
175 foreach ($overrides as $overrideFile) {
176 $languageOverrideFileName = t3lib_div::getFileAbsFileName($overrideFile);
177 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule(
178 $LOCAL_LANG,
179 $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, TRUE)
180 );
181 }
182 }
183 }
184 }
185
186 ?>