npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

telegraf-i18next

v5.0.0

Published

i18next Localization for telegraf.js.

Downloads

107

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