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 <?php
function render_footer($admin = false) function render_footer($admin = false)
{ {
global $best_match, $lang_names; global $lang_names;
?> ?>
</div> </div>
<div id="footerwrap"> <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-left">Copyright © <?php echo date("Y");?> Vojtěch Sajdl</div>
<div class="col-md-4 text-center"> <div class="col-md-4 text-center">
<div class="btn-group dropup"> <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"> <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="caret"></span>
<span class="sr-only"><?php echo _("Toggle Dropdown");?></span> <span class="sr-only"><?php echo _("Toggle Dropdown");?></span>

View File

@ -5,8 +5,9 @@ function mb_ucfirst($string)
return mb_strtoupper(mb_substr($string, 0, 1)).mb_strtolower(mb_substr($string, 1)); 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'])) { {
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); 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])) { if (count($lang_parse[1])) {
@ -23,18 +24,20 @@ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
arsort($langs, SORT_NUMERIC); arsort($langs, SORT_NUMERIC);
} }
} }
$langs = array_flip($langs);
$accepted_langs = glob(__DIR__ . '/locale/*' , GLOB_ONLYDIR);
$lang_names = array(); $langs = array_flip($langs);
foreach ($accepted_langs as $key => $value) { $accepted_langs = glob(__DIR__ . '/locale/*' , GLOB_ONLYDIR);
$lang_names = array();
foreach ($accepted_langs as $key => $value) {
$accepted_langs[$key] = basename($value); $accepted_langs[$key] = basename($value);
} }
$best_match = false; $best_match = false;
foreach ($langs as $lang) { foreach ($langs as $lang) {
if (strlen($lang)>2){ if (strlen($lang)>2){
if (in_array($lang, $accepted_langs)){ if (in_array($lang, $accepted_langs)){
$best_match = $lang; $best_match = $lang;
@ -51,18 +54,22 @@ foreach ($langs as $lang) {
break; break;
} }
} }
} }
foreach ($accepted_langs as $lang) { foreach ($accepted_langs as $lang) {
$lang_names[$lang] = mb_ucfirst(locale_get_display_language($lang, $lang)); $lang_names[$lang] = mb_ucfirst(locale_get_display_language($lang, $lang));
} }
if ($best_match === false){ if ($best_match === false){
$best_match = DEFAULT_LANGUAGE; $best_match = DEFAULT_LANGUAGE;
}
$_SESSION['locale'] = $best_match;
} }
setlocale(LC_ALL, $best_match.".UTF-8");
setlocale(LC_ALL, $_SESSION['locale'].".UTF-8");
bindtextdomain("server-status", __DIR__ . "/locale/"); bindtextdomain("server-status", __DIR__ . "/locale/");
bind_textdomain_codeset($best_match, "utf-8"); bind_textdomain_codeset($_SESSION['locale'], "utf-8");
textdomain("server-status"); textdomain("server-status");