summaryrefslogtreecommitdiffstats
path: root/.husky/commit-msg
blob: e8511eaeaf61dd7e09c3e9f35cdee6c636568f1c (plain)
1
2
3
4
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit $1
*/ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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;
  }
}