feat: add i18n with type safety

This commit is contained in:
Sonny
2024-04-30 00:36:13 +02:00
committed by Sonny
parent 31f22d382e
commit 2cc490b611
32 changed files with 706 additions and 30 deletions

64
inertia/i18n/index.ts Normal file
View File

@@ -0,0 +1,64 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
// TODO: Refacot this stuff
// /!\ Code completion has to keep working
import frResourceAbout from './locales/fr/about.json';
import frResourceAdmin from './locales/fr/admin.json';
import frResourceCommon from './locales/fr/common.json';
import frResourceHome from './locales/fr/home.json';
import frResourceLogin from './locales/fr/login.json';
import frResourcePrivacy from './locales/fr/privacy.json';
import frResourceTerms from './locales/fr/terms.json';
import enResourceAbout from './locales/en/about.json';
import enResourceAdmin from './locales/en/admin.json';
import enResourceCommon from './locales/en/common.json';
import enResourceHome from './locales/en/home.json';
import enResourceLogin from './locales/en/login.json';
import enResourcePrivacy from './locales/en/privacy.json';
import enResourceTerms from './locales/en/terms.json';
type I18nFR =
| RemoveSuffix<Leaves<typeof frResourceAbout>>
| RemoveSuffix<Leaves<typeof frResourceAdmin>>
| RemoveSuffix<Leaves<typeof frResourceCommon>>
| RemoveSuffix<Leaves<typeof frResourceHome>>
| RemoveSuffix<Leaves<typeof frResourceLogin>>
| RemoveSuffix<Leaves<typeof frResourcePrivacy>>
| RemoveSuffix<Leaves<typeof frResourceTerms>>;
export type I18nKey = I18nFR;
export const resources = {
en: {
about: enResourceAbout,
admin: enResourceAdmin,
common: enResourceCommon,
home: enResourceHome,
login: enResourceLogin,
privacy: enResourcePrivacy,
terms: enResourceTerms,
},
fr: {
about: frResourceAbout,
admin: frResourceAdmin,
common: frResourceCommon,
home: frResourceHome,
login: frResourceLogin,
privacy: frResourcePrivacy,
terms: frResourceTerms,
},
} as const;
i18n.use(initReactI18next).init({
returnNull: false,
resources,
fallbackLng: 'en',
interpolation: {
escapeValue: false,
},
defaultNS: 'common',
});
export default i18n;