-
Notifications
You must be signed in to change notification settings - Fork 36
/
Lang.js
94 lines (79 loc) · 1.97 KB
/
Lang.js
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
const LIB_PREFIX = "lang_lib_"
function setUserLanguage(curLangName){
User.setProperty(LIB_PREFIX + "curLangName", curLangName, "string");
}
function getUserLanguage(){
if(user){
let lng = User.getProperty(LIB_PREFIX + "curLangName");
if(lng){ return lng }
}
return getDefaultLanguage();
}
function setDefaultLanguage(langName){
Bot.setProperty(LIB_PREFIX + "default", langName, "string");
}
function getDefaultLanguage(){
return Bot.getProperty(LIB_PREFIX + "default");
}
function setupLanguage(langName, keys){
Bot.setProperty(LIB_PREFIX + langName, keys, "json");
let def = getDefaultLanguage();
if(!def){ setDefaultLanguage(langName) }
}
function get(lang){
let curLng;
if(lang){ curLng = lang }
else{
curLng = getUserLanguage();
}
let json = Bot.getProperty(LIB_PREFIX + curLng);
if(!json){
throw new Error("Language is not setup: " + curLng);
}
return json;
}
function get_trans_item(item, lang){
var result;
var json = get(lang);
try{ result = eval("json." + item) }
catch(err){}
return result
}
function t(item, lang){
// for lang
var result = get_trans_item(item, lang);
if(result){ return result }
// for default language
return get_trans_item(item, getDefaultLanguage());
}
function getCommandByAlias(alias, lang){
if(!alias){ return }
var json = get(lang)
if(!json){ return }
if(!json.aliases){ return }
var aliases;
for(var key in json.aliases){
// aliases separated with ",". Can have spaces - so remove spaces:
aliases = key.split(" ,").join(",").split(", ").join(",");
aliases = aliases.split(",");
for(var ind in aliases){
if(aliases[ind].toLowerCase()==alias.toLowerCase()){
return json.aliases[key]
}
}
}
}
publish({
user:{
setLang: setUserLanguage,
getCurLang: getUserLanguage
},
default:{
setLang: setDefaultLanguage,
getCurLang: getDefaultLanguage
},
setup: setupLanguage,
get: get,
t: t,
getCommandByAlias: getCommandByAlias
})