Fix locale setup

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

refs #6073
This commit is contained in:
Eric Lippmann 2014-11-13 18:02:03 +01:00
parent e3c70bec6d
commit 81b144d057
2 changed files with 25 additions and 25 deletions

View File

@ -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);
}

View File

@ -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;
}
/**