[icinga-checkins] icinga.org: icingaweb2/feature/redesign-7144: Fix locale setup

git at icinga.org git at icinga.org
Thu Nov 13 21:23:01 CET 2014


Module: icingaweb2
Branch: feature/redesign-7144
Commit: 81b144d057a7bf4384fd0f890ca7e627b009e177
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=81b144d057a7bf4384fd0f890ca7e627b009e177

Author: Eric Lippmann <eric.lippmann at netways.de>
Date:   Thu Nov 13 18:02:03 2014 +0100

Fix locale setup

The CLI must not try to use HTTP_ACCEPT_LANGUAGE. Avoided double try-catch blocks.

refs #6073

---

 .../Icinga/Application/ApplicationBootstrap.php    |   27 +++++++++++++-------
 library/Icinga/Application/Web.php                 |   21 +++++----------
 2 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/library/Icinga/Application/ApplicationBootstrap.php b/library/Icinga/Application/ApplicationBootstrap.php
index 29c91f5..93b89bf 100644
--- a/library/Icinga/Application/ApplicationBootstrap.php
+++ b/library/Icinga/Application/ApplicationBootstrap.php
@@ -507,24 +507,33 @@ abstract class ApplicationBootstrap
     }
 
     /**
-     * Setup internationalization using gettext
+     * Detect the locale
      *
-     * Uses the preferred language sent by the browser or the default one
+     * @return null|string
+     */
+    protected function detectLocale()
+    {
+        return null;
+    }
+
+    /**
+     * Set up internationalization using gettext
      *
-     * @return  self
+     * @return $this
      */
-    protected function setupInternationalization()
+    protected final function setupInternationalization()
     {
         if ($this->hasLocales()) {
             Translator::registerDomain(Translator::DEFAULT_DOMAIN, $this->getLocaleDir());
         }
 
+        $locale = $this->detectLocale();
+        if ($locale === null) {
+            $locale = Translator::DEFAULT_LOCALE;
+        }
+
         try {
-            Translator::setupLocale(
-                isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])
-                    ? Translator::getPreferredLocaleCode($_SERVER['HTTP_ACCEPT_LANGUAGE'])
-                    : Translator::DEFAULT_LOCALE
-            );
+            Translator::setupLocale($locale);
         } catch (Exception $error) {
             Logger::error($error);
         }
diff --git a/library/Icinga/Application/Web.php b/library/Icinga/Application/Web.php
index 3f08d0b..06a3726 100644
--- a/library/Icinga/Application/Web.php
+++ b/library/Icinga/Application/Web.php
@@ -313,25 +313,16 @@ class Web extends ApplicationBootstrap
      *
      * @return  self
      */
-    protected function setupInternationalization()
+    protected function detectLocale()
     {
-        parent::setupInternationalization();
-
         $auth = Manager::getInstance();
-
-        if ($auth->isAuthenticated() &&
-            ($locale = $auth->getUser()->getPreferences()->getValue('icingaweb', 'language')) !== null
+        if (! $auth->isAuthenticated()
+            || ($locale = $auth->getUser()->getPreferences()->getValue('icingaweb', 'language')) === null
+            && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])
         ) {
-            try {
-                Translator::setupLocale($locale);
-            } catch (Exception $error) {
-                Logger::warning(
-                    'Cannot set locale "' . $locale . '" configured in ' .
-                    'preferences of user "' . $this->user->getUsername() . '"'
-                );
-            }
+            $locale = Translator::getPreferredLocaleCode($_SERVER['HTTP_ACCEPT_LANGUAGE']);
         }
-        return $this;
+        return $locale;
     }
 
     /**



More information about the icinga-checkins mailing list