Reihenfolge: gespeicherte Wahl → System-/Browsersprache (wenn ein Wörterbuch existiert) → Englisch. Umschalter überschreibt und merkt sich. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
59 lines
2.8 KiB
JavaScript
59 lines
2.8 KiB
JavaScript
/* Dashboard i18n. Dictionaries live in lang.js (window.LANG = { en: {...}, de: {...} }).
|
|
Loaded before app.js, so app.js can use the global t(). Start language: saved choice,
|
|
else the system/browser language if a dictionary exists, else 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; } })();
|
|
// saved choice → system/browser language (if we have it) → English
|
|
const sys = (() => { try { return (navigator.languages?.[0] || navigator.language || '').slice(0, 2).toLowerCase(); } catch { return ''; } })();
|
|
let lang = saved && dict()[saved] ? saved : sys && dict()[sys] ? sys : 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();
|
|
})();
|