-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.js
87 lines (77 loc) · 2.73 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
const EventEmitter = require('events');
class Lang extends EventEmitter {
constructor() {
super();
this.Langs = ['en', 'fr'];
this.CurrentLang = null;
this.Translations = {
'en': {
'loggedas': 'Logged in as [#0]!',
'alreadywarned': 'This user has already been warned. You may consider being rougher ?',
'cantvalid': 'You are not allowed to valid a question ! This will be reported.',
'triedtovalid': 'You are not allowed to valid a question ! This will be reported.',
'alreadyvalid': 'Question [#0] is already valid !'
},
'fr': {
'loggedas': 'Connecté en tant que [#0]!',
'alreadywarned': 'Cet·te utilisateurice a déjà été averti·e. Vous devriez penser à être plus violent·e ?',
'cantvalid': 'Vous ne pouvez pas valider une question ! Ceci sera signalé.',
'triedtovalid': 'Vous ne pouvez pas valider une question ! Ceci sera signalé.',
'alreadyvalid': 'Question [#0] déjà validée !'
}
};
}
get langs() {
return this.Langs;
}
get currentlang() {
return this.CurrentLang;
}
set currentlang(newlang) {
return this.changelang(newlang);
}
get translations() {
return this.Translations;
}
changelang(newlang) {
if (this.CurrentLang === null) {
this.CurrentLang = 'en';
}
if (this.Langs.includes(newlang)) {
this.CurrentLang = newlang;
}
}
getstring(key, bonus = null) {
let text;
if (!this.Translations[this.CurrentLang].hasOwnProperty(key)) {
if (!this.Translations.en.hasOwnProperty(key)) {
return '[[' + key + ']]';
} else {
text = this.Translations.en[key];
}
} else {
text = this.Translations[this.CurrentLang][key];
}
if (bonus !== null) {
if (Array.isArray(bonus)) {
for (let i = 0; i < bonus.length; i++) {
text = text.replace('[#' + i + ']', bonus[i]);
}
} else if (typeof bonus === 'object') {
for (let index in bonus) {
if (bonus.hasOwnProperty(index)) {
text = text.replace('[#' + index + ']', bonus[index]);
}
}
} else if (typeof bonus === "string") {
text = text.replace('[#0]', bonus);
}
}
return text;
}
init(lang = 'en') {
this.changelang(lang);
this.emit('ready');
}
}
module.exports = Lang;