import { createIntl, createIntlCache, IntlShape } from '@formatjs/intl'; import { readFile } from 'fs/promises'; import path from 'path'; import { settings } from '../config'; export type Messages = { [key: string]: string }; export const defaultLocale = settings.locales.defaultLocale; /** * Load the translation for the provided locale. * * @param currentLocale - The current locale. * @returns {Promise} The translated strings. */ export async function loadTranslation( currentLocale: string | undefined ): Promise { const locale: string = currentLocale || defaultLocale; const languagePath = path.join(process.cwd(), `lang/${locale}.json`); try { const contents = await readFile(languagePath, 'utf8'); return JSON.parse(contents); } catch (error) { console.error( 'Error: Could not load compiled language files. Please run `yarn run i18n:compile` first."' ); throw error; } } /** * Create an Intl object to be used outside components. * * @returns {>} The Intl object. */ export async function getIntlInstance(): Promise> { try { const cache = createIntlCache(); const messages = await loadTranslation(defaultLocale); return createIntl({ locale: defaultLocale, messages }, cache); } catch (error) { console.error('Error: Could not create an Intl instance.'); throw error; } }