[+BUGFIX] Extbase (Utility): Make _LOCAL_LANG override work with dots
authorSebastian Kurfürst <sebastian@typo3.org>
Tue, 18 Jan 2011 18:00:19 +0000 (18:00 +0000)
committerSebastian Kurfürst <sebastian@typo3.org>
Tue, 18 Jan 2011 18:00:19 +0000 (18:00 +0000)
When dealing with Locallang files, it is a common approach to
write locallang keys with dots, as example tx_foo.bar.
You can overwrite these keys easily by Typoscript _LOCAL_LANG,
and with this patch this also works with keys containing dots.

Thanks to Thomas Maroschik for the initial implementation!

Resolves: #12121

typo3/sysext/extbase/Classes/Utility/Localization.php
typo3/sysext/extbase/Tests/Unit/Utility/LocalizationTest.php [new file with mode: 0644]

index cf9e0d5..277b4fa 100644 (file)
@@ -83,7 +83,6 @@ class Tx_Extbase_Utility_Localization {
                        $value = self::translateFileReference($key);
                } else {
                        self::initializeLocalization($extensionName);
-
                        // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
                        if (isset(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key])) {
                                $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key];
@@ -205,12 +204,43 @@ class Tx_Extbase_Utility_Localization {
                                        } else {
                                                self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['TSFE']->csConvObj->charSetArray[$languageKey];
                                        }
+                               } elseif (is_array($labelValue)) {
+                                       $labelValue = self::flattenTypoScriptLabelArray($labelValue, $labelKey);
+                                       self::$LOCAL_LANG[$extensionName][$languageKey] = array_merge(self::$LOCAL_LANG[$extensionName][$languageKey], $labelValue);
                                }
                        }
                }
        }
 
        /**
+        * Flatten TypoScript label array; converting a hierarchical array into a flat
+        * array with the keys separated by dots.
+        *
+        * Example Input:  array('k1' => array('subkey1' => 'val1'))
+        * Example Output: array('k1.subkey1' => 'val1')
+        *
+        * @param array $labelValues Hierarchical array of labels
+        * @param string $parentKey the name of the parent key in the recursion; is only needed for recursion.
+        * @return array flattened array of labels.
+        */
+       protected function flattenTypoScriptLabelArray(array $labelValues, $parentKey = '') {
+               $result = array();
+               foreach ($labelValues as $key => $labelValue) {
+                       if (!empty($parentKey)) {
+                               $key = $parentKey . '.' . $key;
+                       }
+
+                       if (is_array($labelValue)) {
+                               $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key);
+                               $result = array_merge($result, $labelValue);
+                       } else {
+                               $result[$key] = $labelValue;
+                       }
+               }
+               return $result;
+       }
+
+       /**
         * Converts a string from the specified character set to the current.
         * The current charset is defined by the TYPO3 mode.
         *
diff --git a/typo3/sysext/extbase/Tests/Unit/Utility/LocalizationTest.php b/typo3/sysext/extbase/Tests/Unit/Utility/LocalizationTest.php
new file mode 100644 (file)
index 0000000..a2984c2
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2011 Extbase Team
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * Testcase for class Tx_Extbase_Utility_Localization
+ *
+ * @package Extbase
+ * @subpackage Utility
+ */
+class Tx_Extbase_Tests_Unit_Utility_LocalizationTest extends Tx_Extbase_BaseTestCase {
+
+       /**
+        * @var Tx_Extbase_Utility_Localization
+        */
+       protected $localization;
+
+       public function setUp() {
+               $this->localization = $this->getAccessibleMock('Tx_Extbase_Utility_Localization', array('dummy'));
+       }
+
+       public function tearDown() {
+               $this->localization = NULL;
+       }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function implodeTypoScriptLabelArrayWorks() {
+               $expected = array(
+                       'key1' => 'value1',
+                       'key2' => 'value2',
+                       'key3.subkey1' => 'subvalue1',
+                       'key3.subkey2.subsubkey' => 'val'
+               );
+               $actual = $this->localization->_call('flattenTypoScriptLabelArray', array(
+                       'key1' => 'value1',
+                       'key2' => 'value2',
+                       'key3' => array(
+                               'subkey1' => 'subvalue1',
+                               'subkey2' => array(
+                                       'subsubkey' => 'val'
+                               )
+                       )
+               ));
+               $this->assertEquals($expected, $actual);
+       }
+}
+?>
\ No newline at end of file