Added feature #15741: PageRenderer should be able to include locallang-files as JS...
authorOliver Hader <oliver.hader@typo3.org>
Sun, 19 Sep 2010 14:28:24 +0000 (14:28 +0000)
committerOliver Hader <oliver.hader@typo3.org>
Sun, 19 Sep 2010 14:28:24 +0000 (14:28 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@8823 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_pagerenderer.php
tests/t3lib/t3lib_pagerendererTest.php

index 7bfc232..d7945fa 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2010-09-19  Oliver Hader  <oliver@typo3.org>
 
        * Fixed bug #8260: Update Wizard in install tool force temp-configuration files and load configuration twice
+       * Added feature #15741: PageRenderer should be able to include locallang-files as JS-InlineLabels (thanks to Steffen Ritter)
 
 2010-09-19  Benjamin Mack  <benni@typo3.org>
 
index a725afe..8b85745 100644 (file)
@@ -1102,6 +1102,58 @@ class t3lib_PageRenderer implements t3lib_Singleton {
        }
 
        /**
+        * Gets labels to be used in JavaScript fetched from a locallang file.
+        *
+        * @param       string          Input is a file-reference (see t3lib_div::getFileAbsFileName). That file is expected to be a 'locallang.xml' file containing a valid XML TYPO3 language structure.
+        * @param       string          $selectionPrefix: Prefix to select the correct labels (default: '')
+        * @param       string          $stripFromSelectionName: Sub-prefix to be removed from label names in the result (default: '')
+        * @param       integer         Error mode (when file could not be found): 0 - syslog entry, 1 - do nothing, 2 - throw an exception
+        * @return      void
+        */
+       public function addInlineLanguageLabelFile($fileRef, $selectionPrefix = '', $stripFromSelectionName = '', $errorMode = 0) {
+               if (!isset($this->lang) || !isset($this->charSet)) {
+                       throw new RuntimeException('Language and character encoding are not set.', 1284906026);
+               }
+
+               $labelsFromFile = array();
+               $allLabels = t3lib_div::readLLfile($fileRef, $this->lang, $this->charSet, $errorMode);
+
+                       // Regular expression to strip the selection prefix and possibly something from the label name:
+               $labelPattern = '#^' . preg_quote($selectionPrefix, '#') . '(' . preg_quote($stripFromSelectionName, '#') . ')?#';
+
+               if ($allLabels !== FALSE) {
+                               // Merge language specific translations:
+                       if ($this->lang !== 'default' && isset($allLabels[$this->lang])) {
+                               $labels = array_merge($allLabels['default'], $allLabels[$this->lang]);
+                       } else {
+                               $labels = $allLabels['default'];
+                       }
+
+                               // Iterate through all locallang labels:
+                       foreach ($labels as $label => $value) {
+                               if ($selectionPrefix === '') {
+                                       $labelsFromFile[$label] = $value;
+                               } elseif (strpos($label, $selectionPrefix) === 0) {
+                                       $key = preg_replace($labelPattern, '', $label);
+                                       $labelsFromFile[$key] = $value;
+                               }
+                       }
+
+                       $this->inlineLanguageLabels = array_merge($this->inlineLanguageLabels, $labelsFromFile);
+               }
+       }
+
+       /**
+        * Gets the inline language lablels.
+        *
+        * @return array The inline language lables
+        */
+       public function getInlineLanguageLabels() {
+               return $this->inlineLanguageLabels;
+       }
+
+
+       /**
         * Adds Javascript Inline Setting. This will occur in TYPO3.settings - object
         * The label can be used in scripts with TYPO3.setting.<key>
         * Need extJs loaded
index 21381bc..78cb58f 100644 (file)
@@ -55,7 +55,7 @@ class t3lib_PageRendererTest extends tx_phpunit_testcase {
 
        public function setUp() {
                $this->fixture = new t3lib_PageRenderer();
-               $this->fixture->setCharSet('utf-8');
+               $this->fixture->setCharSet($GLOBALS['LANG']->charSet);
        }
 
        public function tearDown() {
@@ -773,5 +773,85 @@ class t3lib_PageRendererTest extends tx_phpunit_testcase {
 
                $this->assertContains($testPrefix . '_\\u00d8', $out);
        }
+
+       /**
+        * Tests the addInlineLanguageLabelFile() method.
+        *
+        * @test
+        * @expectedException RuntimeException
+        */
+       public function areInlineLanguageLablesNotProcessable() {
+               $this->fixture->setLanguage(NULL);
+               $this->fixture->addInlineLanguageLabelFile(
+                       'EXT:lang/locallang_core.xml'
+               );
+       }
+
+       /**
+        * Tests the addInlineLanguageLabelFile() method.
+        *
+        * @test
+        */
+       public function areInlineLanguageLablesPassed() {
+               $this->fixture->setLanguage($GLOBALS['LANG']->lang);
+               $this->fixture->addInlineLanguageLabelFile(
+                       'EXT:lang/locallang_core.xml'
+               );
+
+               $inlineLanguageLabels = $this->fixture->getInlineLanguageLabels();
+               $this->assertTrue(isset($inlineLanguageLabels['labels.beUser']));
+               $this->assertTrue(isset($inlineLanguageLabels['labels.feUser']));
+       }
+
+       /**
+        * Tests the addInlineLanguageLabelFile() method.
+        *
+        * @test
+        */
+       public function areInlineLanguageLablesEmptyOnNonExistingFile() {
+               $this->fixture->setLanguage($GLOBALS['LANG']->lang);
+               $this->fixture->addInlineLanguageLabelFile(
+                       ''
+               );
+
+               $inlineLanguageLabels = $this->fixture->getInlineLanguageLabels();
+               $this->assertEquals(array(), $inlineLanguageLabels);
+       }
+
+       /**
+        * Tests the addInlineLanguageLabelFile() method.
+        *
+        * @test
+        */
+       public function areInlineLanguageLablesSelected() {
+               $this->fixture->setLanguage($GLOBALS['LANG']->lang);
+               $this->fixture->addInlineLanguageLabelFile(
+                       'EXT:lang/locallang_core.xml',
+                       'labels.'
+               );
+
+               $inlineLanguageLabels = $this->fixture->getInlineLanguageLabels();
+               $this->assertTrue(isset($inlineLanguageLabels['beUser']));
+               $this->assertTrue(isset($inlineLanguageLabels['feUser']));
+       }
+
+       /**
+        * Tests the addInlineLanguageLabelFile() method.
+        *
+        * @test
+        */
+       public function areInlineLanguageLablesSelectedAndStripped() {
+               $this->fixture->setLanguage($GLOBALS['LANG']->lang);
+               $this->fixture->addInlineLanguageLabelFile(
+                       'EXT:lang/locallang_core.xml',
+                       'labels.',
+                       'lock'
+               );
+
+               $inlineLanguageLabels = $this->fixture->getInlineLanguageLabels();
+               $this->assertTrue(isset($inlineLanguageLabels['edRecord']));
+               $this->assertTrue(isset($inlineLanguageLabels['edRecord_content']));
+               $this->assertTrue(isset($inlineLanguageLabels['edRecordUser']));
+       }
 }
 ?>
\ No newline at end of file