Internationalization plugin for grammY based on Project Fluent. Check out the official documentation for this plugin.
Node.js
npm install @grammyjs/i18n
Deno
import { I18n, I18nFlavor } from "https://deno.land/x/grammy_i18n/mod.ts";
Example project structure:
.
├─ locales/
│ ├── en.ftl
│ ├── it.ftl
│ └── ru.ftl
└── bot.ts
Example bot not using sessions:
import { Bot, Context } from "https://deno.land/x/grammy/mod.ts";
import { I18n, I18nFlavor } from "https://deno.land/x/grammy_i18n/mod.ts";
// For proper typings and auto-completions in IDEs,
// customize the `Context` using `I18nFlavor`.
type MyContext = Context & I18nFlavor;
// Create a new I18n instance.
const i18n = new I18n<MyContext>({
defaultLocale: "en",
directory: "locales",
});
// Create a bot as usual, but use the modified Context type.
const bot = new Bot<MyContext>(""); // <- Put your bot token here
// Remember to register this middleware before registering
// your handlers.
bot.use(i18n);
bot.command("start", async (ctx) => {
// Use the method `t` or `translate` from the context and pass
// in the message id (key) of the message you want to get.
await ctx.reply(ctx.t("greeting"));
});
// Start your bot
bot.start();
See the documentation and examples/ for more detailed examples.
Thanks to...
- Slava Fomin II (@slavafomin) for the Node.js implementation of the original Fluent plugin and the better Fluent integration.
- Roj (@roj1512) for the Deno port of the original @fluent/bundle and @fluent/langneg packages.
- Dunkan (@dcdunkan) for the Deno port of the @moebius/fluent.
- And all the previous maintainers and contributors of this i18n plugin.