Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | /** * @copyright nhcarrigan * @license Naomi's Public License * @author Naomi Carrigan */ import { responses } from "../i18n/responses.js"; /** * Translates a key to the specified locale, performing * interpolation on the string. * @param key -- The key to translate. * @param locale -- The user's locale. * @param interpolation -- An object of keys to replace with values. * @returns The translated string. */ export const i18n = ( key: keyof (typeof responses)["en"], locale: string, interpolation: Record<string, unknown> = {}, ): string => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- We know the en key exists, but having the loose type helps. const string = responses[locale]?.[key] ?? responses.en![key]; // eslint-disable-next-line unicorn/no-array-reduce -- This is the cleanest way to do it, really. return Object.entries(interpolation).reduce((accumulator, [ k, v ]) => { return accumulator.replace(`{{${k}}}`, String(v)); }, string); }; |