[BUGFIX] Numeric translation keys aren't translated right in XML files 15/17615/6
authorReinhard Führicht <rf@typoheads.at>
Mon, 21 Jan 2013 08:52:35 +0000 (09:52 +0100)
committerAndreas Wolf <andreas.wolf@typo3.org>
Sat, 9 Feb 2013 22:11:54 +0000 (23:11 +0100)
When working with the "old" XML based translation files,
numeric keys aren't translated properly.
The reason is, that LocallangXmlParser uses array_merge to combine the
translation arrays of the default language and the current language.
array_merge renumbers numeric keys. Therefore, numeric key in
translation files result in wrong translations in Frontend.

Fixes: #44626
Releases: 6.0, 6.1
Change-Id: I8fdff385f681428b4cb5a3e994828a4a29305c98
Reviewed-on: https://review.typo3.org/17615
Reviewed-by: Andreas Wolf
Tested-by: Andreas Wolf
typo3/sysext/core/Classes/Localization/Parser/LocallangXmlParser.php
typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/locallangNumericKeys.xml [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Localization/Parser/LocallangXmlParserTest.php

index e177600..5ede9cd 100644 (file)
@@ -85,7 +85,7 @@ class LocallangXmlParser extends \TYPO3\CMS\Core\Localization\Parser\AbstractXml
                }
                if ($element === 'target' && isset($localizedBodyOfFileTag[0]) && $localizedBodyOfFileTag[0] instanceof \SimpleXMLElement) {
                        $parsedDataTarget = $this->getParsedDataForElement($localizedBodyOfFileTag[0], $element);
-                       $mergedData = array_merge($parsedData, $parsedDataTarget);
+                       $mergedData = $parsedDataTarget + $parsedData;
                        if ($this->languageKey === 'default') {
                                $parsedData = array_intersect_key($mergedData, $parsedData, $parsedDataTarget);
                        } else {
diff --git a/typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/locallangNumericKeys.xml b/typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/locallangNumericKeys.xml
new file mode 100644 (file)
index 0000000..7facd53
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
+<T3locallang>
+       <meta type="array">
+               <description>Fixture labels for unit tests.</description>
+               <type>module</type>
+       </meta>
+       <data type="array">
+               <languageKey index="default" type="array">
+                       <label index="1">This is label #1</label>
+                       <label index="2">This is label #2</label>
+                       <label index="3">This is label #3</label>
+                       <label index="5">This is label #5</label>
+                       <label index="10">This is label #10</label>
+               </languageKey>
+               <languageKey index="fr" type="array">
+                       <label index="1">This is label #1 [FR]</label>
+                       <label index="2">This is label #2 [FR]</label>
+                       <label index="3">This is label #3 [FR]</label>
+                       <label index="5">This is label #5 [FR]</label>
+                       <label index="10">This is label #10 [FR]</label>
+               </languageKey>
+       </data>
+</T3locallang>
\ No newline at end of file
index f5ec1bd..7eeed35 100644 (file)
@@ -46,10 +46,10 @@ class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        protected $l10nPriority;
 
-       /**
-        * @var array
-        */
-       protected $llxmlFileNames;
+       protected static function getFixtureFilePath($filename) {
+                       // We have to take the whole relative path as otherwise this test fails on Windows systems
+               return PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/' . $filename;
+       }
 
        /**
         * Prepares the environment before running a test.
@@ -60,13 +60,6 @@ class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
                $this->parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Parser\\LocallangXmlParser');
 
-                       // We have to take the whole relative path as otherwise this test fails on Windows systems
-               $fixturePath = PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/';
-               $this->llxmlFileNames = array(
-                       'locallang' => $fixturePath . 'locallang.xml',
-                       'locallang_override' => $fixturePath . 'locallang_override.xml',
-                       'locallangOnlyDefaultLanguage' => $fixturePath . 'locallangOnlyDefaultLanguage.xml'
-               );
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
                \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
                        // Clear localization cache
@@ -88,7 +81,7 @@ class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function canParseLlxmlInEnglish() {
-               $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallang'], 'default');
+               $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default');
                $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
                $expectedLabels = array(
                        'label1' => 'This is label #1',
@@ -104,7 +97,7 @@ class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function canParseLlxmlInFrench() {
-               $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallang'], 'fr');
+               $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'fr');
                $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
                $expectedLabels = array(
                        'label1' => 'Ceci est le libellé no. 1',
@@ -120,7 +113,7 @@ class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound() {
-               $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallangOnlyDefaultLanguage'], 'fr');
+               $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallangOnlyDefaultLanguage.xml'), 'fr');
                $expectedLabels = array(
                        'label1' => NULL,
                        'label2' => NULL,
@@ -135,8 +128,8 @@ class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function canOverrideLlxml() {
-               $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$this->llxmlFileNames['locallang']][] = $this->llxmlFileNames['locallang_override'];
-               $LOCAL_LANG = array_merge(\TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($this->llxmlFileNames['locallang'], 'default'), \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($this->llxmlFileNames['locallang'], 'fr'));
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][self::getFixtureFilePath('locallang.xml')][] = self::getFixtureFilePath('locallang_override.xml');
+               $LOCAL_LANG = array_merge(\TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallang.xml'), 'default'), \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallang.xml'), 'fr'));
                $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
                $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
                $expectedLabels = array(
@@ -158,6 +151,27 @@ class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                }
        }
 
+       public function numericKeysDataProvider() {
+               $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallangNumericKeys.xml'), 'default');
+               $translations = array();
+
+               foreach ($LOCAL_LANG['default'] as $key => $labelData) {
+                       $translations['Numerical key ' . $key] = array($key, $labelData[0]['source'] . ' [FR]');
+               }
+
+               return $translations;
+       }
+
+       /**
+        * @test
+        * @dataProvider numericKeysDataProvider
+        */
+       public function canTranslateNumericKeys($key, $expectedResult) {
+               $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallangNumericKeys.xml'), 'fr');
+
+               $this->assertEquals($expectedResult, $LOCAL_LANG['fr'][$key][0]['target']);
+       }
+
 }
 
 ?>
\ No newline at end of file