Skip to content

Commit c8e0ca4

Browse files
committed
Automatic language detection
Added automatic language detection based on HTTP_ACCEPT_LANGUAGE header. New function _translate, that translates string to required language. New file: setlang.php?l=language_code - sets session language.
1 parent def0f35 commit c8e0ca4

File tree

7 files changed

+78
-27
lines changed

7 files changed

+78
-27
lines changed

web/add/user/index.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@
6464
exec (VESTA_CMD."v-change-user-language ".$v_username." ".$v_language, $output, $return_var);
6565
if (!empty($v_notify)) {
6666
$to = $_POST['v_notify'];
67-
$subject = _("Welcome to Vesta Control Panel");
67+
$subject = _translate($v_language,"Welcome to Vesta Control Panel");
6868
$hostname = exec('hostname');
69-
$from = _('MAIL_FROM',$hostname);
69+
$from = _translate($v_language,'MAIL_FROM',$hostname);
7070
if (!empty($_POST['v_fname'])) {
71-
$mailtext = _('GREETINGS_GORDON_FREEMAN',$_POST['v_fname'],$_POST['v_lname']);
71+
$mailtext = _translate($v_language,'GREETINGS_GORDON_FREEMAN',$_POST['v_fname'],$_POST['v_lname']);
7272
} else {
73-
$mailtext = _('GREETINGS');
73+
$mailtext = _translate($v_language,'GREETINGS');
7474
}
75-
$mailtext .= _('ACCOUNT_READY',$_SERVER['HTTP_HOST'],$_POST['v_username'],$_POST['v_password']);
75+
$mailtext .= _translate($v_language,'ACCOUNT_READY',$_SERVER['HTTP_HOST'],$_POST['v_username'],$_POST['v_password']);
7676
send_email($to, $subject, $mailtext, $from);
7777
}
7878

web/inc/i18n/en.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
if (!defined('LANGUAGE')) die('Direct access not allowed');
99

10-
$LANG = array(
10+
$LANG['en'] = array(
1111
'yes' => 'yes',
1212
'no' => 'no',
1313
'none' => 'нет',

web/inc/i18n/ru.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
if (!defined('LANGUAGE')) die('Direct access not allowed');
99

10-
$LANG = array(
10+
$LANG['ru'] = array(
1111
'yes' => 'да',
1212
'no' => 'нет',
1313
'none' => 'нет',

web/inc/main.php

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,71 @@
11
<?php
22

3-
// need to be moved to user settings
4-
define('LANGUAGE','ru');
5-
6-
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.LANGUAGE.'.php');
7-
8-
// works like sprintf if more than one arguments called
3+
/**
4+
* Translates string by a given key in first parameter to current session language. Works like sprintf
5+
* @global array $LANG Associative array of language pharses
6+
* @return string Translated string
7+
* @see _translate()
8+
*/
99
function _() {
10+
$args = func_get_args();
11+
array_unshift($args,$_SESSION['language']);
12+
return call_user_func_array("_translate",$args);
13+
}
14+
15+
/**
16+
* Translates string to given language in first parameter, key given in second parameter (dynamically loads required language). Works like spritf from second parameter
17+
* @global array $LANG Associative array of language pharses
18+
* @return string Translated string
19+
*/
20+
function _translate() {
1021
global $LANG;
22+
1123
$args = func_get_args();
12-
$key = $args[0];
13-
if (!isset($LANG[$key])) $text=$key; else
14-
$text=$LANG[$key];
15-
24+
$l = $args[0];
25+
26+
if (!$l) return 'NO LANGUAGE DEFINED';
27+
$key = $args[1];
28+
29+
if (!isset($LANG[$l])) {
30+
define('LANGUAGE',true);
31+
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.$l.'.php');
32+
}
33+
if (!isset($LANG[$l][$key])) $text=$key; else
34+
$text=$LANG[$l][$key];
35+
36+
array_shift($args);
1637
if (count($args)>1) { $args[0] = $text;
1738
return call_user_func_array("sprintf",$args);
1839
}
1940
else return $text;
2041
}
2142

22-
// Check user session
43+
44+
define('VESTA_CMD', '/usr/bin/sudo /usr/local/vesta/bin/');
45+
46+
$i = 0;
47+
48+
// setting language here
49+
50+
51+
$ls['command'] = VESTA_CMD."v-list-sys-languages json";
52+
exec ($ls['command'], $ls['output'], $ls['return_var']);
53+
$ls['langs'] = json_decode(implode('', $ls['output']), true);
54+
55+
if (isset($_SESSION['language'])&&!in_array($_SESSION['language'],$ls['langs'])) {
56+
$ls['browserlang'] = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));
57+
if (!in_array($ls['browserlang'],$ls['langs'])) {
58+
unset($ls);
59+
$ls['command'] = VESTA_CMD."v-list-sys-config json";
60+
exec ($ls['command'], $ls['output'], $ls['return_var']);
61+
$ls['langs'] = json_decode(implode('',$ls['output']),true);
62+
$_SESSION['language'] = $ls['langs']['config']['LANGUAGE'];
63+
} else {
64+
$_SESSION['language'] = $ls['browserlang'];
65+
}
66+
}
67+
unset($ls);
68+
2369
if ((!isset($_SESSION['user'])) && (!isset($api_mode))&&!defined('NO_AUTH_REQUIRED')) {
2470
$_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
2571
header("Location: /login/");
@@ -32,10 +78,6 @@ function _() {
3278
$user = $_SESSION['user'];
3379
}
3480

35-
define('VESTA_CMD', '/usr/bin/sudo /usr/local/vesta/bin/');
36-
37-
$i = 0;
38-
3981
// Define functions
4082
function check_error($return_var){
4183
if ( $return_var > 0 ) {

web/index.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22
session_start();
3+
4+
define('NO_AUTH_REQUIRED',true);
5+
6+
include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
7+
8+
39
if (isset($_SESSION['user'])) {
410
header("Location: /list/user");
511
} else {

web/login/index.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
session_start();
33

44
define('NO_AUTH_REQUIRED',true);
5-
define('NO_LANG', true);
65

76
include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
87

@@ -12,14 +11,10 @@
1211

1312
$TAB = 'LOGIN';
1413

15-
exec (VESTA_CMD."v-list-sys-config json", $output, $return_var);
1614
if ($return_var != 0) {
1715
header("Location: /error/");
1816
exit;
1917
}
20-
$data = json_decode(implode('', $output), true);
21-
unset($output);
22-
//$LANGUAGE = $data['config']['LANGUAGE'];
2318

2419
if (isset($_SESSION['user'])) {
2520
if ($_SESSION['user'] == 'admin' && !empty($_GET['loginas'])) {

web/setlang.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/*
3+
* Just sets language
4+
*/
5+
session_start();
6+
$_SESSION['language'] = strtolower(substr((string)$_GET['l'],0,2));
7+
header("Location: /");
8+
?>

0 commit comments

Comments
 (0)