diff --git a/library/Icinga/Application/ApplicationBootstrap.php b/library/Icinga/Application/ApplicationBootstrap.php index 29c91f5d9..93b89bfbd 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 self + * @return null|string */ - protected function setupInternationalization() + protected function detectLocale() + { + return null; + } + + /** + * Set up internationalization using gettext + * + * @return $this + */ + 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 3f08d0b64..06a37267d 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; } /**