telegraf-i18next
v5.0.0
Published
i18next Localization for telegraf.js.
Downloads
228
Maintainers
Readme
Features
- Helps with localization for your bot.
- This module uses i18next.
Resources
Getting started
Installation
$ npm install telegraf-i18next
or
$ yarn add telegraf-i18next
or
$ pnpm add telegraf-i18next
Example
const { Telegraf, session } = require('telegraf');
const { i18next } = require('telegraf-i18next');
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.use(session({ defaultSession: () => ({ locale: 'en' }) }))
bot.use(i18next({
debug: true,
lng: 'en',
fallbackLng: 'en',
supportedLngs: ['en', 'ru'],
resources: {
en: {
translation: {
hello: "Hello!",
changeLanguage: "You have changed the language!",
keyboard: { button: "Button Title!" },
messageOnPress: "You clicked on the ({{ buttonName }}) button!" // You can also send some data (Interpolation).
}
},
ru: {
translation: {
hello: "Привет!",
changeLanguage: "Вы изменили язык!",
keyboard: { button: "Название Кнопки!" },
messageOnPress: "Вы нажали на кнопку ({{ buttonName }})!" // You can also send some data (Interpolation).
}
}
}
}));
// how to change the language
bot.command('changeLanguage', async (ctx) => {
let language = ctx.session.locale == "en" ? "ru" : "en";
ctx.i18next.changeLanguage(language);
return ctx.reply(ctx.i18next.t('changeLanguage'));
});
bot.launch();
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));
i18next
const { i18next } = require('telegraf-i18next')
bot.use(i18next({
debug: true,
lng: 'en',
fallbackLng: 'en',
supportedLngs: ['en', 'ru'],
resources: {
en: {
translation: {
hello: "Hello!",
changeLanguage: "You have changed the language!",
keyboard: { button: "Button Title!" },
messageOnPress: "You clicked on the ({{ buttonName }}) button!" // You can also send some data (Interpolation).
}
},
ru: {
translation: {
hello: "Привет!",
changeLanguage: "Вы изменили язык!",
keyboard: { button: "Название Кнопки!" },
messageOnPress: "Вы нажали на кнопку ({{ buttonName }})!" // You can also send some data (Interpolation).
}
}
}
}));
Other options you can look at i18next
reply
const { reply } = require('telegraf-i18next')
bot.command('reply', reply('hello'));
match
const { match } = require('telegraf-i18next')
bot.command('keyboard', async (ctx) => {
return ctx.reply('Keyboard:', Markup.keyboard([[ctx.i18next.t('hello')]])))
});
bot.hears(match('hello'), reply('hello'));
t
const { t } = require('telegraf-i18next')
bot.command('keyboard', async (ctx) => {
return ctx.reply('Keyboard:', Markup.keyboard([[t('hello')]]))
})