fixes from u1 (default login + i18n)

This commit is contained in:
Serghey Rodin 2016-06-27 01:39:34 +03:00
commit 4b8a2c3f38
2 changed files with 89 additions and 66 deletions

View file

@ -44,69 +44,80 @@ function __() {
$args = func_get_args(); $args = func_get_args();
array_unshift($args,$_SESSION['language']); array_unshift($args,$_SESSION['language']);
return call_user_func_array("_translate",$args); return call_user_func_array("_translate",$args);
} }
/** /**
* Detects user language from Accept-Language HTTP header. * Detects user language from Accept-Language HTTP header.
* @param string Fallback language (default: 'en') * @param string Fallback language (default: 'en')
* @return string Language code (such as 'en' and 'ja') * @return string Language code (such as 'en' and 'ja')
*/ */
function detect_user_language($fallback='en') { function detect_user_language($fallback='en') {
static $user_lang = ''; static $user_lang = '';
// Already detected // Already detected
if (!empty($user_lang)) return $user_lang; if (!empty($user_lang)) return $user_lang;
// Check if Accept-Language header is available // Check if Accept-Language header is available
if (!isset($_SERVER) || if (!isset($_SERVER) ||
!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) || !isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ||
!is_string($_SERVER['HTTP_ACCEPT_LANGUAGE']) !is_string($_SERVER['HTTP_ACCEPT_LANGUAGE'])
) { ) {
// Store result for reusing // Store result for reusing
$user_lang = $fallback; $user_lang = $fallback;
return $user_lang; return $user_lang;
} }
// Sort Accept-Language by `q` value
$accept_langs = explode(',', preg_replace('/\s/', '', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']))); // Sort Accept-Language by `q` value
$accept_langs_sorted = array() ; $accept_langs = explode(',', preg_replace('/\s/', '', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
foreach ($accept_langs as $lang) { $accept_langs_sorted = array() ;
$div = explode(';q=', $lang, 2); foreach ($accept_langs as $lang) {
if (count($div) < 2) { $div = explode(';q=', $lang, 2);
// `q` value was not specfied if (count($div) < 2) {
// -> Set default `q` value (1) // `q` value was not specfied
$div[] = '1'; // -> Set default `q` value (1)
} $div[] = '1';
list($code, $q) = $div; }
if (preg_match('/^[\w\-]+$/', $code)) { list($code, $q) = $div;
// Acceptable language code if (preg_match('/^[\w\-]+$/', $code)) {
$accept_langs_sorted[$code] = (double)$q; // Acceptable language code
} $accept_langs_sorted[$code] = (double)$q;
} }
arsort($accept_langs_sorted); }
arsort($accept_langs_sorted);
// List languages
exec (VESTA_CMD."v-list-sys-languages json", $output, $return_var); // List languages
$languages = json_decode(implode('', $output), true); exec (VESTA_CMD."v-list-sys-languages json", $output, $return_var);
unset($output); $languages = json_decode(implode('', $output), true);
unset($output);
// Find best matching language
foreach ($accept_langs_sorted as $user_lang => $dummy) { // Find best matching language
$decision = ''; foreach ($accept_langs_sorted as $user_lang => $dummy) {
foreach ($languages as $prov_lang) { $decision = '';
if (strlen($decision) > strlen($prov_lang)) continue; foreach ($languages as $prov_lang) {
if (strpos($user_lang, $prov_lang) !== false) { if (strlen($decision) > strlen($prov_lang)) continue;
$decision = $prov_lang; if (strpos($user_lang, $prov_lang) !== false) {
} $decision = $prov_lang;
} }
if (!empty($decision)) { }
// Store result for reusing if (!empty($decision)) {
$user_lang = $decision; // Store result for reusing
return $user_lang; $user_lang = $decision;
} return $user_lang;
} }
}
// Store result for reusing
$user_lang = $fallback; // Store result for reusing
return $user_lang; $user_lang = $fallback;
return $user_lang;
}
/**
* Detects user language .
* @param string Fallback language (default: 'en')
* @return string Language code (such as 'en' and 'ja')
*/
function detect_login_language(){
} }

View file

@ -94,7 +94,19 @@ foreach ($sys_arr as $key => $value) {
} }
// Detect language // Detect language
if (empty($_SESSION['language'])) $_SESSION['language'] = detect_user_language(); if (empty($_SESSION['language'])) {
$output = '';
exec (VESTA_CMD."v-list-sys-config json", $output, $return_var);
$config = json_decode(implode('', $output), true);
$lang = $config['config']['LANGUAGE'];
$output = '';
exec (VESTA_CMD."v-list-sys-languages json", $output, $return_var);
$languages = json_decode(implode('', $output), true);
if(in_array($lang, $languages)){
$_SESSION['language'] = $lang;
}
}
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.$_SESSION['language'].'.php'); require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.$_SESSION['language'].'.php');
require_once('../templates/header.html'); require_once('../templates/header.html');