[BUGFIX] Allow "en" as language key
[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 // 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 // If the content is parsed (local cache), use it
96 if ($this->store->hasData($fileReference, $languageKey)) {
97 return $this->store->getData($fileReference);
98 }
99 // If the content is in cache (system cache), use it
100 if ($this->cacheInstance->has($hash)) {
101 // Load data from the caching framework
102 $this->store->setData($fileReference, $languageKey, $this->cacheInstance->get($hash));
103 return $this->store->getData($fileReference, $languageKey);
104 }
105 $this->store->setConfiguration($fileReference, $languageKey, $charset);
106 /** @var $parser \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface */
107 $parser = $this->store->getParserInstance($fileReference);
108 // Get parsed data
109 $LOCAL_LANG = $parser->getParsedData($this->store->getAbsoluteFileReference($fileReference), $languageKey, $charset);
110 // Override localization
111 if (!$isLocalizationOverride && isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'])) {
112 $this->localizationOverride($fileReference, $languageKey, $charset, $errorMode, $LOCAL_LANG);
113 }
114 // Save parsed data in cache
115 $this->store->setData($fileReference, $languageKey, $LOCAL_LANG[$languageKey]);
116 // Cache processed data
117 $this->cacheInstance->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
118 } catch (\TYPO3\CMS\Core\Localization\Exception\FileNotFoundException $exception) {
119 // Source localization file not found
120 $this->store->setData($fileReference, $languageKey, array());
121 }
122 return $this->store->getData($fileReference);
123 }
124
125 /**
126 * Override localization file
127 *
128 * This method merges the content of the override file with the default file
129 *
130 * @param string $fileReference
131 * @param string $languageKey
132 * @param string $charset
133 * @param integer $errorMode
134 * @param array $LOCAL_LANG
135 * @return void
136 */
137 protected function localizationOverride($fileReference, $languageKey, $charset, $errorMode, array &$LOCAL_LANG) {
138 $overrides = array();
139 $fileReferenceWithoutExtension = $this->store->getFileReferenceWithoutExtension($fileReference);
140 $locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
141 foreach ($this->store->getSupportedExtensions() as $extension) {
142 if (isset($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension])) {
143 $overrides = array_merge($overrides, $locallangXMLOverride[$languageKey][$fileReferenceWithoutExtension . '.' . $extension]);
144 } elseif (isset($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]) && is_array($locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension])) {
145 $overrides = array_merge($overrides, $locallangXMLOverride[$fileReferenceWithoutExtension . '.' . $extension]);
146 }
147 }
148 if (count($overrides) > 0) {
149 foreach ($overrides as $overrideFile) {
150 $languageOverrideFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($overrideFile);
151 $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($LOCAL_LANG, $this->getParsedData($languageOverrideFileName, $languageKey, $charset, $errorMode, TRUE));
152 }
153 }
154 }
155
156 }
157
158
159 ?>