[CLEANUP] Replace wrong/old file copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / LocalizationUtility.php
index f939b47..d537e29 100644 (file)
@@ -4,7 +4,8 @@ namespace TYPO3\CMS\Extbase\Utility;
 /***************************************************************
  *  Copyright notice
  *
- *  (c) 2009 Sebastian Kurf├╝rst <sebastian@typo3.org>
+ *  (c) 2010-2012 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
+ *  Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
  *  All rights reserved
  *
  *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -15,6 +16,9 @@ namespace TYPO3\CMS\Extbase\Utility;
  *
  *  The GNU General Public License can be found at
  *  http://www.gnu.org/copyleft/gpl.html.
+ *  A copy is found in the textfile GPL.txt and important notices to the license
+ *  from the author is found in LICENSE.txt distributed with these scripts.
+ *
  *
  *  This script is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -26,9 +30,6 @@ namespace TYPO3\CMS\Extbase\Utility;
 /**
  * Localization helper which should be used to fetch localized labels.
  *
- * @package Extbase
- * @subpackage Utility
- * @version $ID:$
  * @api
  */
 class LocalizationUtility {
@@ -62,9 +63,9 @@ class LocalizationUtility {
        /**
         * Pointer to alternative fall-back language to use
         *
-        * @var string
+        * @var array
         */
-       static protected $alternativeLanguageKey = '';
+       static protected $alternativeLanguageKeys = array();
 
        /**
         * Returns the localized label of the LOCAL_LANG key, $key.
@@ -86,37 +87,29 @@ class LocalizationUtility {
                } 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])) {
-                               if (is_array(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key])) {
-                                       // TYPO3 >= 4.6
-                                       $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'];
-                               } else {
-                                       // TYPO3 < 4.6
-                                       $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key];
-                               }
-                               if (isset(self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key])) {
+                       if (!empty(self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'])) {
+                               // Local language translation for key exists
+                               $value = self::$LOCAL_LANG[$extensionName][self::$languageKey][$key][0]['target'];
+                               if (!empty(self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key])) {
                                        $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][self::$languageKey][$key]);
                                }
-                       } elseif (self::$alternativeLanguageKey !== '' && isset(self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key])) {
-                               if (is_array(self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key])) {
-                                       // TYPO3 >= 4.6
-                                       $value = self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key][0]['target'];
-                               } else {
-                                       // TYPO3 < 4.6
-                                       $value = self::$LOCAL_LANG[$extensionName][self::$alternativeLanguageKey][$key];
-                               }
-                               if (isset(self::$LOCAL_LANG_charset[$extensionName][self::$alternativeLanguageKey][$key])) {
-                                       $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][self::$alternativeLanguageKey][$key]);
+                       } elseif (count(self::$alternativeLanguageKeys)) {
+                               $languages = array_reverse(self::$alternativeLanguageKeys);
+                               foreach ($languages as $language) {
+                                       if (!empty(self::$LOCAL_LANG[$extensionName][$language][$key][0]['target'])) {
+                                               // Alternative language translation for key exists
+                                               $value = self::$LOCAL_LANG[$extensionName][$language][$key][0]['target'];
+                                               if (!empty(self::$LOCAL_LANG_charset[$extensionName][$language][$key])) {
+                                                       $value = self::convertCharset($value, self::$LOCAL_LANG_charset[$extensionName][$language][$key]);
+                                               }
+                                               break;
+                                       }
                                }
-                       } elseif (isset(self::$LOCAL_LANG[$extensionName]['default'][$key])) {
+                       }
+                       if ($value === NULL && !empty(self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'])) {
+                               // Default language translation for key exists
                                // No charset conversion because default is English and thereby ASCII
-                               if (is_array(self::$LOCAL_LANG[$extensionName]['default'][$key])) {
-                                       // TYPO3 >= 4.6
-                                       $value = self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'];
-                               } else {
-                                       // TYPO3 < 4.6
-                                       $value = self::$LOCAL_LANG[$extensionName]['default'][$key];
-                               }
+                               $value = self::$LOCAL_LANG[$extensionName]['default'][$key][0]['target'];
                        }
                }
                if (is_array($arguments) && $value !== NULL) {
@@ -160,13 +153,15 @@ class LocalizationUtility {
                if (isset(self::$LOCAL_LANG[$extensionName])) {
                        return;
                }
-               $locallangPathAndFilename = ((('EXT:' . \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName)) . '/') . self::$locallangPath) . 'locallang.xml';
+               $locallangPathAndFilename = 'EXT:' . \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xml';
                self::setLanguageKeys();
                $renderCharset = TYPO3_MODE === 'FE' ? $GLOBALS['TSFE']->renderCharset : $GLOBALS['LANG']->charSet;
                self::$LOCAL_LANG[$extensionName] = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($locallangPathAndFilename, self::$languageKey, $renderCharset);
-               if (self::$alternativeLanguageKey !== '') {
-                       $alternativeLocalLang = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($locallangPathAndFilename, self::$alternativeLanguageKey);
-                       self::$LOCAL_LANG[$extensionName] = array_merge(self::$LOCAL_LANG[$extensionName], $alternativeLocalLang);
+               foreach (self::$alternativeLanguageKeys as $language) {
+                       $tempLL = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($locallangPathAndFilename, $language, $renderCharset);
+                       if (self::$languageKey !== 'default' && isset($tempLL[$language])) {
+                               self::$LOCAL_LANG[$extensionName][$language] = $tempLL[$language];
+                       }
                }
                self::loadTypoScriptLabels($extensionName);
        }
@@ -181,12 +176,20 @@ class LocalizationUtility {
         */
        protected function setLanguageKeys() {
                self::$languageKey = 'default';
-               self::$alternativeLanguageKey = '';
+               self::$alternativeLanguageKeys = array();
                if (TYPO3_MODE === 'FE') {
                        if (isset($GLOBALS['TSFE']->config['config']['language'])) {
                                self::$languageKey = $GLOBALS['TSFE']->config['config']['language'];
                                if (isset($GLOBALS['TSFE']->config['config']['language_alt'])) {
-                                       self::$alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
+                                       self::$alternativeLanguageKeys[] = $GLOBALS['TSFE']->config['config']['language_alt'];
+                               } else {
+                                       /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
+                                       $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
+                                       if (in_array(self::$languageKey, $locales->getLocales())) {
+                                               foreach ($locales->getLocaleDependencies(self::$languageKey) as $language) {
+                                                       self::$alternativeLanguageKeys[] = $language;
+                                               }
+                                       }
                                }
                        }
                } elseif (strlen($GLOBALS['BE_USER']->uc['lang']) > 0) {
@@ -217,18 +220,17 @@ class LocalizationUtility {
                        }
                        foreach ($labels as $labelKey => $labelValue) {
                                if (is_string($labelValue)) {
-                                       self::$LOCAL_LANG[$extensionName][$languageKey][$labelKey] = $labelValue;
-                                       // For labels coming from the TypoScript (database) the charset is assumed to be "forceCharset" and if that is not set, assumed to be that of the individual system languages
-                                       if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) && strlen($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) > 0) {
-                                               self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
-                                       } elseif (is_object($GLOBALS['LANG'])) {
+                                       self::$LOCAL_LANG[$extensionName][$languageKey][$labelKey][0]['target'] = $labelValue;
+                                       if (is_object($GLOBALS['LANG'])) {
                                                self::$LOCAL_LANG_charset[$extensionName][$languageKey][$labelKey] = $GLOBALS['LANG']->csConvObj->charSetArray[$languageKey];
                                        } 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);
+                                       foreach ($labelValue as $key => $value) {
+                                               self::$LOCAL_LANG[$extensionName][$languageKey][$key][0]['target'] = $value;
+                                       }
                                }
                        }
                }
@@ -249,7 +251,7 @@ class LocalizationUtility {
                $result = array();
                foreach ($labelValues as $key => $labelValue) {
                        if (!empty($parentKey)) {
-                               $key = ($parentKey . '.') . $key;
+                               $key = $parentKey . '.' . $key;
                        }
                        if (is_array($labelValue)) {
                                $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key);
@@ -277,8 +279,6 @@ class LocalizationUtility {
                        return $convertedValue !== NULL ? $convertedValue : $value;
                }
        }
-
 }
 
-
 ?>
\ No newline at end of file