Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
document.addEventListener("DOMContentLoaded", () => {
const languageOptions = document.querySelectorAll(".language-toggle .lang-option");
const defaultLanguage = "en";
const loader = document.querySelector(".loader");
let currentLanguage = localStorage.getItem("selectedLanguage");
if (!currentLanguage) {
currentLanguage = defaultLanguage;
localStorage.setItem("selectedLanguage", currentLanguage);
}
const loadTranslations = async (lang) => {
try {
const url = `./js/translations/${lang}.json`;
const response = await fetch(url, { credentials: "same-origin" });
if (!response.ok) {
throw new Error(`Failed to load ${lang} translations`);
}
const translations = await response.json();
applyTranslations(translations);
} catch (error) {
console.error(`Error loading ${lang} translations:`, error);
}
};
function calculateAge(birthDateStr) {
const birthDate = new Date(birthDateStr);
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
function applyTranslations(translations) {
const age = calculateAge("2007-07-03");
document.querySelectorAll('[data-translate]').forEach(el => {
const key = el.getAttribute('data-translate');
if (translations[key]) {
let text = translations[key];
text = text.replace("{age}", age);
el.textContent = text;
}
});
document.querySelectorAll('[data-translate-placeholder]').forEach(el => {
const key = el.getAttribute('data-translate-placeholder');
if (translations[key]) {
el.placeholder = translations[key];
}
});
document.querySelectorAll('[data-translate-title]').forEach(el => {
const key = el.getAttribute('data-translate-title');
if (translations[key]) {
el.title = translations[key];
}
});
document.dispatchEvent(new CustomEvent('languageChanged', { detail: { lang: currentLanguage } }));
}
const updateSelectedLanguage = (lang) => {
languageOptions.forEach((option) => {
option.classList.toggle("selected", option.getAttribute("data-lang") === lang);
});
};
const initializePage = async () => {
updateSelectedLanguage(currentLanguage);
await loadTranslations(currentLanguage);
document.body.classList.remove("loading");
loader.style.display = "none";
};
languageOptions.forEach((option) => {
option.addEventListener("click", () => {
const selectedLang = option.getAttribute("data-lang");
if (currentLanguage !== selectedLang) {
currentLanguage = selectedLang;
localStorage.setItem("selectedLanguage", currentLanguage);
updateSelectedLanguage(currentLanguage);
loadTranslations(currentLanguage);
}
});
});
document.body.classList.add("loading");
loader.style.display = "flex";
initializePage();
});