brendmanager/src/i18n.js

19 lines
683 B
JavaScript

import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "../locales");
export const LANGS = ["ru", "kk", "en"];
export const TEXTS = Object.fromEntries(LANGS.map((l) => [l, JSON.parse(fs.readFileSync(path.join(DIR, l + ".json"), "utf8"))]));
export const pickLang = (...hints) => {
for (const h of hints) {
const l = String(h ?? "").trim().slice(0, 2).toLowerCase();
if (LANGS.includes(l)) return l;
}
return "ru";
};
export const t = (lang, key, vars = {}) =>
(TEXTS[lang]?.[key] ?? TEXTS.ru[key] ?? key).replace(/\{(\w+)\}/g, (_, n) => vars[n] ?? "");