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:
@@ -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';
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user