fix: improve i18n robustness (null guards, race condition, error resilience)
This commit is contained in:
+18
-7
@@ -38,7 +38,12 @@ export async function initI18n() {
|
|||||||
currentLocale = resolveLocale();
|
currentLocale = resolveLocale();
|
||||||
fallbackTranslations = await loadLocale(DEFAULT_LOCALE);
|
fallbackTranslations = await loadLocale(DEFAULT_LOCALE);
|
||||||
if (currentLocale !== DEFAULT_LOCALE) {
|
if (currentLocale !== DEFAULT_LOCALE) {
|
||||||
|
try {
|
||||||
translations = await loadLocale(currentLocale);
|
translations = await loadLocale(currentLocale);
|
||||||
|
} catch {
|
||||||
|
translations = fallbackTranslations;
|
||||||
|
currentLocale = DEFAULT_LOCALE;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
translations = fallbackTranslations;
|
translations = fallbackTranslations;
|
||||||
}
|
}
|
||||||
@@ -50,11 +55,11 @@ export async function setLocale(locale) {
|
|||||||
if (!SUPPORTED_LOCALES.includes(locale)) return;
|
if (!SUPPORTED_LOCALES.includes(locale)) return;
|
||||||
localStorage.setItem(STORAGE_KEY, locale);
|
localStorage.setItem(STORAGE_KEY, locale);
|
||||||
currentLocale = locale;
|
currentLocale = locale;
|
||||||
if (locale === DEFAULT_LOCALE) {
|
const loaded = locale === DEFAULT_LOCALE
|
||||||
translations = fallbackTranslations;
|
? fallbackTranslations
|
||||||
} else {
|
: await loadLocale(locale);
|
||||||
translations = await loadLocale(locale);
|
if (currentLocale !== locale) return;
|
||||||
}
|
translations = loaded;
|
||||||
document.documentElement.lang = locale;
|
document.documentElement.lang = locale;
|
||||||
window.dispatchEvent(new CustomEvent('locale-changed', { detail: { locale } }));
|
window.dispatchEvent(new CustomEvent('locale-changed', { detail: { locale } }));
|
||||||
}
|
}
|
||||||
@@ -80,18 +85,24 @@ export function getSupportedLocales() {
|
|||||||
|
|
||||||
/** Datum locale-aware formatieren */
|
/** Datum locale-aware formatieren */
|
||||||
export function formatDate(date) {
|
export function formatDate(date) {
|
||||||
|
if (date == null) return '';
|
||||||
|
const d = date instanceof Date ? date : new Date(date);
|
||||||
|
if (isNaN(d.getTime())) return '';
|
||||||
return new Intl.DateTimeFormat(currentLocale, {
|
return new Intl.DateTimeFormat(currentLocale, {
|
||||||
day: '2-digit',
|
day: '2-digit',
|
||||||
month: '2-digit',
|
month: '2-digit',
|
||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
}).format(date instanceof Date ? date : new Date(date));
|
}).format(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Uhrzeit locale-aware formatieren */
|
/** Uhrzeit locale-aware formatieren */
|
||||||
export function formatTime(date) {
|
export function formatTime(date) {
|
||||||
|
if (date == null) return '';
|
||||||
|
const d = date instanceof Date ? date : new Date(date);
|
||||||
|
if (isNaN(d.getTime())) return '';
|
||||||
return new Intl.DateTimeFormat(currentLocale, {
|
return new Intl.DateTimeFormat(currentLocale, {
|
||||||
hour: '2-digit',
|
hour: '2-digit',
|
||||||
minute: '2-digit',
|
minute: '2-digit',
|
||||||
hour12: false,
|
hour12: false,
|
||||||
}).format(date instanceof Date ? date : new Date(date));
|
}).format(d);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user