-
Notifications
You must be signed in to change notification settings - Fork 1
/
LanguageSelector.js
90 lines (88 loc) · 2.01 KB
/
LanguageSelector.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
let rtl = [
"ar",
"ur",
"fa",
"he",
"arc",
"az",
"dv",
"arabic",
"aramaic",
"azeri",
"maldivian",
"dhivehi",
"hebrew",
"kurdish",
"persian",
"urdu"
];
class LanguageSelector {
setLang(lang) {
l = lang.toLowerCase();
localStorage.setItem("lang", l);
return true;
}
getLang() {
var lang = localStorage.getItem("lang");
if (lang != null) {
return lang;
} else {
return "en";
}
}
removeLang() {
localStorage.removeItem("lang");
return true;
}
loadLang() {
var l = this.getLang();
var strs = lang[l];
return strs;
}
parse(node) {
var lang = this.loadLang();
var len = lang.length - 1;
var keys = Object.keys(lang);
var values = Object.values(lang);
for (var i = 0; i <= keys.length - 1; i++) {
node.innerText = node.innerText.replaceAll(keys[i], values[i]);
}
return true;
}
dfsTraversal(node) {
if (node.childNodes.length == 1) {
this.parse(node);
return true;
} else {
node = node.firstChild;
while (node) {
this.dfsTraversal(node);
node = node.nextSibling;
}
}
}
//if your website uses animations based on language directions , this function sets it.
changeSideFade() {
var elementsToFade = document.querySelectorAll(
".fadeInRight , .fadeInLeft "
);
for (var i = 0; i < elementsToFade.length; i++) {
if (elementsToFade[i].classList.contains("fadeInLeft")) {
elementsToFade[i].classList.remove("fadeInLeft");
elementsToFade[i].classList.add("fadeInRight");
} else {
elementsToFade[i].classList.remove("fadeInRight");
elementsToFade[i].classList.add("fadeInLeft");
}
}
return true;
}
changeLanguage(node) {
if (rtl.includes(this.getLang())) {
this.changeSideFade();
document.body.style.direction = "rtl";
}
this.dfsTraversal(node);
return true;
}
}