22 |
22 |
* @version $Id:$
|
23 |
23 |
*/
|
24 |
24 |
class Tx_Fluid_ViewHelpers_TranslateViewHelper extends Tx_Fluid_Core_AbstractViewHelper {
|
25 |
|
protected static $initialized = FALSE;
|
26 |
|
|
27 |
25 |
/**
|
28 |
26 |
* @var string
|
29 |
27 |
*/
|
... | ... | |
34 |
32 |
*
|
35 |
33 |
* @var string
|
36 |
34 |
**/
|
37 |
|
protected $LOCAL_LANG = array();
|
|
35 |
protected static $LOCAL_LANG = array();
|
38 |
36 |
|
39 |
37 |
/**
|
40 |
38 |
* Local Language content charset for individual labels (overriding)
|
41 |
39 |
*
|
42 |
40 |
* @var string
|
43 |
41 |
**/
|
44 |
|
protected $LOCAL_LANG_charset = array();
|
|
42 |
protected static $LOCAL_LANG_charset = array();
|
45 |
43 |
|
46 |
44 |
/**
|
47 |
45 |
* Key of the language to use
|
48 |
46 |
*
|
49 |
47 |
* @var string
|
50 |
48 |
**/
|
51 |
|
protected $languageKey = 'default';
|
|
49 |
protected static $languageKey = 'default';
|
52 |
50 |
|
53 |
51 |
/**
|
54 |
52 |
* Pointer to alternative fall-back language to use
|
55 |
53 |
*
|
56 |
54 |
* @var string
|
57 |
55 |
**/
|
58 |
|
protected $alternativeLanguageKey = '';
|
59 |
|
|
|
56 |
protected static $alternativeLanguageKey = '';
|
|
57 |
|
|
58 |
/**
|
|
59 |
* The extension name for which this instance of the view helper was called.
|
|
60 |
*
|
|
61 |
* @var string
|
|
62 |
*/
|
|
63 |
protected $extensionName = '';
|
|
64 |
|
60 |
65 |
/**
|
61 |
66 |
* Translate a given key or use the tag body as default.
|
62 |
67 |
*
|
... | ... | |
66 |
71 |
* @author Christopher Hlubek <hlubek@networkteam.com>
|
67 |
72 |
*/
|
68 |
73 |
public function render($key, $htmlEscape = FALSE) {
|
69 |
|
if (!self::$initialized) {
|
|
74 |
$this->extensionName = $this->variableContainer->get('view')->getRequest()->getExtensionName();
|
|
75 |
if (!isset(self::$LOCAL_LANG[$this->extensionName])) {
|
70 |
76 |
$this->initializeLocalization();
|
71 |
|
self::$initialized = TRUE;
|
72 |
77 |
}
|
73 |
78 |
$defaultValue = $this->renderChildren();
|
74 |
79 |
$translation = $this->translate($key, $defaultValue, $htmlEscape);
|
... | ... | |
74 |
79 |
$translation = $this->translate($key, $defaultValue, $htmlEscape);
|
75 |
80 |
return (is_string($translation) && !empty($translation)) ? $translation : '';
|
76 |
81 |
}
|
77 |
|
|
|
82 |
|
78 |
83 |
/**
|
79 |
84 |
* Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
|
80 |
85 |
* Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
|
... | ... | |
82 |
87 |
* @return void
|
83 |
88 |
*/
|
84 |
89 |
protected function initializeLocalization() {
|
85 |
|
$extensionName = $this->variableContainer->get('view')->getRequest()->getExtensionName();
|
86 |
|
$languageFilePath = t3lib_extMgm::extPath(strtolower($extensionName)) . $this->languagePath . 'locallang.php';
|
|
90 |
$languageFilePath = t3lib_extMgm::extPath(strtolower($this->extensionName)) . $this->languagePath . 'locallang.php';
|
87 |
91 |
|
88 |
92 |
if ($GLOBALS['TSFE']->config['config']['language']) {
|
89 |
|
$this->languageKey = $GLOBALS['TSFE']->config['config']['language'];
|
|
93 |
self::$languageKey = $GLOBALS['TSFE']->config['config']['language'];
|
90 |
94 |
if ($GLOBALS['TSFE']->config['config']['language_alt']) {
|
91 |
|
$this->alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
|
|
95 |
self::$alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
|
92 |
96 |
}
|
93 |
97 |
}
|
94 |
98 |
|
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 |
self::$LOCAL_LANG[$this->extensionName] = t3lib_div::readLLfile($languageFilePath, self::$languageKey, $GLOBALS['TSFE']->renderCharset);
|
|
100 |
if (self::$alternativeLanguageKey) {
|
|
101 |
$tempLOCAL_LANG = t3lib_div::readLLfile($languageFilePath, self::$alternativeLanguageKey);
|
|
102 |
self::$LOCAL_LANG[$this->extensionName] = array_merge(
|
|
103 |
is_array(self::$LOCAL_LANG[$this->extensionName])
|
|
104 |
? self::$LOCAL_LANG[$this->extensionName]
|
|
105 |
: array(),
|
|
106 |
$tempLOCAL_LANG
|
|
107 |
);
|
99 |
108 |
}
|
100 |
109 |
|
101 |
110 |
$configurationManager = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Manager');
|
102 |
|
$settings = $configurationManager->getSettings($extensionName);
|
|
111 |
$settings = $configurationManager->getSettings($this->extensionName);
|
103 |
112 |
if (is_array($settings['_LOCAL_LANG'])) {
|
104 |
113 |
foreach ($settings['_LOCAL_LANG'] as $k => $lA) {
|
105 |
114 |
if (is_array($lA)) {
|
... | ... | |
105 |
114 |
if (is_array($lA)) {
|
106 |
115 |
foreach($lA as $llK => $llV) {
|
107 |
116 |
if (!is_array($llV)) {
|
108 |
|
$this->LOCAL_LANG[$k][$llK] = $llV;
|
|
117 |
self::$LOCAL_LANG[$this->extensionName][$k][$llK] = $llV;
|
109 |
118 |
// 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];
|
|
119 |
self::$LOCAL_LANG_charset[$this->extensionName][$k][$llK] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : $GLOBALS['TSFE']->csConvObj->charSetArray[$k];
|
111 |
120 |
}
|
112 |
121 |
}
|
113 |
122 |
}
|
114 |
123 |
}
|
115 |
124 |
}
|
116 |
125 |
}
|
117 |
|
|
|
126 |
|
118 |
127 |
/**
|
119 |
128 |
* Returns the localized label of the LOCAL_LANG key, $key
|
120 |
129 |
* Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
|
... | ... | |
124 |
133 |
* @param boolean If true, the output label is passed through htmlspecialchars()
|
125 |
134 |
* @return string The value from LOCAL_LANG.
|
126 |
135 |
*/
|
127 |
|
function translate($key, $default = '', $filterTranslation = FALSE) {
|
|
136 |
protected function translate($key, $default = '', $filterTranslation = FALSE) {
|
128 |
137 |
// 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]);
|
|
138 |
if (isset(self::$LOCAL_LANG[$this->extensionName][self::$languageKey][$key])) {
|
|
139 |
$translation = $GLOBALS['TSFE']->csConv(self::$LOCAL_LANG[$this->extensionName][self::$languageKey][$key], self::$LOCAL_LANG_charset[$this->extensionName][self::$languageKey][$key]);
|
|
140 |
} elseif (self::$alternativeLanguageKey && isset(self::$LOCAL_LANG[$this->extensionName][self::$alternativeLanguageKey][$key])) {
|
|
141 |
$translation = $GLOBALS['TSFE']->csConv(self::$LOCAL_LANG[$this->extensionName][self::$alternativeLanguageKey][$key], self::$LOCAL_LANG_charset[$this->extensionName][self::$alternativeLanguageKey][$key]);
|
133 |
142 |
} elseif (isset($this->LOCAL_LANG['default'][$key])) {
|
134 |
|
$translation = $this->LOCAL_LANG['default'][$key]; // No charset conversion because default is english and thereby ASCII
|
|
143 |
$translation = self::$LOCAL_LANG[$this->extensionName]['default'][$key]; // No charset conversion because default is english and thereby ASCII
|
135 |
144 |
} else {
|
136 |
145 |
$translation = $default;
|
137 |
146 |
}
|