2 /***************************************************************
5 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * Provides a language parser factory.
33 * @author Dominique Feyer <dfeyer@reelpeek.net>
35 class t3lib_l10n_Factory
implements t3lib_Singleton
{
38 * @var t3lib_cache_frontend_StringFrontend
40 protected $cacheInstance;
48 * @var t3lib_l10n_Store
55 public function __construct() {
59 protected function initialize() {
60 $this->store
= t3lib_div
::makeInstance('t3lib_l10n_Store');
62 $this->initializeCache();
66 * Initialize cache instance to be ready to use
70 protected function initializeCache() {
71 $this->cacheInstance
= $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n');
75 * Returns parsed data from a given file and language key.
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
83 public function getParsedData($fileReference, $languageKey, $charset, $errorMode) {
85 $hash = md5($fileReference . $languageKey . $charset);
86 $this->errorMode
= $errorMode;
88 // English is the default language
89 $languageKey = ($languageKey === 'en') ?
'default' : $languageKey;
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
);
96 // If the content is parsed (local cache), use it
97 if ($this->store
->hasData($fileReference, $languageKey)) {
98 return $this->store
->getData($fileReference);
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));
106 return $this->store
->getData($fileReference, $languageKey);
109 $this->store
->setConfiguration($fileReference, $languageKey, $charset);
111 /** @var $parser t3lib_l10n_parser */
112 $parser = $this->store
->getParserInstance($fileReference);
115 $LOCAL_LANG = $parser->getParsedData(
116 $this->store
->getAbsoluteFileReference($fileReference),
121 // Override localization
122 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
123 $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
126 // Save parsed data in cache
127 $this->store
->setData(
130 $LOCAL_LANG[$languageKey]
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());
140 return $this->store
->getData($fileReference);
144 * Override localization file
146 * This method merges the content of the override file with the default file
148 * @param string $fileReference
149 * @param string $languageKey
150 * @param string $charset
151 * @param integer $errorMode
152 * @param array $LOCAL_LANG
155 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG) {
156 $overrides = array();
157 $fileReferenceWithoutExtension = $this->store
->getFileReferenceWithoutExtension($fileReference);
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]);
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(
173 $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode)
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']);