Initialize translate view helper only once per request
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / TranslateViewHelper.php
1 <?php
2
3 /* *
4 * This script is part of the TYPO3 project - inspiring people to share! *
5 * *
6 * TYPO3 is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License version 2 as published by *
8 * the Free Software Foundation. *
9 * *
10 * This script is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
13 * Public License for more details. *
14 * */
15
16 /**
17 * Translate a key from locallang. The files are loaded from the folder
18 * "Resources/Private/Language/".
19 *
20 * @package TYPO3
21 * @subpackage Fluid
22 * @version $Id:$
23 */
24 class Tx_Fluid_ViewHelpers_TranslateViewHelper extends Tx_Fluid_Core_AbstractViewHelper {
25 protected static $initialized = FALSE;
26
27 /**
28 * @var string
29 */
30 protected $languagePath = 'Resources/Private/Language/';
31
32 /**
33 * Local Language content
34 *
35 * @var string
36 **/
37 protected $LOCAL_LANG = array();
38
39 /**
40 * Local Language content charset for individual labels (overriding)
41 *
42 * @var string
43 **/
44 protected $LOCAL_LANG_charset = array();
45
46 /**
47 * Key of the language to use
48 *
49 * @var string
50 **/
51 protected $languageKey = 'default';
52
53 /**
54 * Pointer to alternative fall-back language to use
55 *
56 * @var string
57 **/
58 protected $alternativeLanguageKey = '';
59
60 /**
61 * Translate a given key or use the tag body as default.
62 *
63 * @param string $key The locallang key
64 * @param boolean $htmlEscape TRUE if the result should be htmlescaped
65 * @return string The translated key or tag body if key doesn't exist
66 * @author Christopher Hlubek <hlubek@networkteam.com>
67 */
68 public function render($key, $htmlEscape = FALSE) {
69 if (!self::$initialized) {
70 $this->initializeLocalization();
71 self::$initialized = TRUE;
72 }
73 $defaultValue = $this->renderChildren();
74 $translation = $this->translate($key, $defaultValue, $htmlEscape);
75 return (is_string($translation) && !empty($translation)) ? $translation : '';
76 }
77
78 /**
79 * Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
80 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
81 *
82 * @return void
83 */
84 protected function initializeLocalization() {
85 $extensionName = $this->variableContainer->get('view')->getRequest()->getExtensionName();
86 $languageFilePath = t3lib_extMgm::extPath(strtolower($extensionName)) . $this->languagePath . 'locallang.php';
87
88 if ($GLOBALS['TSFE']->config['config']['language']) {
89 $this->languageKey = $GLOBALS['TSFE']->config['config']['language'];
90 if ($GLOBALS['TSFE']->config['config']['language_alt']) {
91 $this->alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
92 }
93 }
94
95 $this->LOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->languageKey, $GLOBALS['TSFE']->renderCharset);
96 if ($this->alternativeLanguageKey) {
97 $tempLOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->alternativeLanguageKey);
98 $this->LOCAL_LANG = array_merge(is_array($this->LOCAL_LANG) ? $this->LOCAL_LANG : array(), $tempLOCAL_LANG);
99 }
100
101 $configurationManager = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Manager');
102 $settings = $configurationManager->getSettings($extensionName);
103 if (is_array($settings['_LOCAL_LANG'])) {
104 foreach ($settings['_LOCAL_LANG'] as $k => $lA) {
105 if (is_array($lA)) {
106 foreach($lA as $llK => $llV) {
107 if (!is_array($llV)) {
108 $this->LOCAL_LANG[$k][$llK] = $llV;
109 // For labels coming from the TypoScript (database) the charset is assumed to be "forceCharset" and if that is not set, assumed to be that of the individual system languages
110 $this->LOCAL_LANG_charset[$k][$llK] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : $GLOBALS['TSFE']->csConvObj->charSetArray[$k];
111 }
112 }
113 }
114 }
115 }
116 }
117
118 /**
119 * Returns the localized label of the LOCAL_LANG key, $key
120 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
121 *
122 * @param string The key from the LOCAL_LANG array for which to return the value.
123 * @param string Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
124 * @param boolean If true, the output label is passed through htmlspecialchars()
125 * @return string The value from LOCAL_LANG.
126 */
127 function translate($key, $default = '', $filterTranslation = FALSE) {
128 // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
129 if (isset($this->LOCAL_LANG[$this->languageKey][$key])) {
130 $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->languageKey][$key], $this->LOCAL_LANG_charset[$this->languageKey][$key]);
131 } elseif ($this->alternativeLanguageKey && isset($this->LOCAL_LANG[$this->alternativeLanguageKey][$key])) {
132 $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->alternativeLanguageKey][$key], $this->LOCAL_LANG_charset[$this->alternativeLanguageKey][$key]);
133 } elseif (isset($this->LOCAL_LANG['default'][$key])) {
134 $translation = $this->LOCAL_LANG['default'][$key]; // No charset conversion because default is english and thereby ASCII
135 } else {
136 $translation = $default;
137 }
138 return $filterTranslation === TRUE ? htmlspecialchars($translation) : $translation;
139 }
140 }
141
142
143 ?>