Package Exports
- @grammyjs/i18n
- @grammyjs/i18n/dist/source/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@grammyjs/i18n) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
i18n for grammY and Telegraf
Internationalization middleware for grammY and Telegraf.
Installation
npm install @grammyjs/i18n
Example
yaml and json are ok
Example directory structure:
├── locales
│ ├── en.yaml
│ ├── en-US.yaml
│ ├── it.json
│ └── ru.yaml
└── bot.js
import {Bot, session} from 'grammy'
import {I18n, pluralize} from '@grammyjs/i18n'
const i18n = new I18n({
defaultLanguageOnMissing: true, // implies allowMissing = true
directory: 'locales',
useSession: true,
})
// Also you can provide i18n data directly
i18n.loadLocale('en', {greeting: 'Hello!'})
const bot = new Bot(process.env['BOT_TOKEN']!)
bot.use(session())
bot.use(i18n.middleware())
// Start message handler
bot.command('start', async ctx => ctx.reply(ctx.i18n.t('greeting')))
bot.start()
A full example for both grammY and Telegraf are in the examples folder.
User context
Commonly used Context functions:
bot.use(ctx => {
ctx.i18n.locale() // Get current locale
ctx.i18n.locale(code) // Set current locale
ctx.i18n.t(resourceKey, [data]) // Get resource value (data will be used by template engine)
});