From 1d8b25f8723e8a47adc4e70364f72e0df925c4d4 Mon Sep 17 00:00:00 2001 From: Richard Bergsma Date: Thu, 24 Jul 2025 21:01:12 +0200 Subject: [PATCH] Implement enhanced translation function in i18n utility to support fallback to English and improve localization handling. Update getCurrentLocale function to default to 'en' for now. --- src/utils/i18n.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index c8477b12..b7734ef0 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -58,11 +58,33 @@ export async function useTranslations(lang: Locale) { // Simple translation function for components that don't need async export function t(key: string): string { - // This is a fallback - in practice, components should use useTranslations - return key; + // Get current locale (default to 'en' for now) + const currentLocale = getCurrentLocale(); + const translationData = translations[currentLocale] || translations.en || {}; + + const keys = key.split('.'); + let value: any = translationData; + + for (const k of keys) { + value = value?.[k]; + if (value === undefined) break; + } + + // Fallback to English if translation missing + if (value === undefined && currentLocale !== 'en') { + let fallback: any = translations.en; + for (const k of keys) { + fallback = fallback?.[k]; + if (fallback === undefined) break; + } + value = fallback; + } + + return value || key; } export function getCurrentLocale(): Locale { + // For now, default to 'en' - this should be enhanced to detect from URL return 'en'; }