ExtBase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / View / Helper / TranslateHelper.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A For Helper
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 */
32 class Tx_ExtBase_View_Helper_TranslateHelper extends Tx_ExtBase_View_Helper_AbstractHelper {
33
34 /**
35 * @var string
36 */
37 protected $languagePath = 'Resources/Private/Language/';
38
39 /**
40 * Local Language content
41 *
42 * @var string
43 **/
44 protected $LOCAL_LANG = array();
45
46 /**
47 * Local Language content charset for individual labels (overriding)
48 *
49 * @var string
50 **/
51 protected $LOCAL_LANG_charset = array();
52
53 /**
54 * Key of the language to use
55 *
56 * @var string
57 **/
58 protected $languageKey = 'default';
59
60 /**
61 * Pointer to alternative fall-back language to use
62 *
63 * @var string
64 **/
65 protected $alternativeLanguageKey = '';
66 // var $LLtestPrefix=''; // You can set this during development to some value that makes it easy for you to spot all labels that ARe delivered by the getLL function.
67 // var $LLtestPrefixAlt=''; // Save as LLtestPrefix, but additional prefix for the alternative value in getLL() function calls
68
69
70 public function render($view, $arguments, $templateSource, $variables) {
71 $this->initializeLocalization($view);
72 $translation = $this->translate($arguments['key']);
73 return (is_string($translation) && !empty($translation)) ? $translation : '';
74 }
75
76 /**
77 * Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
78 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
79 *
80 * @return void
81 */
82 protected function initializeLocalization($view) {
83 $languageFilePath = t3lib_extMgm::extPath(strtolower($this->request->getExtensionName())) . $this->languagePath . 'locallang.php';
84
85 if ($GLOBALS['TSFE']->config['config']['language']) {
86 $this->languageKey = $GLOBALS['TSFE']->config['config']['language'];
87 if ($GLOBALS['TSFE']->config['config']['language_alt']) {
88 $this->alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
89 }
90 }
91
92 $this->LOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->languageKey, $GLOBALS['TSFE']->renderCharset);
93 if ($this->alternativeLanguageKey) {
94 $tempLOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->alternativeLanguageKey);
95 $this->LOCAL_LANG = array_merge(is_array($this->LOCAL_LANG) ? $this->LOCAL_LANG : array(), $tempLOCAL_LANG);
96 }
97
98 $configurationManager = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Manager');
99 $settings = $configurationManager->getSettings($this->request->getExtensionName());
100 if (is_array($settings['_LOCAL_LANG'])) {
101 foreach ($settings['_LOCAL_LANG'] as $k => $lA) {
102 if (is_array($lA)) {
103 foreach($lA as $llK => $llV) {
104 if (!is_array($llV)) {
105 $this->LOCAL_LANG[$k][$llK] = $llV;
106 // 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
107 $this->LOCAL_LANG_charset[$k][$llK] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : $GLOBALS['TSFE']->csConvObj->charSetArray[$k];
108 }
109 }
110 }
111 }
112 }
113 }
114
115 /**
116 * Returns the localized label of the LOCAL_LANG key, $key
117 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
118 *
119 * @param string The key from the LOCAL_LANG array for which to return the value.
120 * @param string Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
121 * @param boolean If true, the output label is passed through htmlspecialchars()
122 * @return string The value from LOCAL_LANG.
123 */
124 function translate($key, $default = '', $filterTranslation = FALSE) {
125 // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
126 if (isset($this->LOCAL_LANG[$this->languageKey][$key])) {
127 $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->languageKey][$key], $this->LOCAL_LANG_charset[$this->languageKey][$key]);
128 } elseif ($this->alternativeLanguageKey && isset($this->LOCAL_LANG[$this->alternativeLanguageKey][$key])) {
129 $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->alternativeLanguageKey][$key], $this->LOCAL_LANG_charset[$this->alternativeLanguageKey][$key]);
130 } elseif (isset($this->LOCAL_LANG['default'][$key])) {
131 $translation = $this->LOCAL_LANG['default'][$key]; // No charset conversion because default is english and thereby ASCII
132 } else {
133 $translation = $default;
134 }
135 return $filterTranslation === TRUE ? htmlspecialchars($translation) : $translation;
136 }
137
138
139
140 }
141
142 ?>