9bf7fe69236b205ebfb3df95d8c89359404f0fe9
[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 * @return array|bool
82 */
83 public function getParsedData($fileReference, $languageKey, $charset, $errorMode) {
84 try {
85 $hash = md5($fileReference . $languageKey . $charset);
86 $this->errorMode = $errorMode;
87
88 // English is the default language
89 $languageKey = ($languageKey === 'en') ? 'default' : $languageKey;
90
91 // Check if the default language is processed before processing other language
92 if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
93 $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
94 }
95
96 // If the content is parsed (local cache), use it
97 if ($this->store->hasData($fileReference, $languageKey)) {
98 return $this->store->getData($fileReference);
99 }
100
101 // If the content is in cache (system cache), use it
102 if ($this->cacheInstance->has($hash)) {
103 // Load data from the caching framework
104 $this->store->setData($fileReference, $languageKey, $this->cacheInstance->get($hash));
105
106 return $this->store->getData($fileReference, $languageKey);
107 }
108
109 $this->store->setConfiguration($fileReference, $languageKey, $charset);
110
111 /** @var $parser t3lib_l10n_parser */
112 $parser = $this->store->getParserInstance($fileReference);
113
114 // Get parsed data
115 $LOCAL_LANG = $parser->getParsedData(
116 $this->store->getAbsoluteFileReference($fileReference),
117 $languageKey,
118 $charset
119 );
120
121 // Override localization
122 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
123 $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
124 }
125
126 // Save parsed data in cache
127 $this->store->setData(
128 $fileReference,
129 $languageKey,
130 $LOCAL_LANG[$languageKey]
131 );
132
133 // Cache processed data
134 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
135 } catch (t3lib_l10n_exception_FileNotFound $exception) {
136 // Source localization file not found
137 $this->store->setData($fileReference, $languageKey, array());
138 }
139
140 return $this->store->getData($fileReference);
141 }
142
143 /**
144 * Override localization file
145 *
146 * This method merges the content of the override file with the default file
147 *
148 * @param string $fileReference
149 * @param string $languageKey
150 * @param string $charset
151 * @param integer $errorMode
152 * @param array $LOCAL_LANG
153 * @return void
154 */
155 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG) {
156 $overrides = array();
157 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
158
159 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
160 foreach ($this->store->getSupportedExtensions() as $extension) {
161 if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
162 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
163 } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
164 $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
165 }
166 }
167
168 if (count($overrides) > 0) {
169 foreach ($overrides as $overrideFile) {
170 $languageOverrideFileName = t3lib_div::getFileAbsFileName($overrideFile);
171 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule(
172 $LOCAL_LANG,
173 $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode)
174 );
175 }
176 }
177 }
178
179 }
180
181
182 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/l10n/class.t3lib_l10n_factory.php'])) {
183 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/l10n/class.t3lib_l10n_factory.php']);
184 }
185
186 ?>