From 5dee7c55caba45c6ae0e7bbab699e3d574ba2193 Mon Sep 17 00:00:00 2001 From: "alexei.dolgolyov" Date: Thu, 19 Mar 2026 17:38:10 +0300 Subject: [PATCH] Fix i18n: auto-initialize locale on module load 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) --- frontend/src/lib/i18n/index.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/frontend/src/lib/i18n/index.ts b/frontend/src/lib/i18n/index.ts index 6d6c009..2545c6d 100644 --- a/frontend/src/lib/i18n/index.ts +++ b/frontend/src/lib/i18n/index.ts @@ -12,6 +12,19 @@ const translations: Record> = { 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; }