tx_fluid:
authorBastian Waidelich <bastian@typo3.org>
Sun, 21 Jun 2009 21:02:35 +0000 (21:02 +0000)
committerBastian Waidelich <bastian@typo3.org>
Sun, 21 Jun 2009 21:02:35 +0000 (21:02 +0000)
* translate view helper: added support for LLL-references. Resolves #3525
* translate view helper: small refactoring and code/comment tweaks, added unit test case (still needs more tests)
* !!! translate view helper: default values are not HTMLEscaped anymore, so you can write <f:translate key="nonexistingKey">fallback with <strong>HTML tags</strong></f:translate>

typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php
typo3/sysext/fluid/Tests/ViewHelpers/TranslateViewHelperTest_testcase.php [new file with mode: 0644]

index 3c3c807..315e92c 100644 (file)
@@ -92,9 +92,14 @@ class Tx_Fluid_ViewHelpers_TranslateViewHelper extends Tx_Fluid_Core_ViewHelper_
         * @author Bastian Waidelich <bastian@typo3.org>
         */
        public function render($key, $htmlEscape = TRUE) {
-               $defaultValue = $this->renderChildren();
-               $value = $this->translate($key, $defaultValue);
-               if ($htmlEscape) {
+               if (t3lib_div::isFirstPartOfStr($key, 'LLL:')) {
+                       $value = $GLOBALS['TSFE']->sL($key);
+               } else {
+                       $value = $this->translate($key);
+               }
+               if ($value === NULL) {
+                       $value = $this->renderChildren();
+               } elseif ($htmlEscape) {
                        $value = htmlspecialchars($value);
                }
                return $value;
@@ -114,29 +119,40 @@ class Tx_Fluid_ViewHelpers_TranslateViewHelper extends Tx_Fluid_Core_ViewHelper_
                $this->setLanguageKeys();
                self::$LOCAL_LANG[$this->extensionName] = t3lib_div::readLLfile($this->locallangPathAndFilename, self::$languageKey, $GLOBALS['TSFE']->renderCharset);
                if (self::$alternativeLanguageKey === '') {
-                       $this->loadAlternativeLanguage();
+                       $alternativeLocalLang = t3lib_div::readLLfile($this->locallangPathAndFilename, self::$alternativeLanguageKey);
+                       self::$LOCAL_LANG[$this->extensionName] = array_merge(self::$LOCAL_LANG[$this->extensionName], $alternativeLocalLang);
                }
                $this->loadTypoScriptLabels();
        }
 
+       /**
+        * Sets the currently active language/language_alt keys.
+        * Default values are "default" for language key and "" for language_alt key.
+        *
+        * @return void
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
        protected function setLanguageKeys() {
                self::$languageKey = 'default';
                self::$alternativeLanguageKey = '';
-               if ($GLOBALS['TSFE']->config['config']['language'] !== NULL) {
+               if (isset($GLOBALS['TSFE']->config['config']['language'])) {
                        self::$languageKey = $GLOBALS['TSFE']->config['config']['language'];
                        if (isset($GLOBALS['TSFE']->config['config']['language_alt'])) {
-                               if ($GLOBALS['TSFE']->config['config']['language_alt'] !== NULL) {
-                                       self::$alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
-                               }
+                               self::$alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
                        }
                }
        }
 
-       protected function loadAlternativeLanguage() {
-               $alternativeLocalLang = t3lib_div::readLLfile($this->locallangPathAndFilename, self::$alternativeLanguageKey);
-               self::$LOCAL_LANG[$this->extensionName] = array_merge(self::$LOCAL_LANG[$this->extensionName], $alternativeLocalLang);
-       }
-
+       /**
+        * Overwrites labels that are set via typoscript.
+        * TS locallang labels have to be configured like:
+        * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value
+        *
+        * @return void
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
        protected function loadTypoScriptLabels() {
                $configurationManager = t3lib_div::makeInstance('Tx_Extbase_Configuration_Manager');
                $settings = $configurationManager->getSettings($this->extensionName);
@@ -162,12 +178,11 @@ class Tx_Fluid_ViewHelpers_TranslateViewHelper extends Tx_Fluid_Core_ViewHelper_
         * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
         *
         * @param string $key The key from the LOCAL_LANG array for which to return the value.
-        * @param string $default Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
-        * @return string The value from LOCAL_LANG.
+        * @return string The value from LOCAL_LANG or NULL if no translation was found.
         * @author Christopher Hlubek <hlubek@networkteam.com>
         * @author Bastian Waidelich <bastian@typo3.org>
         */
-       protected function translate($key, $default = '') {
+       protected function translate($key) {
                // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
                if (isset(self::$LOCAL_LANG[$this->extensionName][self::$languageKey][$key])) {
                        $value = self::$LOCAL_LANG[$this->extensionName][self::$languageKey][$key];
@@ -188,7 +203,7 @@ class Tx_Fluid_ViewHelpers_TranslateViewHelper extends Tx_Fluid_Core_ViewHelper_
                        return self::$LOCAL_LANG[$this->extensionName]['default'][$key]; // No charset conversion because default is english and thereby ASCII
                }
 
-               return $default;
+               return NULL;
        }
 }
 
diff --git a/typo3/sysext/fluid/Tests/ViewHelpers/TranslateViewHelperTest_testcase.php b/typo3/sysext/fluid/Tests/ViewHelpers/TranslateViewHelperTest_testcase.php
new file mode 100644 (file)
index 0000000..2465203
--- /dev/null
@@ -0,0 +1,101 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+/**
+ * @package Fluid
+ * @subpackage Tests
+ * @version $Id$
+ */
+/**
+ * Testcase for TranslateViewHelper
+ *
+ * @package Fluid
+ * @subpackage Tests
+ * @version $Id$
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
+ */
+require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
+class Tx_Fluid_ViewHelpers_TranslateViewHelperTest_testcase extends Tx_Extbase_Base_testcase {
+
+       protected $tsfeBackup;
+
+       public function setUp() {
+               $this->tsfeBackup = $GLOBALS['TSFE'];
+               $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array(), array(), '', FALSE);
+       }
+
+       public function tearDown() {
+               $GLOBALS['TSFE'] = $this->tsfeBackup;
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAcceptsLllFileReference() {
+               $GLOBALS['TSFE']->expects($this->once())->method('sL')->with('LLL:someExtension/locallang.xml')->will($this->returnValue('some translation'));
+
+               $viewHelper = new Tx_Fluid_ViewHelpers_TranslateViewHelper();
+               $actualResult = $viewHelper->render('LLL:someExtension/locallang.xml');
+               $this->assertEquals('some translation', $actualResult);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderHtmlEscapesLllTranslationsByDefault() {
+               $GLOBALS['TSFE']->expects($this->once())->method('sL')->with('LLL:someExtension/locallang.xml')->will($this->returnValue('some translation with <strong>HTML tags</strong> and special chäracterß.'));
+
+               $viewHelper = new Tx_Fluid_ViewHelpers_TranslateViewHelper();
+               $actualResult = $viewHelper->render('LLL:someExtension/locallang.xml');
+               $this->assertEquals('some translation with &lt;strong&gt;HTML tags&lt;/strong&gt; and special chäracterß.', $actualResult);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function htmlEscapingCanBeDisabledForLllTranslations() {
+               $GLOBALS['TSFE']->expects($this->once())->method('sL')->with('LLL:someExtension/locallang.xml')->will($this->returnValue('some translation with <strong>HTML tags</strong> and special chäracterß.'));
+
+               $viewHelper = new Tx_Fluid_ViewHelpers_TranslateViewHelper();
+               $actualResult = $viewHelper->render('LLL:someExtension/locallang.xml', FALSE);
+               $this->assertEquals('some translation with <strong>HTML tags</strong> and special chäracterß.', $actualResult);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function defaultValuesAreNotHtmlEscaped() {
+               $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_TranslateViewHelper', array('translate', 'renderChildren'));
+               $viewHelper->expects($this->once())->method('translate')->with('nonexistingKey')->will($this->returnValue(NULL));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('some translation with <strong>HTML tags</strong> and special chäracterß.'));
+
+               $actualResult = $viewHelper->render('nonexistingKey');
+               $this->assertEquals('some translation with <strong>HTML tags</strong> and special chäracterß.', $actualResult);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function stillNeedsALotMoreTests() {
+               $this->markTestIncomplete('This still needs a lot more tests');
+       }
+}
+
+?>