Skip to content

Commit cd0dbb4

Browse files
committed
Merge branch 'master' of github.com:serghey-rodin/vesta
2 parents 0b48a04 + 8503efa commit cd0dbb4

File tree

10 files changed

+300
-271
lines changed

10 files changed

+300
-271
lines changed

bin/v-change-user-language

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ source $VESTA/conf/vesta.conf
1919

2020

2121
is_language_valid() {
22-
if [ ! -e "$VESTA/web/inc/i18n/$language.php" ]; then
23-
echo "Error: language $language not exist"
24-
log_event "$E_NOTEXIST $EVENT"
22+
if ! [[ "$1" =~ ^[[:alnum:]_-]+$ ]]; then
23+
echo "Error: language $1 is not valid"
24+
log_event "$E_INVALID" "$EVENT"
25+
exit $E_INVALID
26+
fi
27+
if [ ! -e "$VESTA/web/inc/i18n/$1.php" ]; then
28+
echo "Error: language $1 doesn't exist"
29+
log_event "$E_NOTEXIST" "$EVENT"
2530
exit $E_NOTEXIST
2631
fi
2732
}

web/edit/user/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
$packages = json_decode(implode('', $output), true);
6363
unset($output);
6464

65-
// List lanugages
65+
// List languages
6666
exec (VESTA_CMD."v-list-sys-languages json", $output, $return_var);
6767
$languages = json_decode(implode('', $output), true);
6868
unset($output);

web/inc/i18n.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
// Functions for internationalization
3+
4+
/**
5+
* Translates string to given language in first parameter, key given in second parameter (dynamically loads required language). Works like spritf from second parameter
6+
* @global array $LANG Associative array of language pharses
7+
* @return string Translated string
8+
*/
9+
function _translate() {
10+
global $LANG;
11+
12+
$args = func_get_args();
13+
$l = $args[0];
14+
15+
if (!$l) return 'NO LANGUAGE DEFINED';
16+
$key = $args[1];
17+
18+
if (!isset($LANG[$l])) {
19+
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.$l.'.php');
20+
}
21+
22+
if (!isset($LANG[$l][$key])) {
23+
$text=$key;
24+
} else {
25+
$text=$LANG[$l][$key];
26+
}
27+
28+
array_shift($args);
29+
if (count($args)>1) {
30+
$args[0] = $text;
31+
return call_user_func_array("sprintf",$args);
32+
} else {
33+
return $text;
34+
}
35+
}
36+
37+
/**
38+
* Translates string by a given key in first parameter to current session language. Works like sprintf
39+
* @global array $LANG Associative array of language pharses
40+
* @return string Translated string
41+
* @see _translate()
42+
*/
43+
function __() {
44+
$args = func_get_args();
45+
array_unshift($args,$_SESSION['language']);
46+
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 = [];
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;
112+
}

0 commit comments

Comments
 (0)