@grully/i18n-pug
v1.0.2
Published
Pug plugin for @grully/i18n
Downloads
20
Maintainers
Readme
@grully/i18n-pug
Pug template plugin for @grully/i18n – allows you to use Pug (formerly Jade) templates as translation files in your grammY bot.
Write expressive, dynamic translations with Pug’s powerful syntax – includes, mixins, variables, and logic – all fully supported.
Features
- 🧩 Seamless integration with
@grully/i18n. - 📁 Translation files end with
.pug. - 🎨 Supports all Pug features: HTML/text output, variables, conditionals, loops, mixins.
Installation
npm install @grully/i18n-pugExample
import { env } from "bun"
import { Bot, session, type Context, type SessionFlavor } from "grammy"
import i18n, { type GrullyI18nFlavor, type GrullyI18nSessionData } from '@grully/i18n'
import i18nPug from '@grully/i18n-pug'
type BotContext = Context & GrullyI18nFlavor & SessionFlavor<GrullyI18nSessionData>
type MyBot = Bot<BotContext>
const TOKEN = env.TOKEN!
const bot: MyBot = new Bot(
TOKEN
)
bot.use(session({
initial: () => {
return {
languageCode: 'en'
}
}
}))
bot.use(i18n({
folder: 'locales',
defaultLocale: 'en',
plugin: i18nPug()
}))
bot.command(
'start',
async ctx => {
const result = ctx.t(
'start/start',
{
name: ctx.from?.first_name
}
)
await ctx.reply(result)
}
)
bot.command(
'locales',
async ctx => {
const result = ctx.t(
'locales',
{
locales: ctx.i18n.locales
}
)
await ctx.reply(result)
}
)
bot.command(
'set_lang',
async ctx => {
const key = ctx.match
const lang = key ?? 'ru'
ctx.session.languageCode = lang
await ctx.reply(ctx.t('set_lang', { lang: lang }))
}
)
bot.api.config.use(
async (prev, method, payload, abort) => {
return prev(
method,
{
...payload,
parse_mode: 'HTML'
},
abort
)
}
)
bot.start({
onStart: info => {
console.log(info)
}
})