1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
<?php
class L10N {
private $translations = [];
const default_language = "en_US";
private $language;
public function __construct($desired_languages, $l10n_dir, $modules_path=null)
{
if (!isset($desired_languages)) {
$desired_languages = "";
}
$l10n_dir = rtrim($l10n_dir, '/');
$available_languages = array_filter(scandir($l10n_dir),
function ($s) {
return pathinfo($s, PATHINFO_EXTENSION) == "json";
});
$available_languages = array_map(
function ($s) {
return basename($s, ".json");
},
$available_languages);
$desired_lang = $this->load_language_setting($modules_path);
if($desired_lang != "auto")
{
$desired_languages = $desired_lang;
}
$lang = $this->find_language($available_languages, $desired_languages);
$this->language = $lang;
if ($lang === L10N::default_language || !file_exists(join('/', [$l10n_dir, $lang . ".json"])) || !$this->load($lang, $l10n_dir, $modules_path)) {
$this->language = L10N::default_language;
}
}
function load($lang, $l10n_dir, $modules_path)
{
$modules_path = join('/', [rtrim($modules_path, '/'), $l10n_dir]);
$files = [join('/', [$l10n_dir, $lang . ".json"])];
if (is_dir($modules_path)) {
foreach (scandir($modules_path) as $dir) {
$file_path = join('/', [$modules_path, trim($dir, '/'), $lang . ".json"]);
if (is_file($file_path)) {
array_push($files, $file_path);
}
}
}
$jsonError = null;
foreach ($files as $file) {
$module_name = pathinfo(pathinfo($file, PATHINFO_DIRNAME), PATHINFO_BASENAME);
if( $module_name == pathinfo($l10n_dir, PATHINFO_BASENAME)) {
$module_name = "__core__";
}
$json = json_decode(file_get_contents($file), true);
if (!is_array($json)) {
$jsonError = json_last_error();
continue;
}
$this->translations[$module_name] = $json['translations'];
}
return ($jsonError == null);
}
public function __($text, $module="__core__") {
if( isset($this->translations[$module]) && isset($this->translations[$module][$text])) {
return $this->translations[$module][$text];
}
return $text;
}
function find_language(array $available_languages, $desired_language) {
$available_languages = array_flip($available_languages);
$langs = [];
preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($desired_language), $matches, PREG_SET_ORDER);
foreach($matches as $match) {
list($a, $b) = explode('-', $match[1]) + array('', '');
$value = isset($match[2]) ? (float) $match[2] : 1.0;
if(isset($available_languages[$match[1]])) {
$langs[$match[1]] = $value;
continue;
}
if(isset($available_languages[$a])) {
$langs[$a] = $value - 0.1;
}
}
arsort($langs);
if(count($langs) == 0) {
return L10N::default_language;
}
return array_keys($langs)[0];
}
function load_language_setting($modules_path) {
$webui_config_file = join('/', [rtrim($modules_path, '/'), 'nc-webui.sh']);
$fh = fopen( $webui_config_file ,'r')
or exit( '{ "output": "' . $webui_config_file . ' read error" }' );
$lang = "auto";
while ( $line = fgets($fh) )
{
// drop down menu
if(preg_match('/^LANGUAGE_=\[(([_\w]+,)*[_\w]+)\]$/', $line, $matches))
{
$options = explode(',', $matches[1]);
foreach($options as $option)
{
if($option[0] == "_" && $option[count($option) - 1] == "_")
{
fclose($fh);
$lang = trim($option,'_');
}
}
fclose($fh);
return $lang;
}
}
}
}
|