From 8c836de71b47744ec3e99cb8b3c853528fa66b52 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 13 Dec 2021 12:35:10 +0100 Subject: chore: load i18n translations using lingui provider --- src/utils/helpers/i18n.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/utils/helpers/i18n.ts (limited to 'src/utils/helpers') diff --git a/src/utils/helpers/i18n.ts b/src/utils/helpers/i18n.ts new file mode 100644 index 0000000..38616a2 --- /dev/null +++ b/src/utils/helpers/i18n.ts @@ -0,0 +1,35 @@ +import type { I18n } from '@lingui/core'; +import { en, fr } from 'make-plural/plurals'; + +export interface LocaleData { + messages: object; + default: object; +} + +export const locales = { + en: 'English', + fr: 'Français', +}; + +export const defaultLocale = 'fr'; + +export function initTranslation(i18n: I18n): void { + i18n.loadLocaleData({ + en: { plurals: en }, + fr: { plurals: fr }, + }); +} + +export async function loadTranslation( + locale: string, + isProduction: boolean = true +) { + let data: LocaleData; + if (isProduction) { + data = await import(`src/i18n/${locale}/messages`); + } else { + data = await import(`@lingui/loader!src/i18n/${locale}/messages.po`); + } + + return data.messages; +} -- cgit v1.2.3