#1620 Improve setting language by GET parameter

This commit is contained in:
Alexandre Marcoux 2024-11-08 15:52:09 -05:00
parent 1153d7698b
commit 2105e72fa8
2 changed files with 42 additions and 19 deletions

View file

@ -128,16 +128,6 @@ $languages = [
$config['language_codes'] = $languages;
$language_code = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : 'en';
$config['language'] =
$_GET['language'] ??
(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'], $languages[$language_code])
? $languages[$language_code]
: Config::LANGUAGE);
$config['language_code'] = array_search($config['language'], $languages) ?: 'en';
/*
|--------------------------------------------------------------------------
| Available Languages

View file

@ -112,17 +112,50 @@ class EA_Controller extends CI_Controller
*/
private function configure_language()
{
$session_language = session('language');
if ($session_language) {
$language_codes = config('language_codes');
config([
'language' => $session_language,
'language_code' => array_search($session_language, $language_codes) ?: 'en',
]);
$language_codes = config('language_codes');
// set the language code from the language GET parameter if it's a supported language
if (!empty($_GET['language'])) {
$queryLanguage = strtolower($_GET['language']);
$language_code = array_search($queryLanguage, $language_codes);
if (!$language_code) {
// check if the ISO 639-1 language code was set as the get parameter
$queryLangCode = substr($queryLanguage, 0, 2);
if (isset($language_codes[$queryLangCode])) {
$language_code = $queryLangCode;
}
}
}
if (empty($language_code)) {
// set the language code from the language SESSION parameter if it's a supported language
if (session('language')) {
$language_code = array_search(session('language'), $language_codes);
}
// set the language code from the language header if it's a supported language
elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$headerLangCode = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if (isset($language_codes[$headerLangCode])) {
$language_code = $headerLangCode;
}
}
}
// no language set. set it to the default language defined in the config
if (!$language_code) {
$language = strtolower(Config::LANGUAGE);
$language_code = array_search($language, $language_codes);
}
else {
$language = $language_codes[$language_code];
}
session(['language' => $language]);
config([
'language' => $language,
'language_code' => $language_code,
]);
$this->lang->load('translations');
}