Front: Type Safe internationalization

This commit is contained in:
Arthi-chaud
2022-08-08 12:10:20 +02:00
parent e7af552656
commit 25281e5539
2 changed files with 15 additions and 4 deletions
+15 -4
View File
@@ -1,7 +1,11 @@
import { en, fr } from './translations';
import { en, fr } from './Translations';
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
export type AvailableLanguages = 'en' | 'fr';
export const DefaultLanguage: AvailableLanguages = 'en';
i18n
.use(initReactI18next)
.init({
@@ -13,10 +17,17 @@ i18n
translation: fr
}
},
lng: "en",
fallbackLng: "en",
lng: DefaultLanguage,
fallbackLng: DefaultLanguage,
interpolation: {
escapeValue: false
}
});
export default i18n;
export default i18n;
/**
* Typesafe translation method
* @param textKey the key of th text to translate
* @returns the translated text
*/
export const translate = (textKey: keyof typeof en) => i18n.t(textKey);