f9162b3f918a76d50c3ca8decfa61b75dc4646a3
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / classes / class.tx_lang_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 tx_lang
33 * @author Dominique Feyer <dfeyer@reelpeek.net>
34 */
35 class tx_lang_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 tx_lang_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('tx_lang_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('lang_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 tx_lang_parser */
112 $parser = $this->store->getParserInstance($fileReference);
113
114 $LOCAL_LANG = $parser->getParsedData(
115 $this->store->getAbsoluteFileReference($fileReference),
116 $languageKey,
117 $charset
118 );
119 $this->store->setData(
120 $fileReference,
121 $languageKey,
122 $LOCAL_LANG[$languageKey]
123 );
124
125 // Cache processed data
126 $this->cacheInstance->set(
127 $hash,
128 $this->store->getDataByLanguage($fileReference, $languageKey),
129 array(),
130 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['cache']['lifetime']
131 );
132
133 } catch (tx_lang_exception_FileNotFound $exception) {
134 // Source localization file not found
135 $this->store->setData($fileReference, $languageKey, array());
136 }
137
138 return $this->store->getData($fileReference);
139 }
140
141 }
142
143 ?>