let r={en:"English"},o=localStorage.getItem("susfs_language")||"en";async function f(){try{const e=await fetch("/languages/languages.json");e.ok?(r=await e.json(),console.log("Available languages loaded:",r)):console.error("Could not load languages.json, using defaults")}catch(e){console.error("Error loading languages.json:",e)}}async function c(e){try{const n=await(await fetch(`/languages/${e}.xml`)).text();return new DOMParser().parseFromString(n,"text/xml")}catch(t){return console.error(`Failed to load language ${e}:`,t),e!=="en"?c("en"):null}}function m(e){if(!e)return;const t=e.getElementsByTagName("string");for(let n=0;n<t.length;n++){const a=t[n].getAttribute("id"),l=t[n].textContent;document.querySelectorAll(`[data-i18n="${a}"]`).forEach(i=>{i.textContent=l})}}async function s(e){if(!r[e]){console.error(`Language ${e} not supported`);return}const t=await c(e);if(t){m(t),localStorage.setItem("susfs_language",e),o=e;const n=document.getElementById("language");n&&(n.value=e)}}async function y(){await f(),u(),await s(o)}async function p(){const e=document.getElementById("language");await s(o),e.addEventListener("change",t=>{s(t.target.value)})}function u(){const e=document.getElementById("language");for(const[t,n]of Object.entries(r)){const a=document.createElement("option");a.value=t,a.textContent=n,a.className="text-black dark:text-white dark:bg-black",t===o&&(a.selected=!0),e.appendChild(a)}e.addEventListener("change",t=>{s(t.target.value)})}async function x(e){const t=await c(o);if(!t)return;const n=t.getElementsByTagName("string");for(let a=0;a<n.length;a++){const l=n[a].getAttribute("id"),g=n[a].textContent;e.querySelectorAll(`[data-i18n="${l}"]`).forEach(d=>{d.textContent=g})}e.querySelector("#language")||u(),p()}window.i18n={init:y,switchLanguage:s,getCurrentLanguage:()=>o,applyTranslationsToNewContent:x};
