diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-01-29 19:03:59 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-01-29 19:03:59 +0100 |
| commit | 8fb5e4ef3ae925ebc6622711fb5c8c6147642cbc (patch) | |
| tree | 9e99137a7b64ea7993a8311a7162336a551be8b2 /src/utils/helpers/i18n.ts | |
| parent | 2bae7c43764df5678fe2fc2e68be11ae95d85a41 (diff) | |
| parent | e4d5b8151802517b2943756fc0d09ffa95e2c4e2 (diff) | |
feat(i18n): replace linguijs with formatjs
Diffstat (limited to 'src/utils/helpers/i18n.ts')
| -rw-r--r-- | src/utils/helpers/i18n.ts | 91 |
1 files changed, 27 insertions, 64 deletions
diff --git a/src/utils/helpers/i18n.ts b/src/utils/helpers/i18n.ts index 4439906..16c83f4 100644 --- a/src/utils/helpers/i18n.ts +++ b/src/utils/helpers/i18n.ts @@ -1,86 +1,49 @@ -import { messages as messagesEn } from '@i18n/en/messages.js'; -import { messages as messagesFr } from '@i18n/fr/messages.js'; -import { i18n, Messages } from '@lingui/core'; -import { en, fr } from 'make-plural/plurals'; +import { config } from '@config/website'; +import { createIntl, createIntlCache, IntlShape } from '@formatjs/intl'; +import { readFile } from 'fs/promises'; +import path from 'path'; -type Catalog = { - messages: Messages; -}; +type Messages = { [key: string]: string }; -export const locales = { - en: 'English', - fr: 'Français', -}; - -export const defaultLocale = 'fr'; +export const defaultLocale = config.locales.defaultLocale; /** - * Load the translation with the correct method depending on environment. + * Load the translation for the provided locale. * - * @param {string} locale - The current locale. - * @returns {Promise<Messages>} The translated messages. + * @param currentLocale - The current locale. + * @returns {Promise<Messages>} The translated strings. */ -export async function loadTranslation(locale: string): Promise<Messages> { - let catalog: Catalog; - - try { - if (process.env.NODE_ENV === 'production') { - catalog = await import(`src/i18n/${locale}/messages`); - } else { - catalog = await import(`@lingui/loader!src/i18n/${locale}/messages.po`); - } +export async function loadTranslation( + currentLocale: string | undefined +): Promise<Messages> { + const locale: string = currentLocale || defaultLocale; - return catalog.messages; - } catch (error) { - console.error('Error while loading translation.'); - throw error; - } -} + const languagePath = path.join(process.cwd(), `lang/${locale}.json`); -/** - * Init lingui. - * - * @param {string} locale - The locale to activate. - * @param {Messages} [messages] - The compiled translation. - */ -export function initLingui(locale: string, messages?: Messages) { try { - i18n.loadLocaleData({ - en: { plurals: en }, - fr: { plurals: fr }, - }); - - if (messages) { - i18n.load(locale, messages); - } else { - i18n.load({ - en: messagesEn, - fr: messagesFr, - }); - } - - i18n.activate(locale, Object.keys(locales)); + const contents = await readFile(languagePath, 'utf8'); + return JSON.parse(contents); } catch (error) { - console.error('Error while Lingui init.'); + console.error( + 'Error: Could not load compiled language files. Please run `yarn run i18n:compile` first."' + ); throw error; } } /** - * Activate the given locale. + * Create an Intl object to be used outside components. * - * @param {string} locale - The locale to activate. - * @param {Messages} messages - The compiled translation. + * @returns {<Promise<IntlShape<string>>} The Intl object. */ -export function activateLocale(currentLocale: string, messages: Messages) { - const locale: string = Object.keys(locales).includes(currentLocale) - ? currentLocale - : defaultLocale; - +export async function getIntlInstance(): Promise<IntlShape<string>> { try { - initLingui(locale, messages); + const cache = createIntlCache(); + const messages = await loadTranslation(defaultLocale); + + return createIntl({ locale: defaultLocale, messages }, cache); } catch (error) { - console.error(`Error while activating ${currentLocale}`); + console.error('Error: Could not create an Intl instance.'); throw error; } } |
