UNPKG

@foxxie/i18n

Version:

I18n management utility for Foxxie projects.

147 lines (144 loc) 4.79 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { FT: () => FT, T: () => T, addFormatters: () => addFormatters, discordLocales: () => discordLocales, formatters: () => formatters, getT: () => getT, i18next: () => import_i18next2.default, init: () => init, isDiscordLocale: () => isDiscordLocale, load: () => load, loadedLocales: () => loadedLocales, loadedNamespaces: () => loadedNamespaces, loadedPaths: () => loadedPaths }); module.exports = __toCommonJS(src_exports); var import_i18next2 = __toESM(require("i18next")); // src/lib/init.ts var import_collection = __toESM(require("@discordjs/collection")); var import_v10 = require("discord-api-types/v10"); var import_i18next = __toESM(require("i18next")); var import_promises = require("fs/promises"); var import_node_path = require("path"); var import_i18next_fs_backend = __toESM(require("i18next-fs-backend")); var discordLocales = new Set(Object.values(import_v10.Locale)); function isDiscordLocale(lng) { return discordLocales.has(lng); } __name(isDiscordLocale, "isDiscordLocale"); var loadedLocales = /* @__PURE__ */ new Set(); var loadedNamespaces = /* @__PURE__ */ new Set(); var loadedPaths = /* @__PURE__ */ new Set(); var formatters = []; function addFormatters(...formatters1) { formatters1.push(...formatters1); } __name(addFormatters, "addFormatters"); async function init(options) { import_i18next.default.use(import_i18next_fs_backend.default); const languagesDir = options.languageDirectory; await load((0, import_node_path.join)(languagesDir)); await import_i18next.default.init({ ns: [ ...loadedNamespaces ], preload: [ ...loadedLocales ], initImmediate: false, interpolation: { escapeValue: false, skipOnVariables: false, ...options.interpolation }, ignoreJSONStructure: false, backend: { loadPath: (0, import_node_path.join)(languagesDir, "{{lng}}", "{{ns}}.json"), addPath: languagesDir }, ...options }); for (const { name, format } of formatters) { import_i18next.default.services.formatter.add(name, format); } } __name(init, "init"); async function load(directory) { const dir = await (0, import_promises.opendir)(directory); for await (const entry of dir) { if (!entry.isDirectory()) continue; loadedLocales.add(entry.name); await loadLocale((0, import_node_path.join)(dir.path, entry.name), ""); } loadedPaths.add(dir.path); } __name(load, "load"); async function loadLocale(directory, ns) { const dir = await (0, import_promises.opendir)(directory); for await (const entry of dir) { if (entry.isDirectory()) { await loadLocale((0, import_node_path.join)(dir.path, entry.name), `${ns}${entry.name}/`); } else if (entry.isFile() && entry.name.endsWith(".json")) { loadedNamespaces.add(`${ns}${entry.name.slice(0, -5)}`); } } } __name(loadLocale, "loadLocale"); var cache = new import_collection.default(); function getT(locale) { if (!loadedLocales.has(locale)) process.emitWarning(`could not find locale (${locale})`); return cache.ensure(locale, () => (0, import_i18next.getFixedT)(locale)); } __name(getT, "getT"); // src/lib/types.ts function T(k) { return k; } __name(T, "T"); function FT(k) { return k; } __name(FT, "FT"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FT, T, addFormatters, discordLocales, formatters, getT, i18next, init, isDiscordLocale, load, loadedLocales, loadedNamespaces, loadedPaths }); //# sourceMappingURL=index.js.map