[TASK] Move and Namespace classes
[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 * @package TYPO3
33 * @subpackage t3lib
34 * @author Dominique Feyer <dfeyer@reelpeek.net>
35 */
36 class LocalizationFactory implements \TYPO3\CMS\Core\SingletonInterface {
37
38 /**
39 * @var \TYPO3\CMS\Core\Cache\Frontend\StringFrontend
40 */
41 protected $cacheInstance;
42
43 /**
44 * @var integer
45 */
46 protected $errorMode;
47
48 /**
49 * @var \TYPO3\CMS\Core\Localization\LanguageStore
50 */
51 public $store;
52
53 /**
54 * Class constructor
55 */
56 public function __construct() {
57 $this->initialize();
58 }
59
60 /**
61 * Initialize
62 *
63 * @return void
64 */
65 protected function initialize() {
66 $this->store = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore');
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 // English is the default language
94 $languageKey = $languageKey === 'en' ? 'default' : $languageKey;
95 // Check if the default language is processed before processing other language
96 if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
97 $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
98 }
99 // If the content is parsed (local cache), use it
100 if ($this->store->hasData($fileReference, $languageKey)) {
101 return $this->store->getData($fileReference);
102 }
103 // If the content is in cache (system cache), use it
104 if ($this->cacheInstance->has($hash)) {
105 // Load data from the caching framework
106 $this->store->setData($fileReference, $languageKey, $this->cacheInstance->get($hash));
107 return $this->store->getData($fileReference, $languageKey);
108 }
109 $this->store->setConfiguration($fileReference, $languageKey, $charset);
110 /** @var $parser \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface */
111 $parser = $this->store->getParserInstance($fileReference);
112 // Get parsed data
113 $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey, $charset);
114 // Override localization
115 if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
116 $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
117 }
118 // Save parsed data in cache
119 $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
120 // Cache processed data
121 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
122 } catch (\TYPO3\CMS\Core\Localization\Exception\FileNotFoundException $exception) {
123 // Source localization file not found
124 $this->store->setData($fileReference, $languageKey, array());
125 }
126 return $this->store->getData($fileReference);
127 }
128
129 /**
130 * Override localization file
131 *
132 * This method merges the content of the override file with the default file
133 *
134 * @param string $fileReference
135 * @param string $languageKey
136 * @param string $charset
137 * @param integer $errorMode
138 * @param array $LOCAL_LANG
139 * @return void
140 */
141 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG) {
142 $overrides = array();
143 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
144 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
145 foreach ($this->store->getSupportedExtensions() as $extension) {
146 if (isset($locallangXMLOverride[$languageKey][($fileReferenceWithoutExtension . '.') . $extension]) && is_array($locallangXMLOverride[$languageKey][($fileReferenceWithoutExtension . '.') . $extension])) {
147 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][($fileReferenceWithoutExtension . '.') . $extension]);
148 } elseif (isset($locallangXMLOverride[($fileReferenceWithoutExtension . '.') . $extension]) && is_array($locallangXMLOverride[($fileReferenceWithoutExtension . '.') . $extension])) {
149 $overrides = array_merge($overrides, $locallangXMLOverride[($fileReferenceWithoutExtension . '.') . $extension]);
150 }
151 }
152 if (count($overrides) > 0) {
153 foreach ($overrides as $overrideFile) {
154 $languageOverrideFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($overrideFile);
155 $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, TRUE));
156 }
157 }
158 }
159
160 }
161
162
163 ?>