@imjxsx/i18n
v3.0.0
Published
A simple and lightweight JSON-based i18n library for Node.js applications
Downloads
4
Maintainers
Readme
# With NPM
npm install @imjxsx/i18n
# With PNPM
pnpm add @imjxsx/i18n
# With Yarn
yarn add @imjxsx/i18n
# With Bun
bun add @imjxsx/i18n// locales/en.json
{
"unknown-command": "The command '%command%' is not valid, use '%prefix%help' for help.",
"registration-completed": "You have successfully registered.",
"without-permission": "You do not have permission to use this command.",
"hello-world": "Hello, World!"
}// locales/es.json
{
"unknown-command": "El comando '%command%' no es válido. Usa '%prefix%help' para obtener ayuda.",
"registration-completed": "Te has registrado correctamente.",
"without-permission": "No tienes permiso para usar este comando."
}// index.js
import I18n from "@imjxsx/i18n";
// (directory: string, callback: string, logger?: Optional<Logger>)
const i18n = new I18n("./locales", "en");
await i18n.load();
const user = {
nickname: "I'm Jxsx",
language: "es",
};
console.log(i18n.t("unknown-command", user.language, {
command: "ayuda",
prefix: "#",
})); // -> "El comando 'ayuda' no es válido. Usa '#help' para obtener ayuda."
console.log(i18n.t("registration-completed", user.language)); // -> "Te has registrado correctamente."
console.log(i18n.t("hello-world", user.language)); // -> "Hello, World!" Because the "hello-world" key does not exist in the "es" locale.
console.log(i18n.t("invalid-key", user.language)); // -> "invalid-key" Returns the key back because it is not in any locale.