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.

This commit is contained in:
2025-07-24 21:01:12 +02:00
parent f3ccd766fa
commit 1d8b25f872

View File

@@ -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';
}