I18n: multilanguage
// index.ts
import { I18n, Session } from 'degreet-telegram'
import { IContext, IMessage, I18nSession } from 'degreet-telegram/src/types'
const i18n: I18n = new I18n(
path.resolve(__dirname, 'locales'), // path to locales directory
['en'] // locales, must contains with file names (e.g. en - en.yaml)
)
interface ISession {
i18n: I18nSession
}
new Keyboard('under_the_message', true) // use i18n for keyboard layouts
.btn('cb', 'go_home_btn', 'home').row() // pass yaml text id as text, it will be replaced when you use layout
.saveLayout('home_btn')
bot.use(new Session<ISession>().middleware()) // required for using i18n
bot.use(i18n.middleware())
bot.on('text', I18n.listen('go_home_btn'), console.log) // use for listen i18n text, e.g. when you use under_the_chat keyboard
bot.command('start', async (ctx: IContext): Promise<any> => {
try {
await ctx.answer.send(
ctx.i18n?.get('hello', { name: 'Author' }),
new Keyboard('under_the_message').useLayout('home_btn')
)
} catch (e: any) {
console.error(e)
}
})
// locales/en.yaml
hello: >
Hello, ${name}!
go_home_btn: Go home
// index.js
const { I18n, Session } = require('degreet-telegram')
const i18n = new I18n(
path.resolve(__dirname, 'locales'), // path to locales directory
['en'] // locales, must contains with file names (e.g. en - en.yaml)
)
new Keyboard('under_the_message', true) // use i18n for keyboard layouts
.btn('cb', 'go_home_btn', 'home').row() // pass yaml text id as text, it will be replaced when you use layout
.saveLayout('home_btn')
bot.use(new Session().middleware()) // required for using i18n
bot.use(i18n.middleware())
bot.on('text', I18n.listen('go_home_btn'), console.log) // use for listen i18n text, e.g. when you use under_the_chat keyboard
bot.command('start', async (ctx) => {
try {
await ctx.answer.send(
ctx.i18n?.get('hello', { name: 'Author' }),
new Keyboard('under_the_message').useLayout('home_btn')
)
} catch (e) {
console.error(e)
}
})
// locales/en.yaml
hello: >
Hello, ${name}!
go_home_btn: Go home
Last updated