-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguagePicker.js
More file actions
38 lines (34 loc) · 1.38 KB
/
LanguagePicker.js
File metadata and controls
38 lines (34 loc) · 1.38 KB
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
document.addEventListener("DOMContentLoaded", () => {
const languageSelect = document.getElementById("languageSelect");
const defaultLang = localStorage.getItem("siteLang") || "nl";
function loadLanguage(lang) {
fetch(`lang/${lang}.json`)
.then(res => {
if (!res.ok) throw new Error(`Language-file not found: ${lang}.json`);
return res.json();
})
.then(data => {
console.log("✅ File successfully loaded:", data);
for (const key in data) {
const el = document.getElementById(key);
if (el) {
el.textContent = data[key];
} else {
console.warn(`⚠️ Element with ID '${key}' doesn't exists on this page. This can happen when it wants to load an element that's not on this page, but on another one. If you don't see any text on this page, please reload the page.`);
}
}
})
.catch(err => {
console.error("❌ Error with loading file:", err, "Please refresh the site or contact me via my GitHub-profile.");
});
document.documentElement.lang = lang;
localStorage.setItem("siteLang", lang);
if (languageSelect) languageSelect.value = lang;
}
loadLanguage(defaultLang);
if (languageSelect) {
languageSelect.addEventListener("change", (e) => {
loadLanguage(e.target.value);
});
}
});