From 188345ffd0941371b8fa01802437b270c24200d1 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Wed, 6 Aug 2014 13:05:34 -0500 Subject: [PATCH] i18n: Default to system primary language --- client.inc.php | 2 +- include/class.client.php | 3 +-- include/class.i18n.php | 25 +++++++++++++++++++++---- include/class.staff.php | 8 ++++---- include/class.translation.php | 3 +-- setup/setup.inc.php | 2 +- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/client.inc.php b/client.inc.php index bb8e8badb..2faee156f 100644 --- a/client.inc.php +++ b/client.inc.php @@ -48,7 +48,7 @@ $nav=null; $thisclient = UserAuthenticationBackend::getUser(); if (isset($_GET['lang']) && $_GET['lang']) { - $_SESSION['client:lang'] = $_GET['lang']; + Internationalization::setCurrentLanguage($_GET['lang']); } // Bootstrap gettext translations as early as possible, but after attempting diff --git a/include/class.client.php b/include/class.client.php index bfcffeaca..21d230047 100644 --- a/include/class.client.php +++ b/include/class.client.php @@ -271,7 +271,6 @@ class EndUser extends AuthenticatedUser { function getLanguage() { static $cached = false; - if (!$cached) $cached = &$_SESSION['client:lang']; if (!$cached) { if ($acct = $this->getAccount()) @@ -396,7 +395,7 @@ class ClientAccount extends UserAccount { $this->set('dst', isset($vars['dst']) ? 1 : 0); // Change language $this->set('lang', $vars['lang'] ?: null); - $_SESSION['client:lang'] = null; + Internationalization::setCurrentLanguage(null); TextDomain::configureForUser($this); if ($vars['backend']) { diff --git a/include/class.i18n.php b/include/class.i18n.php index d141439d1..2f5619c05 100644 --- a/include/class.i18n.php +++ b/include/class.i18n.php @@ -274,7 +274,7 @@ class Internationalization { if (empty($_SERVER["HTTP_ACCEPT_LANGUAGE"])) return $cfg->getPrimaryLanguage(); - $languages = self::availableLanguages(); + $languages = self::getConfiguredSystemLanguages(); // The Accept-Language header contains information about the // language preferences configured in the user's browser / operating @@ -348,18 +348,27 @@ class Internationalization { } } - return $best_match_langcode; + if (self::isLanguageInstalled($best_match_langcode)) + return $best_match_langcode; + else + return $cfg->getPrimaryLanguage(); } static function getCurrentLanguage($user=false) { global $thisstaff, $thisclient; + static $session = null; + + if (!isset($session)) + $session = &$_SESSION['::lang']; $user = $user ?: $thisstaff ?: $thisclient; if ($user && method_exists($user, 'getLanguage')) return $user->getLanguage(); + // Support the flag buttons for guests - if (isset($_SESSION['client:lang'])) - return $_SESSION['client:lang']; + if ((!$user || $user != $thisstaff) && isset($session)) + return $session; + return self::getDefaultLanguage(); } @@ -386,6 +395,14 @@ class Internationalization { return $rv; } + static function setCurrentLanguage($lang) { + if (!self::isLanguageInstalled($lang)) + return false; + + $_SESSION['::lang'] = $lang ?: null; + return true; + } + static function bootstrap() { require_once INCLUDE_DIR . 'class.translation.php'; diff --git a/include/class.staff.php b/include/class.staff.php index da2473c0e..b9b50ce10 100644 --- a/include/class.staff.php +++ b/include/class.staff.php @@ -279,12 +279,12 @@ class Staff extends AuthenticatedUser { function getLanguage() { static $cached = false; - if (!$cached) $cached = &$_SESSION['staff:lang']; + if (!$cached) + $cached = &$_SESSION['staff:lang']; if (!$cached) { - $cached = $this->config->get('lang'); - if (!$cached) - $cached = Internationalization::getDefaultLanguage(); + $cached = $this->config->get('lang', + Internationalization::getDefaultLanguage()); } return $cached; } diff --git a/include/class.translation.php b/include/class.translation.php index 84570ff53..e6fb8301d 100644 --- a/include/class.translation.php +++ b/include/class.translation.php @@ -760,8 +760,7 @@ class TextDomain { static function configureForUser($user=false) { $lang = Internationalization::getCurrentLanguage($user); - - $info = Internationalization::getLanguageInfo(strtolower($lang)); + $info = Internationalization::getLanguageInfo($lang); if (!$info) // Not a supported language return; diff --git a/setup/setup.inc.php b/setup/setup.inc.php index 035a8333d..68bb94322 100644 --- a/setup/setup.inc.php +++ b/setup/setup.inc.php @@ -68,7 +68,7 @@ require_once INCLUDE_DIR.'class.translation.php'; // Support flags in the setup portal too if (isset($_GET['lang']) && $_GET['lang']) { - $_SESSION['client:lang'] = $_GET['lang']; + Internationalization::setCurrentLanguage($_GET['lang']); } TextDomain::configureForUser(); -- GitLab