summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/i18n.ts
blob: 5d19c8c48c59ad7d5165e5d2b60d08712d2548dd (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { createIntl, createIntlCache, IntlShape } from '@formatjs/intl';
import { settings } from '@utils/config';
import { readFile } from 'fs/promises';
import path from 'path';

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;
  }
}
for (var r in e) e[r] = e[r].replace(/<[\w\s]+>/g, function (r) { return '(?:' + e[r].trim() + ')'; }); return e[r]; })({ '<ureal dec>': '\\d+(?:/\\d+)|(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[esfdl][+-]?\\d+)?', '<real dec>': '[+-]?<ureal dec>|[+-](?:inf|nan)\\.0', '<imaginary dec>': '[+-](?:<ureal dec>|(?:inf|nan)\\.0)?i', '<complex dec>': '<real dec>(?:@<real dec>|<imaginary dec>)?|<imaginary dec>', '<num dec>': '(?:#d(?:#[ei])?|#[ei](?:#d)?)?<complex dec>', '<ureal box>': '[0-9a-f]+(?:/[0-9a-f]+)?', '<real box>': '[+-]?<ureal box>|[+-](?:inf|nan)\\.0', '<imaginary box>': '[+-](?:<ureal box>|(?:inf|nan)\\.0)?i', '<complex box>': '<real box>(?:@<real box>|<imaginary box>)?|<imaginary box>', '<num box>': '#[box](?:#[ei])?|(?:#[ei])?#[box]<complex box>', '<number>': '(^|[()\\[\\]\\s])(?:<num dec>|<num box>)(?=[()\\[\\]\\s]|$)', }), 'i' ), lookbehind: !0, }, boolean: { pattern: /(^|[()\[\]\s])#(?:[ft]|false|true)(?=[()\[\]\s]|$)/, lookbehind: !0, }, function: { pattern: /((?:^|[^'`#])[(\[])(?:[^|()\[\]'\s]+|\|(?:[^\\|]|\\.)*\|)(?=[()\[\]\s]|$)/, lookbehind: !0, }, identifier: { pattern: /(^|[()\[\]\s])\|(?:[^\\|]|\\.)*\|(?=[()\[\]\s]|$)/, lookbehind: !0, greedy: !0, }, punctuation: /[()\[\]']/, }; })(Prism);