Skip to content

Commit 4b8a2c3

Browse files
committed
fixes from u1 (default login + i18n)
1 parent 5edfe8d commit 4b8a2c3

File tree

2 files changed

+89
-66
lines changed

2 files changed

+89
-66
lines changed

web/inc/i18n.php

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -44,69 +44,80 @@ function __() {
4444
$args = func_get_args();
4545
array_unshift($args,$_SESSION['language']);
4646
return call_user_func_array("_translate",$args);
47-
}
48-
49-
/**
50-
* Detects user language from Accept-Language HTTP header.
51-
* @param string Fallback language (default: 'en')
52-
* @return string Language code (such as 'en' and 'ja')
53-
*/
54-
function detect_user_language($fallback='en') {
55-
static $user_lang = '';
56-
57-
// Already detected
58-
if (!empty($user_lang)) return $user_lang;
59-
60-
// Check if Accept-Language header is available
61-
if (!isset($_SERVER) ||
62-
!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ||
63-
!is_string($_SERVER['HTTP_ACCEPT_LANGUAGE'])
64-
) {
65-
// Store result for reusing
66-
$user_lang = $fallback;
67-
return $user_lang;
68-
}
69-
70-
// Sort Accept-Language by `q` value
71-
$accept_langs = explode(',', preg_replace('/\s/', '', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
72-
$accept_langs_sorted = array() ;
73-
foreach ($accept_langs as $lang) {
74-
$div = explode(';q=', $lang, 2);
75-
if (count($div) < 2) {
76-
// `q` value was not specfied
77-
// -> Set default `q` value (1)
78-
$div[] = '1';
79-
}
80-
list($code, $q) = $div;
81-
if (preg_match('/^[\w\-]+$/', $code)) {
82-
// Acceptable language code
83-
$accept_langs_sorted[$code] = (double)$q;
84-
}
85-
}
86-
arsort($accept_langs_sorted);
87-
88-
// List languages
89-
exec (VESTA_CMD."v-list-sys-languages json", $output, $return_var);
90-
$languages = json_decode(implode('', $output), true);
91-
unset($output);
92-
93-
// Find best matching language
94-
foreach ($accept_langs_sorted as $user_lang => $dummy) {
95-
$decision = '';
96-
foreach ($languages as $prov_lang) {
97-
if (strlen($decision) > strlen($prov_lang)) continue;
98-
if (strpos($user_lang, $prov_lang) !== false) {
99-
$decision = $prov_lang;
100-
}
101-
}
102-
if (!empty($decision)) {
103-
// Store result for reusing
104-
$user_lang = $decision;
105-
return $user_lang;
106-
}
107-
}
108-
109-
// Store result for reusing
110-
$user_lang = $fallback;
111-
return $user_lang;
47+
}
48+
49+
/**
50+
* Detects user language from Accept-Language HTTP header.
51+
* @param string Fallback language (default: 'en')
52+
* @return string Language code (such as 'en' and 'ja')
53+
*/
54+
function detect_user_language($fallback='en') {
55+
static $user_lang = '';
56+
57+
// Already detected
58+
if (!empty($user_lang)) return $user_lang;
59+
60+
// Check if Accept-Language header is available
61+
if (!isset($_SERVER) ||
62+
!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ||
63+
!is_string($_SERVER['HTTP_ACCEPT_LANGUAGE'])
64+
) {
65+
// Store result for reusing
66+
$user_lang = $fallback;
67+
return $user_lang;
68+
}
69+
70+
71+
// Sort Accept-Language by `q` value
72+
$accept_langs = explode(',', preg_replace('/\s/', '', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
73+
$accept_langs_sorted = array() ;
74+
foreach ($accept_langs as $lang) {
75+
$div = explode(';q=', $lang, 2);
76+
if (count($div) < 2) {
77+
// `q` value was not specfied
78+
// -> Set default `q` value (1)
79+
$div[] = '1';
80+
}
81+
list($code, $q) = $div;
82+
if (preg_match('/^[\w\-]+$/', $code)) {
83+
// Acceptable language code
84+
$accept_langs_sorted[$code] = (double)$q;
85+
}
86+
}
87+
arsort($accept_langs_sorted);
88+
89+
// List languages
90+
exec (VESTA_CMD."v-list-sys-languages json", $output, $return_var);
91+
$languages = json_decode(implode('', $output), true);
92+
unset($output);
93+
94+
// Find best matching language
95+
foreach ($accept_langs_sorted as $user_lang => $dummy) {
96+
$decision = '';
97+
foreach ($languages as $prov_lang) {
98+
if (strlen($decision) > strlen($prov_lang)) continue;
99+
if (strpos($user_lang, $prov_lang) !== false) {
100+
$decision = $prov_lang;
101+
}
102+
}
103+
if (!empty($decision)) {
104+
// Store result for reusing
105+
$user_lang = $decision;
106+
return $user_lang;
107+
}
108+
}
109+
110+
// Store result for reusing
111+
$user_lang = $fallback;
112+
return $user_lang;
113+
}
114+
115+
/**
116+
* Detects user language .
117+
* @param string Fallback language (default: 'en')
118+
* @return string Language code (such as 'en' and 'ja')
119+
*/
120+
121+
function detect_login_language(){
122+
112123
}

web/login/index.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,19 @@
9494
}
9595

9696
// Detect language
97-
if (empty($_SESSION['language'])) $_SESSION['language'] = detect_user_language();
97+
if (empty($_SESSION['language'])) {
98+
$output = '';
99+
exec (VESTA_CMD."v-list-sys-config json", $output, $return_var);
100+
$config = json_decode(implode('', $output), true);
101+
$lang = $config['config']['LANGUAGE'];
102+
103+
$output = '';
104+
exec (VESTA_CMD."v-list-sys-languages json", $output, $return_var);
105+
$languages = json_decode(implode('', $output), true);
106+
if(in_array($lang, $languages)){
107+
$_SESSION['language'] = $lang;
108+
}
109+
}
98110

99111
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.$_SESSION['language'].'.php');
100112
require_once('../templates/header.html');

0 commit comments

Comments
 (0)