5.0.0 • Published 2 years ago

telegraf-i18next v5.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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')]]))
})
5.0.0

2 years ago

4.0.0

2 years ago

3.0.0

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago