[dyad] Fixing the translation logic - wrote 1 file(s)
This commit is contained in:
@@ -12,7 +12,7 @@ interface I18nContextType {
|
|||||||
|
|
||||||
const I18nContext = createContext<I18nContextType | undefined>(undefined);
|
const I18nContext = createContext<I18nContextType | undefined>(undefined);
|
||||||
|
|
||||||
const translations: { [key: string]: { [key: string]: string } } = {
|
const translations: { [key: string]: any } = {
|
||||||
en,
|
en,
|
||||||
de,
|
de,
|
||||||
};
|
};
|
||||||
@@ -21,13 +21,21 @@ export function I18nProvider({ children }: { children: ReactNode }) {
|
|||||||
const [language, setLanguage] = useState('en');
|
const [language, setLanguage] = useState('en');
|
||||||
|
|
||||||
const t = (key: string, params?: { [key: string]: string | number }) => {
|
const t = (key: string, params?: { [key: string]: string | number }) => {
|
||||||
let translation = translations[language]?.[key] || key;
|
const translation = key.split('.').reduce((acc, currentKey) => {
|
||||||
|
if (acc && typeof acc === 'object' && currentKey in acc) {
|
||||||
|
return acc[currentKey];
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}, translations[language]);
|
||||||
|
|
||||||
|
let result = (typeof translation === 'string' ? translation : key);
|
||||||
|
|
||||||
if (params) {
|
if (params) {
|
||||||
Object.keys(params).forEach(paramKey => {
|
Object.keys(params).forEach(paramKey => {
|
||||||
translation = translation.replace(`{${paramKey}}`, String(params[paramKey]));
|
result = result.replace(`{${paramKey}}`, String(params[paramKey]));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return translation;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user