-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.js
62 lines (55 loc) · 1.65 KB
/
translate.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
const defaultLanguage = "en";
var englishPath = "";
var frenchPath = "";
window.addEventListener('DOMContentLoaded', (event) => {
console.info('Translate.js loaded successfully')
try {
englishPath = document.head.querySelector("[property~=en-lang][content]").content;
} catch (error) {
console.warn("English language meta is not avaible")
}
try {
frenchPath = document.head.querySelector("[property~=fr-lang][content]").content;
} catch (error) {
console.warn("French language meta is not avaible")
}
const urlParameters = new URLSearchParams(window.location.search);
var choiceLang = urlParameters.get('lang');
if (choiceLang == null) {
choiceLang = defaultLanguage
}
if (choiceLang=="en") {
fetch(englishPath)
.then(res => res.json())
.then(lang => {
var elements = document.querySelectorAll( 'body *' );
for (i=0; i < elements.length; i+=1) {
if (elements[i].innerHTML.includes('translate.')) {
var name = elements[i].innerHTML.slice(10);
if (lang[name] != null) {
elements[i].innerHTML = lang[name]
} else {
console.error('Unable to find a translation for "translation.' + name + '"')
}
}
}
})
}
if (choiceLang=="fr") {
fetch(frenchPath)
.then(res => res.json())
.then(lang => {
var elements = document.querySelectorAll( 'body *' );
for (i=0; i < elements.length; i+=1) {
if (elements[i].innerHTML.includes('translate.')) {
var name = elements[i].innerHTML.slice(10);
if (lang[name] != null) {
elements[i].innerHTML = lang[name]
} else {
console.error('Unable to find a translation for "translation.' + name + '"')
}
}
}
})
}
});