Remember locale in session

This commit is contained in:
Vojtěch Sajdl 2018-01-02 19:43:00 +01:00
parent 333d6b40b8
commit 57fdd4c442
No known key found for this signature in database
GPG Key ID: 5D4EB1361A272390
2 changed files with 54 additions and 47 deletions

View File

@ -1,7 +1,7 @@
<?php
function render_footer($admin = false)
{
global $best_match, $lang_names;
global $lang_names;
?>
</div>
<div id="footerwrap">
@ -10,7 +10,7 @@ function render_footer($admin = false)
<div class="col-md-4 text-left">Copyright © <?php echo date("Y");?> Vojtěch Sajdl</div>
<div class="col-md-4 text-center">
<div class="btn-group dropup">
<button type="button" class="btn btn-primary"><?php echo '<img src="'.WEB_URL.'/locale/'.$best_match.'/flag.png" alt="'.$lang_names[$best_match].'">'.$lang_names[$best_match];?></button>
<button type="button" class="btn btn-primary"><?php echo '<img src="'.WEB_URL.'/locale/'.$_SESSION['locale'].'/flag.png" alt="'.$lang_names[$_SESSION['locale']].'">'.$lang_names[$_SESSION['locale']];?></button>
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only"><?php echo _("Toggle Dropdown");?></span>

View File

@ -5,64 +5,71 @@ function mb_ucfirst($string)
return mb_strtoupper(mb_substr($string, 0, 1)).mb_strtolower(mb_substr($string, 1));
}
if (!isset($_SESSION['locale'])||isset($_GET['lang']))
{
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1])) {
$langs = array_combine($lang_parse[1], $lang_parse[4]);
foreach ($langs as $lang => $val) {
if ($val === '') $langs[$lang] = 1;
}
if (count($lang_parse[1])) {
$langs = array_combine($lang_parse[1], $lang_parse[4]);
foreach ($langs as $lang => $val) {
if ($val === '') $langs[$lang] = 1;
}
if (isset($_GET['lang']))
{
$langs[$_GET['lang']] = 2;
}
if (isset($_GET['lang']))
{
$langs[$_GET['lang']] = 2;
}
arsort($langs, SORT_NUMERIC);
}
}
arsort($langs, SORT_NUMERIC);
}
}
$langs = array_flip($langs);
$accepted_langs = glob(__DIR__ . '/locale/*' , GLOB_ONLYDIR);
$lang_names = array();
foreach ($accepted_langs as $key => $value) {
$accepted_langs[$key] = basename($value);
}
$best_match = false;
$langs = array_flip($langs);
$accepted_langs = glob(__DIR__ . '/locale/*' , GLOB_ONLYDIR);
$lang_names = array();
foreach ($accepted_langs as $key => $value) {
$accepted_langs[$key] = basename($value);
}
foreach ($langs as $lang) {
if (strlen($lang)>2){
if (in_array($lang, $accepted_langs)){
$best_match = $lang;
break;
}
}else{
$possible = array_filter($accepted_langs, function($key) {
global $lang;
return strpos($key, $lang) === 0;
});
$best_match = false;
if (count($possible)){
$best_match = $possible[0];
break;
foreach ($langs as $lang) {
if (strlen($lang)>2){
if (in_array($lang, $accepted_langs)){
$best_match = $lang;
break;
}
}else{
$possible = array_filter($accepted_langs, function($key) {
global $lang;
return strpos($key, $lang) === 0;
});
if (count($possible)){
$best_match = $possible[0];
break;
}
}
}
foreach ($accepted_langs as $lang) {
$lang_names[$lang] = mb_ucfirst(locale_get_display_language($lang, $lang));
}
if ($best_match === false){
$best_match = DEFAULT_LANGUAGE;
}
$_SESSION['locale'] = $best_match;
}
foreach ($accepted_langs as $lang) {
$lang_names[$lang] = mb_ucfirst(locale_get_display_language($lang, $lang));
}
if ($best_match === false){
$best_match = DEFAULT_LANGUAGE;
}
setlocale(LC_ALL, $best_match.".UTF-8");
setlocale(LC_ALL, $_SESSION['locale'].".UTF-8");
bindtextdomain("server-status", __DIR__ . "/locale/");
bind_textdomain_codeset($best_match, "utf-8");
bind_textdomain_codeset($_SESSION['locale'], "utf-8");
textdomain("server-status");