-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlang.js
45 lines (32 loc) · 1.11 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
// Example of how to implement Lang.translate in javascript ...
// import Cookies from 'https://cdn.jsdelivr.net/npm/js-cookie@3.0.1/dist/js.cookie.min.mjs'
function replaceSubstitutions(str, substitutions) {
for (let substitution in substitutions) {
let search = '{' + substitution + '}';
str = str.replace(search, substitutions[substitution])
}
return str;
}
class Translate {
constructor() {
this.Translation = [];
}
async load(language) {
// Maybe control js language using cookies
// let language = Cookies.get('language');
// if (!language) language = 'en';
const modulePath = '/js/lang/' + language + '/language.js';
let { Translation} = await import(modulePath)
this.Translation = Translation;
}
translate(str, substitutions) {
let toTranslate = this.Translation[str];
if (!toTranslate) toTranslate = str;
if (substitutions) {
toTranslate = replaceSubstitutions(toTranslate, substitutions);
}
return toTranslate;
}
}
let Lang = new Translate();
export {Lang};