use Accept-Language header before logging into setup / user selected a language

This commit is contained in:
Ralf Becker 2018-01-29 11:05:55 +01:00
parent 37957a49f4
commit 96bcda7433
3 changed files with 19 additions and 5 deletions

View File

@ -195,9 +195,10 @@ class setup
/** /**
* Get configuration language from $_POST or $_SESSION and validate it * Get configuration language from $_POST or $_SESSION and validate it
* *
* @param boolean $use_accept_language =false true: use Accept-Language header as fallback
* @return string * @return string
*/ */
static function get_lang() static function get_lang($use_accept_language=false)
{ {
if (isset($_POST['ConfigLang'])) if (isset($_POST['ConfigLang']))
{ {
@ -208,11 +209,25 @@ class setup
if (!isset($_SESSION)) self::session_start(); if (!isset($_SESSION)) self::session_start();
$ConfigLang = $_SESSION['ConfigLang']; $ConfigLang = $_SESSION['ConfigLang'];
} }
$matches = null;
if ($use_accept_language && empty($ConfigLang) &&
preg_match_all('/(^|,)([a-z-]+)/', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), $matches))
{
$available = Api\Translation::get_available_langs(false);
foreach($matches[2] as $lang)
{
if (isset($available[$lang]))
{
$ConfigLang = $lang;
break;
}
}
}
if (!preg_match('/^[a-z]{2}(-[a-z]{2})?$/',$ConfigLang)) if (!preg_match('/^[a-z]{2}(-[a-z]{2})?$/',$ConfigLang))
{ {
$ConfigLang = null; // not returning 'en', as it suppresses the language selection in check_install and manageheader $ConfigLang = null; // not returning 'en', as it suppresses the language selection in check_install and manageheader
} }
//error_log(__METHOD__."() \$_POST['ConfigLang']=".array2string($_POST['ConfigLang']).", \$_SESSION['ConfigLang']=".array2string($_SESSION['ConfigLang'])." returning ".array2string($ConfigLang)); //error_log(__METHOD__."() \$_POST['ConfigLang']=".array2string($_POST['ConfigLang']).", \$_SESSION['ConfigLang']=".array2string($_SESSION['ConfigLang']).", HTTP_ACCEPT_LANGUAGE=$_SERVER[HTTP_ACCEPT_LANGUAGE] --> returning ".array2string($ConfigLang));
return $ConfigLang; return $ConfigLang;
} }

View File

@ -286,8 +286,7 @@ class setup_html
{ {
if (empty($ConfigLang)) if (empty($ConfigLang))
{ {
$ConfigLang = setup::get_lang(); $ConfigLang = setup::get_lang(true); // true use Accept-Language header
if (empty($ConfigLang)) $ConfigLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
} }
return Api\Html::select('ConfigLang', $ConfigLang, Api\Translation::get_available_langs(false), true, return Api\Html::select('ConfigLang', $ConfigLang, Api\Translation::get_available_langs(false), true,
$onChange ? ' onchange="this.form.submit();"' : ''); $onChange ? ' onchange="this.form.submit();"' : '');

View File

@ -43,7 +43,7 @@ class setup_translation
*/ */
function __construct() function __construct()
{ {
$ConfigLang = setup::get_lang(); $ConfigLang = setup::get_lang(true);
if(!$ConfigLang) if(!$ConfigLang)
{ {