Commit 85e3b0ec authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Merge "Bug 914019: Default to the first language listed in Accept-Language for lang dropdown"

parents ed50ec8c b53830ef
......@@ -13,7 +13,7 @@ define('INTERNAL', 1);
define('PUBLIC', 1);
require('init.php');
$lang = param_alphanumext('lang');
$lang = param_alphanumext('lang') === 'default' ? get_accept_lang() : param_alphanumext('lang');
$languages = get_languages();
if (!isset($languages[$lang])) {
throw new ParameterException('Unknown language');
......
......@@ -1444,7 +1444,7 @@ function current_language() {
// If there's no language from the user pref or the logged-out lang menu...
if (empty($lang)) {
$lang = !empty($CFG->lang) ? $CFG->lang : 'en.utf8';
$lang = !empty($CFG->lang) ? $CFG->lang : get_accept_lang();
}
if ($lang == $lastlang) {
......@@ -1452,10 +1452,55 @@ function current_language() {
}
set_locale_for_language($lang);
return $lastlang = $lang;
}
/**
* Try to match the Accept-Language request header with installed lang packs
*
* @return string of matching lang pack key (ending with .utf8) on success
* @return en.utf8 on failing to match an installed language that is not English
*/
function get_accept_lang() {
$acceptlangs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$acceptlangs = preg_replace('/;q=[0-9\.]+/', '', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$acceptlangs = explode(',', $acceptlangs);
}
else if (isset($_SERVER['LANGUAGE'])) {
// If installing via CLI
$acceptlangs = explode(':', $_SERVER['LANGUAGE']);
}
$langpacks = get_languages(); // installed lang packs
// keep list of first 2 or 5 letters for the lang packs for matching the accept-language values
// Mahara language packs don't matchup with Accept-Language languages so will need to change any _ to -
$langpacksshort = array_map(function($v) {
$v = preg_replace('/_/', '-', $v);
if (preg_match('/-/', $v)) {
return substr($v, 0, 5);
}
return substr($v, 0, 2);
}, array_keys($langpacks));
// match the langpack short names to their full lang pack name
$matchedkeys = array_combine($langpacksshort, array_keys($langpacks));
foreach ($acceptlangs as $lang) {
// match the closest possible to an acceptlang option
if (isset($matchedkeys[$lang]) && language_installed($matchedkeys[$lang])) {
return $matchedkeys[$lang];
}
$langshort = substr($lang, 0, 2);
// check for next best similar lang option
if (isset($matchedkeys[$langshort]) && language_installed($matchedkeys[$langshort])) {
return $matchedkeys[$langshort];
}
}
return 'en.utf8';
}
/**
* Find out a user's institution language. If they belong to one institution that has specified
......
......@@ -633,7 +633,7 @@ function core_postinst() {
// Set default search plugin
set_config('searchplugin', 'internal');
set_config('lang', 'en.utf8');
set_config('lang', get_accept_lang());
set_config('installation_key', get_random_key());
set_config('installation_time', $now);
set_config('stats_installation_time', $now);
......
......@@ -802,9 +802,17 @@ EOF;
else {
$smarty->assign('SELECTEDSUBNAV', $SELECTEDSUBNAV);
}
$displaylangs = get_languages();
if (count($displaylangs) > 1) {
$smarty->assign('LANGCHOICES', get_languages());
$installedlangs = get_languages();
if (count($installedlangs) > 1) {
$acceptedlang = get_accept_lang();
$honouracceptlangs = array();
foreach (array_reverse($installedlangs) as $key => $value) {
if ($key !== $acceptedlang) {
$honouracceptlangs[$key] = $value;
}
}
$honouracceptlangs[$acceptedlang] = $installedlangs[$acceptedlang];
$smarty->assign('LANGCHOICES', array_reverse($honouracceptlangs));
}
$smarty->assign('LANGCURRENT', current_language());
}
......@@ -4708,7 +4716,7 @@ function language_select_form() {
'submitonchange' => true,
'class' => 'submitonchange',
'options' => $languages,
'defaultvalue' => $SESSION->get('lang') ? $SESSION->get('lang') : 'default',
'defaultvalue' => $SESSION->get('lang') ? $SESSION->get('lang') : get_accept_lang(),
'rules' => array('required' => true),
),
'changelang' => array(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment