From f861e6a269ba9f62700776d3cd13b644a9e836d4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 20 Sep 2023 16:38:54 +0200 Subject: 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. --- src/utils/helpers/server/i18n.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/utils/helpers/server/i18n.ts (limited to 'src/utils/helpers/server/i18n.ts') diff --git a/src/utils/helpers/server/i18n.ts b/src/utils/helpers/server/i18n.ts new file mode 100644 index 0000000..dbbc4e5 --- /dev/null +++ b/src/utils/helpers/server/i18n.ts @@ -0,0 +1,31 @@ +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; + } +} -- cgit v1.2.3