-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
106 lines (86 loc) · 2.78 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
import './styles/resume.scss'
import { extractLocale, getLangFromLocale } from './javascript/i18n.js'
import { langs } from './javascript/langs'
import { getYears } from './javascript/dates.js'
const locale = extractLocale(document.location);
async function i18Loader(lang) {
const jsons = await Promise.all(
langs.map((l) => fetch("/translations/" + l + ".json").then((r) => r.json()))
);
const res = langs.reduce((acc, l, idx) => {
acc[l] = { translation: jsons[idx] };
return acc;
}, {});
await i18next.init({
lng: lang,
debug: false,
resources: res
});
updateContent();
updateUrl(lang);
i18next.on("languageChanged", () => {
updateContent();
});
function updateElements() {
const elements = document.getElementsByClassName("i18nelement");
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const k = element.getAttribute("data-i18n");
const options = element.getAttribute("data-i18n-options");
const keys = JSON.parse(options);
let value = i18next.t(k);
for (var key in keys) {
value = value.replace (`__${key}__`, keys[key]);
}
element.innerHTML = value;
}
}
function updateAttributes() {
const elements = document.getElementsByClassName("i18nattribute");
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const data = element.getAttribute("data-i18n");
const options = element.getAttribute("data-i18n-options");
const optKeys = JSON.parse(options);
const [attrName, k] = data.split(':');
let attrValue = i18next.t(k);
for (var optKey in optKeys) {
value = value.replace (`__${optKey}__`, optKeys[optKey]);
}
element.setAttribute(attrName, attrValue);
}
}
function updateContent() {
updateElements();
updateAttributes();
}
function updateUrl(lang) {
window.history.pushState({}, "", `/?lang=${lang}`);
}
const langSelectors = document.querySelectorAll('.langSelector');
langSelectors.forEach(langSelector => {
langSelector.addEventListener('click', (e) => {
let lang = e.target.name;
i18next.changeLanguage(lang);
updateUrl(lang);
});
});
}
function updateAge(birthdate) {
document.getElementById("ageNum").innerHTML = getYears(birthdate);
}
function updateXP(birthdate) {
document.getElementById("xpNum").innerHTML = getYears(birthdate);
}
function updateDownloadPath(lang) {
document.getElementById("download").href = `files/resume-baptiste-meurant-${lang}.pdf`;
}
function updateEmail() {
document.getElementById("email").href = "mailto:[email protected]";
}
let lang=getLangFromLocale(locale);
i18Loader(lang);
updateAge("01/17/1980");
updateXP("10/01/2005");
updateDownloadPath(lang);
updateEmail();