98dacda80248d580f20187247d3906dcd041b4e9
[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 }
72 $defaultValue = $this->renderChildren();
73 $translation = $this->translate($key, $defaultValue, $htmlEscape);
74 return (is_string($translation) && !empty($translation)) ? $translation : '';
75 }
76
77 /**
78 * Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
79 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
80 *
81 * @return void
82 */
83 protected function initializeLocalization() {
84 $extensionName = $this->variableContainer->get('view')->getRequest()->getExtensionName();
85 $languageFilePath = t3lib_extMgm::extPath(strtolower($extensionName)) . $this->languagePath . 'locallang.php';
86
87 if ($GLOBALS['TSFE']->config['config']['language']) {
88 $this->languageKey = $GLOBALS['TSFE']->config['config']['language'];
89 if ($GLOBALS['TSFE']->config['config']['language_alt']) {
90 $this->alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
91 }
92 }
93
94 $this->LOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->languageKey, $GLOBALS['TSFE']->renderCharset);
95 if ($this->alternativeLanguageKey) {
96 $tempLOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->alternativeLanguageKey);
97 $this->LOCAL_LANG = array_merge(is_array($this->LOCAL_LANG) ? $this->LOCAL_LANG : array(), $tempLOCAL_LANG);
98 }
99
100 $configurationManager = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Manager');
101 $settings = $configurationManager->getSettings($extensionName);
102 if (is_array($settings['_LOCAL_LANG'])) {
103 foreach ($settings['_LOCAL_LANG'] as $k => $lA) {
104 if (is_array($lA)) {
105 foreach($lA as $llK => $llV) {
106 if (!is_array($llV)) {
107 $this->LOCAL_LANG[$k][$llK] = $llV;
108 // 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
109 $this->LOCAL_LANG_charset[$k][$llK] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : $GLOBALS['TSFE']->csConvObj->charSetArray[$k];
110 }
111 }
112 }
113 }
114 }
115 }
116
117 /**
118 * Returns the localized label of the LOCAL_LANG key, $key
119 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
120 *
121 * @param string The key from the LOCAL_LANG array for which to return the value.
122 * @param string Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
123 * @param boolean If true, the output label is passed through htmlspecialchars()
124 * @return string The value from LOCAL_LANG.
125 */
126 function translate($key, $default = '', $filterTranslation = FALSE) {
127 // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
128 if (isset($this->LOCAL_LANG[$this->languageKey][$key])) {
129 $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->languageKey][$key], $this->LOCAL_LANG_charset[$this->languageKey][$key]);
130 } elseif ($this->alternativeLanguageKey && isset($this->LOCAL_LANG[$this->alternativeLanguageKey][$key])) {
131 $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->alternativeLanguageKey][$key], $this->LOCAL_LANG_charset[$this->alternativeLanguageKey][$key]);
132 } elseif (isset($this->LOCAL_LANG['default'][$key])) {
133 $translation = $this->LOCAL_LANG['default'][$key]; // No charset conversion because default is english and thereby ASCII
134 } else {
135 $translation = $default;
136 }
137 return $filterTranslation === TRUE ? htmlspecialchars($translation) : $translation;
138 }
139 }
140
141
142 ?>