Ticket #9437: Tweak preferred language selection.
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 22 Dec 2010 09:33:42 +0000 (02:33 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 22 Dec 2010 18:05:03 +0000 (11:05 -0700)
I *think* this should be the behavior (precedence-wise):

Pre-auth
--------

Prefs will most likely be unavailable or default language is empty.
Language will be set based on language drop-down on login refresh (if
language switching allowed)
Session language is used (pre-auth session will have language set if the
login screen has been reloaded)
System-wide default used (nls config)
Finally, browser detection.

Post-auth
---------

Value in user's prefs will ALWAYS control (regardless of login screen
language selection)
Language from login screen will be used (if language switching allowed)
On initial login session is new/clean; language info unavailable here
On subsequent login, this contains the session language (code will
ALWAYS stop here after user is authenticated)
System-wide default used (nls config)
Finally, browser detection.

framework/Core/lib/Horde/Registry.php

index 890d877..8a18e5e 100644 (file)
@@ -1249,8 +1249,9 @@ class Horde_Registry
          * with the current language, and reset the language later. */
         $this->setLanguageEnvironment($GLOBALS['language'], $app);
 
-        /* Load config and prefs and set proper language from the prefs. */
-        $this->_onAppSwitch($app);
+        /* Load config and prefs. */
+        $this->importConfig($app);
+        $this->loadPrefs($app);
 
         /* Call post-push hook. */
         try {
@@ -1299,7 +1300,9 @@ class Horde_Registry
          * and set the gettext domain and the preferred language. */
         $app = $this->getApp();
         if ($app) {
-            $this->_onAppSwitch($app);
+            /* Load config and prefs. */
+            $this->importConfig($app);
+            $this->loadPrefs($app);
             $this->setTextdomain(
                 $app,
                 $this->get('fileroot', $app) . '/locale'
@@ -1310,30 +1313,6 @@ class Horde_Registry
     }
 
     /**
-     * Code to run when switching to an application.
-     *
-     * @param string $app  The application name.
-     *
-     * @throws Horde_Exception
-     */
-    protected function _onAppSwitch($app)
-    {
-        /* Import this application's configuration values. */
-        $this->importConfig($app);
-
-        /* Load preferences after the configuration has been loaded to make
-         * sure the prefs file has all the information it needs. */
-        $this->loadPrefs($app);
-
-        /* Reset the language in case there is a different one selected in the
-         * preferences. */
-        $language = $GLOBALS['prefs']->getValue('language');
-        if ($language != $GLOBALS['language']) {
-            $this->setLanguageEnvironment($language, $app);
-        }
-    }
-
-    /**
      * Return the current application - the app at the top of the application
      * stack.
      *
@@ -2180,22 +2159,29 @@ class Horde_Registry
      */
     public function preferredLang($lang = null)
     {
-        /* First, check if language pref is locked and, if so, set it to its
-         * value */
+        /* If language pref exists, we should use that. */
         if (isset($GLOBALS['prefs']) &&
-            $GLOBALS['prefs']->isLocked('language')) {
-            $language = $GLOBALS['prefs']->getValue('language');
+            ($language = $GLOBALS['prefs']->getValue('language'))) {
+            return basename($language);
+        }
+
         /* Check if the user selected a language from the login screen */
-        } elseif (!empty($lang) && $this->isValidLang($lang)) {
-            $language = $lang;
+        if (!empty($lang) && $this->isValidLang($lang)) {
+            return basename($lang);
+        }
+
         /* Check if we have a language set in the session */
-        } elseif ($GLOBALS['session']->exists('horde', 'language')) {
-            $language = $GLOBALS['session']->get('horde', 'language');
+        if ($GLOBALS['session']->exists('horde', 'language')) {
+            return basename($GLOBALS['session']->get('horde', 'language'));
+        }
+
         /* Use site-wide default, if one is defined */
-        } elseif (!empty($this->nlsconfig['defaults']['language'])) {
-            $language = $this->nlsconfig['defaults']['language'];
+        if (!empty($this->nlsconfig['defaults']['language'])) {
+            return basename($this->nlsconfig['defaults']['language']);
+        }
+
         /* Try browser-accepted languages. */
-        } elseif (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+        if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
             /* The browser supplies a list, so return the first valid one. */
             $browser_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
             foreach ($browser_langs as $lang) {
@@ -2203,6 +2189,7 @@ class Horde_Registry
                 if (($pos = strpos($lang, ';')) !== false) {
                     $lang = substr($lang, 0, $pos);
                 }
+
                 $lang = $this->_mapLang(trim($lang));
                 if ($this->isValidLang($lang)) {
                     $language = $lang;