[TASK] Move BE Login BrowserLanguage detection to Locales 34/46734/2
authorBenni Mack <benni@typo3.org>
Tue, 16 Feb 2016 21:59:59 +0000 (22:59 +0100)
committerBenni Mack <benni@typo3.org>
Wed, 17 Feb 2016 09:37:33 +0000 (10:37 +0100)
The CharsetConverter contains the calculation to find the
right language based on the browsers language settings
if no backend user has been logged in (Backend Login),
and chooses the TYPO3 language based on that.

This code does not belong to the CharsetConverter but
rather to the locales object.

Resolves: #73511
Releases: master
Change-Id: I4361d0e3a842efd0c5b69837776fe0c397edd593
Reviewed-on: https://review.typo3.org/46734
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Sebastian Fischer <typo3@evoweb.de>
Reviewed-by: Thomas Schlumberger <thomas@b13.de>
Tested-by: Thomas Schlumberger <thomas@b13.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/backend/Classes/Controller/LoginController.php
typo3/sysext/core/Classes/Charset/CharsetConverter.php
typo3/sysext/core/Classes/Localization/Locales.php
typo3/sysext/core/Classes/Page/PageRenderer.php
typo3/sysext/core/Documentation/Changelog/master/Deprecation-73511-BrowserLanguageDetectionMovedToLocales.rst [new file with mode: 0644]

index 1265553..a0b5b72 100644 (file)
@@ -20,9 +20,9 @@ use TYPO3\CMS\Backend\Exception;
 use TYPO3\CMS\Backend\LoginProvider\LoginProviderInterface;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Backend\LoginProvider\LoginProviderInterface;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
-use TYPO3\CMS\Core\Charset\CharsetConverter;
 use TYPO3\CMS\Core\FormProtection\BackendFormProtection;
 use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
 use TYPO3\CMS\Core\FormProtection\BackendFormProtection;
 use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
+use TYPO3\CMS\Core\Localization\Locales;
 use TYPO3\CMS\Core\Page\PageRenderer;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\HttpUtility;
 use TYPO3\CMS\Core\Page\PageRenderer;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\HttpUtility;
@@ -100,9 +100,9 @@ class LoginController
         $this->submitValue = GeneralUtility::_GP('commandLI');
 
         // Try to get the preferred browser language
         $this->submitValue = GeneralUtility::_GP('commandLI');
 
         // Try to get the preferred browser language
-        /** @var CharsetConverter $charsetConverter */
-        $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
-        $preferredBrowserLanguage = $charsetConverter
+        /** @var Locales $locales */
+        $locales = GeneralUtility::makeInstance(Locales::class);
+        $preferredBrowserLanguage = $locales
             ->getPreferredClientLanguage(GeneralUtility::getIndpEnv('HTTP_ACCEPT_LANGUAGE'));
 
         // If we found a $preferredBrowserLanguage and it is not the default language and no be_user is logged in
             ->getPreferredClientLanguage(GeneralUtility::getIndpEnv('HTTP_ACCEPT_LANGUAGE'));
 
         // If we found a $preferredBrowserLanguage and it is not the default language and no be_user is logged in
index 7265bbf..65ff728 100644 (file)
@@ -224,6 +224,7 @@ class CharsetConverter implements SingletonInterface
      * Empty values means "utf-8"
      *
      * @var array
      * Empty values means "utf-8"
      *
      * @var array
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use Locales
      */
     public $charSetArray = array(
         'af' => '',
      */
     public $charSetArray = array(
         'af' => '',
@@ -1417,62 +1418,14 @@ class CharsetConverter implements SingletonInterface
      *
      * @param string $languageCodesList List of language codes. something like 'de,en-us;q=0.9,de-de;q=0.7,es-cl;q=0.6,en;q=0.4,es;q=0.3,zh;q=0.1'
      * @return string A preferred language that TYPO3 supports, or "default" if none found
      *
      * @param string $languageCodesList List of language codes. something like 'de,en-us;q=0.9,de-de;q=0.7,es-cl;q=0.6,en;q=0.4,es;q=0.3,zh;q=0.1'
      * @return string A preferred language that TYPO3 supports, or "default" if none found
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use Locales::getPreferredClientLanguage() for usage
      */
     public function getPreferredClientLanguage($languageCodesList)
     {
      */
     public function getPreferredClientLanguage($languageCodesList)
     {
-        $allLanguageCodes = $this->getAllLanguageCodes();
-        $selectedLanguage = 'default';
-        $preferredLanguages = GeneralUtility::trimExplode(',', $languageCodesList);
-        // Order the preferred languages after they key
-        $sortedPreferredLanguages = array();
-        foreach ($preferredLanguages as $preferredLanguage) {
-            $quality = 1.0;
-            if (strpos($preferredLanguage, ';q=') !== false) {
-                list($preferredLanguage, $quality) = explode(';q=', $preferredLanguage);
-            }
-            $sortedPreferredLanguages[$preferredLanguage] = $quality;
-        }
-        // Loop through the languages, with the highest priority first
-        arsort($sortedPreferredLanguages, SORT_NUMERIC);
-        foreach ($sortedPreferredLanguages as $preferredLanguage => $quality) {
-            if (isset($allLanguageCodes[$preferredLanguage])) {
-                $selectedLanguage = $allLanguageCodes[$preferredLanguage];
-                break;
-            }
-            // Strip the country code from the end
-            list($preferredLanguage, ) = explode('-', $preferredLanguage);
-            if (isset($allLanguageCodes[$preferredLanguage])) {
-                $selectedLanguage = $allLanguageCodes[$preferredLanguage];
-                break;
-            }
-        }
-        if (!$selectedLanguage || $selectedLanguage === 'en') {
-            $selectedLanguage = 'default';
-        }
-        return $selectedLanguage;
-    }
-
-    /**
-     * Merges all available charsets and locales, currently only used for getPreferredClientLanguage()
-     *
-     * @return array
-     */
-    protected function getAllLanguageCodes()
-    {
-        // Get all languages where TYPO3 code is the same as the ISO code
-        $typo3LanguageCodes = array_keys($this->charSetArray);
-        $allLanguageCodes = array_combine($typo3LanguageCodes, $typo3LanguageCodes);
-        // Get all languages where TYPO3 code differs from ISO code
-        // or needs the country part
-        // the iso codes will here overwrite the default typo3 language in the key
+        GeneralUtility::logDeprecatedFunction();
         /** @var Locales $locales */
         $locales = GeneralUtility::makeInstance(Locales::class);
         /** @var Locales $locales */
         $locales = GeneralUtility::makeInstance(Locales::class);
-        foreach ($locales->getIsoMapping() as $typo3Lang => $isoLang) {
-            $isoLang = join('-', explode('_', $isoLang));
-            $allLanguageCodes[$typo3Lang] = $isoLang;
-        }
-        // Move the iso codes to the (because we're comparing the keys with "isset" later on)
-        return array_flip($allLanguageCodes);
+        return $locales->getPreferredClientLanguage($languageCodesList);
     }
 
     /********************************************
     }
 
     /********************************************
index f57d784..25ff995 100644 (file)
@@ -20,12 +20,8 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
 /**
  * Locales.
  *
 /**
  * Locales.
  *
- * Defining backend system languages
+ * Used to define backend system languages
  * When adding new keys, remember to:
  * When adding new keys, remember to:
- * - Add character encoding for lang. key in \TYPO3\CMS\Core\Charset\CharsetConverter
- * (default for new languages is "utf-8")
- * - Add mappings for language in \TYPO3\CMS\Core\Charset\CharsetConverter
- * (TYPO3/ISO, language/script, script/charset)
  * - Update 'setup' extension labels (sysext/setup/Resources/Private/Language/locallang.xlf)
  * That's it!
  */
  * - Update 'setup' extension labels (sysext/setup/Resources/Private/Language/locallang.xlf)
  * That's it!
  */
@@ -214,4 +210,55 @@ class Locales implements \TYPO3\CMS\Core\SingletonInterface
         }
         return $dependencies;
     }
         }
         return $dependencies;
     }
+
+
+    /**
+     * Converts the language codes that we get from the client (usually HTTP_ACCEPT_LANGUAGE)
+     * into a TYPO3-readable language code
+     *
+     * @param string $languageCodesList List of language codes. something like 'de,en-us;q=0.9,de-de;q=0.7,es-cl;q=0.6,en;q=0.4,es;q=0.3,zh;q=0.1'
+     * @return string A preferred language that TYPO3 supports, or "default" if none found
+     */
+    public function getPreferredClientLanguage($languageCodesList)
+    {
+        $allLanguageCodesFromLocales = [];
+        foreach ($this->getIsoMapping() as $typo3Lang => $isoLang) {
+            $isoLang = str_replace('_', '-', $isoLang);
+            $allLanguageCodesFromLocales[$isoLang] = $typo3Lang;
+        }
+        foreach ($this->getLocales() as $locale) {
+            $locale = str_replace('_', '-', $locale);
+            $allLanguageCodesFromLocales[$locale] = $locale;
+        }
+        $selectedLanguage = 'default';
+        $preferredLanguages = GeneralUtility::trimExplode(',', $languageCodesList);
+        // Order the preferred languages after they key
+        $sortedPreferredLanguages = array();
+        foreach ($preferredLanguages as $preferredLanguage) {
+            $quality = 1.0;
+            if (strpos($preferredLanguage, ';q=') !== false) {
+                list($preferredLanguage, $quality) = explode(';q=', $preferredLanguage);
+            }
+            $sortedPreferredLanguages[$preferredLanguage] = $quality;
+        }
+        // Loop through the languages, with the highest priority first
+        arsort($sortedPreferredLanguages, SORT_NUMERIC);
+        foreach ($sortedPreferredLanguages as $preferredLanguage => $quality) {
+            if (isset($allLanguageCodes[$preferredLanguage])) {
+                $selectedLanguage = $allLanguageCodes[$preferredLanguage];
+                break;
+            }
+            // Strip the country code from the end
+            list($preferredLanguage, ) = explode('-', $preferredLanguage);
+            if (isset($allLanguageCodes[$preferredLanguage])) {
+                $selectedLanguage = $allLanguageCodes[$preferredLanguage];
+                break;
+            }
+        }
+        if (!$selectedLanguage || $selectedLanguage === 'en') {
+            $selectedLanguage = 'default';
+        }
+        return $selectedLanguage;
+    }
+
 }
 }
index b3e2014..c661fdb 100644 (file)
@@ -2073,7 +2073,6 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface
             // Swedish
             $localeMap['se'] = 'se_SV';
             $extJsLang = isset($localeMap[$this->lang]) ? $localeMap[$this->lang] : $this->lang;
             // Swedish
             $localeMap['se'] = 'se_SV';
             $extJsLang = isset($localeMap[$this->lang]) ? $localeMap[$this->lang] : $this->lang;
-            // @todo autoconvert file from UTF8 to current BE charset if necessary!!!!
             $extJsLocaleFile = $this->extJsPath . 'locale/ext-lang-' . $extJsLang . '.js';
             if (file_exists(PATH_typo3 . $extJsLocaleFile)) {
                 $out .= '<script src="' . $this->processJsFile(($this->backPath . $extJsLocaleFile)) . '" type="text/javascript" charset="utf-8"></script>' . LF;
             $extJsLocaleFile = $this->extJsPath . 'locale/ext-lang-' . $extJsLang . '.js';
             if (file_exists(PATH_typo3 . $extJsLocaleFile)) {
                 $out .= '<script src="' . $this->processJsFile(($this->backPath . $extJsLocaleFile)) . '" type="text/javascript" charset="utf-8"></script>' . LF;
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-73511-BrowserLanguageDetectionMovedToLocales.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-73511-BrowserLanguageDetectionMovedToLocales.rst
new file mode 100644 (file)
index 0000000..9d1e33d
--- /dev/null
@@ -0,0 +1,30 @@
+================================================================
+Deprecation: #73511 - BrowserLanguage detection moved to Locales
+================================================================
+
+Description
+===========
+
+The CharsetConverter contained the calculation to find the right language based on the browsers language
+settings at the backend login screen.
+
+The according code was moved to TYPO3\CMS\Localization\Locales. The method ``CharsetConverter::getPreferredClientLanguage()`` and the property ``CharsetConverter::charSetArray`` have
+been marked as deprecated.
+
+
+Impact
+======
+
+Calling ``CharsetConverter::getPreferredClientLanguage()`` will trigger a deprecation log entry.
+
+
+Affected Installations
+======================
+
+All installations with a third-party extension using the CharsetConverter language resolving directly.
+
+
+Migration
+=========
+
+Use the method ``Locales->getPreferredClientLanguage()`` instead.
\ No newline at end of file