This commit is contained in:
2025-11-26 22:44:59 +01:00
parent e5e32d20a1
commit ebc72f3e13
2 changed files with 170 additions and 52 deletions

View File

@@ -1,20 +1,57 @@
// public/assets/js/header.js
document.addEventListener('DOMContentLoaded', function () {
const supportedLangs = ['de', 'en', 'it', 'fr'];
// --------------------------------------------------
// Sprachauflösung: basiert NUR auf usbConfig / i18n
// --------------------------------------------------
const cfg = window.usbConfig || {};
const i18nCfg = cfg.i18n || {};
const availLangs = i18nCfg.available || {};
const availCodes = Object.keys(availLangs);
// Globale Config aus PHP-Partial (app_config.php)
const cfg = window.usbConfig || {};
const cfgLang = (cfg.lang || '').toLowerCase();
function normalizeLang(code) {
if (!code) return '';
return String(code).slice(0, 2).toLowerCase();
}
function resolveCurrentLang() {
const url = new URL(window.location.href);
const urlLang = (url.searchParams.get('lang') || '').toLowerCase();
const docLang = (document.documentElement.getAttribute('lang') || '').toLowerCase();
const url = new URL(window.location.href);
const urlLang = normalizeLang(url.searchParams.get('lang'));
if (supportedLangs.includes(urlLang)) return urlLang;
if (supportedLangs.includes(cfgLang)) return cfgLang;
if (supportedLangs.includes(docLang)) return docLang;
return 'de';
// 1) ?lang= aus der URL, wenn gültig
if (urlLang && availCodes.includes(urlLang)) {
return urlLang;
}
// 2) localStorage (vom lang.js gesetzt)
const stored = normalizeLang(localStorage.getItem('usbcheck_lang'));
if (stored && availCodes.includes(stored)) {
return stored;
}
// 3) PHP-Konfig (fileload/app_config)
const cfgLang = normalizeLang(i18nCfg.current || cfg.lang);
if (cfgLang && availCodes.includes(cfgLang)) {
return cfgLang;
}
// 4) <html lang="...">
const docLang = normalizeLang(document.documentElement.getAttribute('lang'));
if (docLang && availCodes.includes(docLang)) {
return docLang;
}
// 5) Fallback: en, wenn vorhanden
if (availCodes.includes('en')) {
return 'en';
}
// 6) sonst erste verfügbare Sprache
if (availCodes.length > 0) {
return availCodes[0];
}
// 7) absolute Notlösung
return 'en';
}
// --------------------------------------------------