diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-09-20 16:38:54 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-09-20 16:38:54 +0200 |
| commit | f861e6a269ba9f62700776d3cd13b644a9e836d4 (patch) | |
| tree | a5a107e7a6e4ff8b4261fe04349357bc00b783ee /src/utils/helpers/i18n.ts | |
| parent | 03331c44276ec56e9f235e4d5ee75030455a753f (diff) | |
refactor: use named export for everything except pages
Next expect a default export for pages so only those components should
use default exports. Everything else should use named exports to
reduce the number of import statements.
Diffstat (limited to 'src/utils/helpers/i18n.ts')
| -rw-r--r-- | src/utils/helpers/i18n.ts | 49 |
1 files changed, 0 insertions, 49 deletions
diff --git a/src/utils/helpers/i18n.ts b/src/utils/helpers/i18n.ts deleted file mode 100644 index 35d495e..0000000 --- a/src/utils/helpers/i18n.ts +++ /dev/null @@ -1,49 +0,0 @@ -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<Messages>} The translated strings. - */ -export async function loadTranslation( - currentLocale: string | undefined -): Promise<Messages> { - 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 {<Promise<IntlShape<string>>} The Intl object. - */ -export async function getIntlInstance(): Promise<IntlShape<string>> { - 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; - } -} |
