summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/i18n.ts
blob: dd010c468105b337bb4cfe0ba82507f0d858f732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { config } from '@config/website';
import { readFile } from 'fs/promises';
import path from 'path';

type Messages = { [key: string]: string };

/**
 * 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 || config.locales.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;
  }
}