40dc3636de0341892a4809cc7b4e04cdc1472369
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / LocalizationFactory.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Provides a language parser factory.
31 *
32 * @author Dominique Feyer <dfeyer@reelpeek.net>
33 */
34 class LocalizationFactory implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * @var \TYPO3\CMS\Core\Cache\Frontend\StringFrontend
38 */
39 protected $cacheInstance;
40
41 /**
42 * @var integer
43 */
44 protected $errorMode;
45
46 /**
47 * @var \TYPO3\CMS\Core\Localization\LanguageStore
48 */
49 public $store;
50
51 /**
52 * Class constructor
53 */
54 public function __construct() {
55 $this->initialize();
56 }
57
58 /**
59 * Initialize
60 *
61 * @return void
62 */
63 protected function initialize() {
64 $this->store = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore');
65 $this->initializeCache();
66 }
67
68 /**
69 * Initialize cache instance to be ready to use
70 *
71 * @return void
72 */
73 protected function initializeCache() {
74 $this->cacheInstance = $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n');
75 }
76
77 /**
78 * Returns parsed data from a given file and language key.
79 *
80 * @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
81 * @param string $languageKey Language key
82 * @param string $charset Character set (option); if not set, determined by the language key
83 * @param integer $errorMode Error mode (when file could not be found): 0 - syslog entry, 1 - do nothing, 2 - throw an exception$
84 * @param boolean $isLocalizationOverride TRUE if $fileReference is a localization override
85 * @return array|boolean
86 */
87 public function getParsedData($fileReference, $languageKey, $charset, $errorMode, $isLocalizationOverride = FALSE) {
88 try {
89 $hash = md5($fileReference . $languageKey . $charset);
90 $this->errorMode = $errorMode;
91 // English is the default language
92 $languageKey = $languageKey === 'en' ? 'default' : $languageKey;
93 // Check if the default language is processed before processing other language
94 if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
95 $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
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 // 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 return $this->store->getData($fileReference, $languageKey);
106 }
107 $this->store->setConfiguration($fileReference, $languageKey, $charset);
108 /** @var $parser \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface */
109 $parser = $this->store->getParserInstance($fileReference);
110 // Get parsed data
111 $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey, $charset);
112 // Override localization
113 if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
114 $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
115 }
116 // Save parsed data in cache
117 $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
118 // Cache processed data
119 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
120 } catch (\TYPO3\CMS\Core\Localization\Exception\FileNotFoundException $exception) {
121 // Source localization file not found
122 $this->store->setData($fileReference, $languageKey, array());
123 }
124 return $this->store->getData($fileReference);
125 }
126
127 /**
128 * Override localization file
129 *
130 * This method merges the content of the override file with the default file
131 *
132 * @param string $fileReference
133 * @param string $languageKey
134 * @param string $charset
135 * @param integer $errorMode
136 * @param array $LOCAL_LANG
137 * @return void
138 */
139 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG) {
140 $overrides = array();
141 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
142 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
143 foreach ($this->store->getSupportedExtensions() as $extension) {
144 if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
145 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
146 } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
147 $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
148 }
149 }
150 if (count($overrides) > 0) {
151 foreach ($overrides as $overrideFile) {
152 $languageOverrideFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($overrideFile);
153 $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, TRUE));
154 }
155 }
156 }
157
158 }
159
160
161 ?>