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,7 +5,8 @@ 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);
@ -25,6 +26,8 @@ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
}
}
$langs = array_flip($langs);
$accepted_langs = glob(__DIR__ . '/locale/*' , GLOB_ONLYDIR);
$lang_names = array();
@ -61,8 +64,12 @@ if ($best_match === false){
$best_match = DEFAULT_LANGUAGE;
}
setlocale(LC_ALL, $best_match.".UTF-8");
$_SESSION['locale'] = $best_match;
}
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");