Fix i18n: auto-initialize locale on module load
Some checks failed
Validate / Hassfest (push) Has been cancelled

The locale defaulted to 'en' until onMount ran initLocale(), causing
all server-side and initial client renders to show English even when
Russian was saved in localStorage.

Fix: read localStorage synchronously at module import time so t()
returns the correct locale from the first render.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 17:38:10 +03:00
parent ca6a9c8830
commit 5dee7c55ca

View File

@@ -12,6 +12,19 @@ const translations: Record<Locale, Record<string, any>> = { en, ru };
let currentLocale: Locale = 'en';
// Auto-initialize from localStorage on module load (client-side)
if (typeof localStorage !== 'undefined') {
const saved = localStorage.getItem('locale') as Locale | null;
if (saved && saved in translations) {
currentLocale = saved;
} else if (typeof navigator !== 'undefined') {
const lang = navigator.language.slice(0, 2);
if (lang in translations) {
currentLocale = lang as Locale;
}
}
}
export function getLocale(): Locale {
return currentLocale;
}