/i18n

Internationalization for grammY powered by Fluent.

Primary LanguageTypeScriptMIT LicenseMIT

grammY i18n

Internationalization plugin for grammY based on Project Fluent. Check out the official documentation for this plugin.

Installation

Node.js

npm install @grammyjs/i18n

Deno

import { I18n, I18nFlavor } from "https://deno.land/x/grammy_i18n/mod.ts";

Example

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.

Credits

Thanks to...