@foxxie/i18n
Version:
I18n management utility for Foxxie projects.
110 lines (107 loc) • 2.87 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/index.ts
import { default as default2 } from "i18next";
// src/lib/init.ts
import Collection from "@discordjs/collection";
import { Locale } from "discord-api-types/v10";
import i18next, { getFixedT } from "i18next";
import { opendir } from "node:fs/promises";
import { join } from "node:path";
import backend from "i18next-fs-backend";
var discordLocales = new Set(Object.values(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) {
i18next.use(backend);
const languagesDir = options.languageDirectory;
await load(join(languagesDir));
await i18next.init({
ns: [
...loadedNamespaces
],
preload: [
...loadedLocales
],
initImmediate: false,
interpolation: {
escapeValue: false,
skipOnVariables: false,
...options.interpolation
},
ignoreJSONStructure: false,
backend: {
loadPath: join(languagesDir, "{{lng}}", "{{ns}}.json"),
addPath: languagesDir
},
...options
});
for (const { name, format } of formatters) {
i18next.services.formatter.add(name, format);
}
}
__name(init, "init");
async function load(directory) {
const dir = await opendir(directory);
for await (const entry of dir) {
if (!entry.isDirectory())
continue;
loadedLocales.add(entry.name);
await loadLocale(join(dir.path, entry.name), "");
}
loadedPaths.add(dir.path);
}
__name(load, "load");
async function loadLocale(directory, ns) {
const dir = await opendir(directory);
for await (const entry of dir) {
if (entry.isDirectory()) {
await loadLocale(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 Collection();
function getT(locale) {
if (!loadedLocales.has(locale))
process.emitWarning(`could not find locale (${locale})`);
return cache.ensure(locale, () => 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");
export {
FT,
T,
addFormatters,
discordLocales,
formatters,
getT,
default2 as i18next,
init,
isDiscordLocale,
load,
loadedLocales,
loadedNamespaces,
loadedPaths
};
//# sourceMappingURL=index.mjs.map