/* Dashboard i18n. Dictionaries live in lang.js (window.LANG = { en: {...}, de: {...} }). Loaded before app.js, so app.js can use the global t(). Default language: English. t(key, vars) -> translated string; {name} placeholders filled from vars I18N.apply(root) fills [data-i18n] (text), [data-i18n-html], [data-i18n-title], [data-i18n-placeholder] I18N.set(code) switch language, remember it, re-apply, notify listeners I18N.get() current language code I18N.langs { code: 'Native name' } for the picker I18N.locale() matching BCP-47 locale for Date/number formatting I18N.onChange(fn) run fn(code) after every switch (used to re-render dynamic parts) Add a language: add its dictionary in lang.js and a row in LANGS below. */ (function () { 'use strict'; const LANGS = { en: 'English', de: 'Deutsch' }; const LOCALE = { en: 'en-US', de: 'de-DE' }; const DEFAULT = 'en'; const dict = () => (typeof window !== 'undefined' && window.LANG) || {}; const saved = (() => { try { return localStorage.getItem('dash.lang'); } catch { return null; } })(); let lang = saved && dict()[saved] ? saved : DEFAULT; const listeners = []; function t(key, vars) { let s = dict()[lang]?.[key] ?? dict()[DEFAULT]?.[key] ?? key; if (vars) for (const [k, v] of Object.entries(vars)) s = s.split('{' + k + '}').join(String(v)); return s; } function apply(root) { root = root || document; for (const el of root.querySelectorAll('[data-i18n]')) el.textContent = t(el.dataset.i18n); for (const el of root.querySelectorAll('[data-i18n-html]')) el.innerHTML = t(el.dataset.i18nHtml); for (const el of root.querySelectorAll('[data-i18n-title]')) el.title = t(el.dataset.i18nTitle); for (const el of root.querySelectorAll('[data-i18n-placeholder]')) el.placeholder = t(el.dataset.i18nPlaceholder); document.documentElement.lang = lang; } function set(code) { if (!dict()[code] || code === lang) return; lang = code; try { localStorage.setItem('dash.lang', code); } catch {} apply(); for (const fn of listeners) { try { fn(code); } catch (e) { console.error(e); } } } window.I18N = { t, apply, set, get: () => lang, langs: LANGS, locale: () => LOCALE[lang] || 'en-US', onChange: (fn) => listeners.push(fn), }; window.t = t; if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', () => apply()); else apply(); })();