[BUGFIX] pi_getLL() alternative language translation fails
authorXavier Perseguers <xavier@typo3.org>
Thu, 16 Aug 2012 10:44:07 +0000 (12:44 +0200)
committerXavier Perseguers <xavier@typo3.org>
Wed, 19 Sep 2012 10:07:57 +0000 (12:07 +0200)
The translation fallback introduced with TYPO3 4.6 is not taken
into account by the pi_getLL() method.

Change-Id: I9c60d14e903eba1cc0d19ea8cd3a58f5365abfa1
Fixes: #39850
Releases: 6.0, 4.7, 4.6
Reviewed-on: http://review.typo3.org/14238
Reviewed-by: Markus Klein
Reviewed-by: Xavier Perseguers
Tested-by: Xavier Perseguers
typo3/sysext/cms/tslib/class.tslib_pibase.php

index bdc7eec..c651a1e 100644 (file)
@@ -155,7 +155,17 @@ class tslib_pibase {
                }
                if (!empty($GLOBALS['TSFE']->config['config']['language'])) {
                        $this->LLkey = $GLOBALS['TSFE']->config['config']['language'];
-                       if (!empty($GLOBALS['TSFE']->config['config']['language_alt'])) {
+                       if (empty($GLOBALS['TSFE']->config['config']['language_alt'])) {
+                               /** @var $locales t3lib_l10n_Locales */
+                               $locales = t3lib_div::makeInstance('t3lib_l10n_Locales');
+                               if (in_array($this->LLkey, $locales->getLocales())) {
+                                       $this->altLLkey = '';
+                                       foreach ($locales->getLocaleDependencies($this->LLkey) as $language) {
+                                               $this->altLLkey .= $language . ',';
+                                       }
+                                       $this->altLLkey = rtrim($this->altLLkey, ',');
+                               }
+                       } else {
                                $this->altLLkey = $GLOBALS['TSFE']->config['config']['language_alt'];
                        }
                }
@@ -877,13 +887,14 @@ class tslib_pibase {
         * Returns the localized label of the LOCAL_LANG key, $key
         * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
         *
-        * @param       string          The key from the LOCAL_LANG array for which to return the value.
-        * @param       string          Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
-        * @param       boolean         If TRUE, the output label is passed through htmlspecialchars()
-        * @return      string          The value from LOCAL_LANG.
+        * @param string $key The key from the LOCAL_LANG array for which to return the value.
+        * @param string $alternativeLabel Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
+        * @param boolean $hsc If TRUE, the output label is passed through htmlspecialchars()
+        * @return string The value from LOCAL_LANG.
         */
        public function pi_getLL($key, $alternativeLabel = '', $hsc = FALSE) {
-               if (isset($this->LOCAL_LANG[$this->LLkey][$key][0]['target'])) {
+               $word = NULL;
+               if (!empty($this->LOCAL_LANG[$this->LLkey][$key][0]['target'])) {
 
                                // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
                        if (isset($this->LOCAL_LANG_charset[$this->LLkey][$key])) {
@@ -894,28 +905,37 @@ class tslib_pibase {
                        } else {
                                $word = $this->LOCAL_LANG[$this->LLkey][$key][0]['target'];
                        }
-               } elseif ($this->altLLkey && isset($this->LOCAL_LANG[$this->altLLkey][$key][0]['target'])) {
-
-                               // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
-                       if (isset($this->LOCAL_LANG_charset[$this->altLLkey][$key])) {
-                               $word = $GLOBALS['TSFE']->csConv(
-                                       $this->LOCAL_LANG[$this->altLLkey][$key][0]['target'],
-                                       $this->LOCAL_LANG_charset[$this->altLLkey][$key]
-                               );
-                       } else {
-                               $word = $this->LOCAL_LANG[$this->altLLkey][$key][0]['target'];
+               } elseif ($this->altLLkey) {
+                       $alternativeLanguageKeys = t3lib_div::trimExplode(',', $this->altLLkey, TRUE);
+                       $alternativeLanguageKeys = array_reverse($alternativeLanguageKeys);
+                       foreach ($alternativeLanguageKeys as $languageKey) {
+                               if (!empty($this->LOCAL_LANG[$languageKey][$key][0]['target'])) {
+                                               // alternative language translation for key exists
+                                       $word = $this->LOCAL_LANG[$languageKey][$key][0]['target'];
+                                               // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
+                                       if (isset($this->LOCAL_LANG_charset[$languageKey][$key])) {
+                                               $word = $GLOBALS['TSFE']->csConv(
+                                                       $word,
+                                                       $this->LOCAL_LANG_charset[$this->altLLkey][$key]
+                                               );
+                                       }
+                                       break;
+                               }
                        }
-               } elseif (isset($this->LOCAL_LANG['default'][$key][0]['target'])) {
+               }
+               if ($word === NULL) {
+                       if (!empty($this->LOCAL_LANG['default'][$key][0]['target'])) {
 
-                               // Get default translation (without charset conversion, english)
-                       $word = $this->LOCAL_LANG['default'][$key][0]['target'];
-               } else {
+                                       // Get default translation (without charset conversion, english)
+                               $word = $this->LOCAL_LANG['default'][$key][0]['target'];
+                       } else {
 
-                               // Return alternative string or empty
-                       $word = (isset($this->LLtestPrefixAlt)) ? $this->LLtestPrefixAlt . $alternativeLabel : $alternativeLabel;
+                                       // Return alternative string or empty
+                               $word = isset($this->LLtestPrefixAlt) ? $this->LLtestPrefixAlt . $alternativeLabel : $alternativeLabel;
+                       }
                }
 
-               $output = (isset($this->LLtestPrefix)) ? $this->LLtestPrefix . $word : $word;
+               $output = isset($this->LLtestPrefix) ? $this->LLtestPrefix . $word : $word;
 
                if ($hsc) {
                        $output = htmlspecialchars($output);
@@ -938,9 +958,13 @@ class tslib_pibase {
                        $basePath = 'EXT:' . $this->extKey . '/' . dirname($this->scriptRelPath) . '/locallang.xml';
 
                                // Read the strings in the required charset (since TYPO3 4.2)
-                       $this->LOCAL_LANG = t3lib_div::readLLfile($basePath,$this->LLkey, $GLOBALS['TSFE']->renderCharset);
-                       if ($this->altLLkey) {
-                               $this->LOCAL_LANG = t3lib_div::readLLfile($basePath,$this->altLLkey);
+                       $this->LOCAL_LANG = t3lib_div::readLLfile($basePath, $this->LLkey, $GLOBALS['TSFE']->renderCharset);
+                       $alternativeLanguageKeys = t3lib_div::trimExplode(',', $this->altLLkey, TRUE);
+                       foreach ($alternativeLanguageKeys as $languageKey) {
+                               $tempLL = t3lib_div::readLLfile($basePath, $languageKey);
+                               if ($this->LLkey !== 'default' && isset($tempLL[$languageKey])) {
+                                       $this->LOCAL_LANG[$languageKey] = $tempLL[$languageKey];
+                               }
                        }
 
                                // Overlaying labels from TypoScript (including fictitious language keys for non-system languages!):