Lang
Lib for multi language support
introduction
It is important to understand:
Any bot can be translated
The bot consists of commands in which there are no untranslated places.
All commands are multilingual
There are no commands like
/menuEn,/menuFr, /menuRu. There is only one/menucommandAll multilingual texts must be placed in each language files (command), for example, in lng-en, lng-ru. This will make them easier to translate.
everything can be translated: any text, answer, keyboard, inline keyboard, alert...
This is especially important for large bots
Getting started
First - need to setup languages. For example with /setup command:
const enLang = {
user: { whatIsYourName: "What is your name?" },
hello: "Hello!",
onlyOnEnglish: "Only on english"
keyboards: {
mainMenu: { buttons: "Bonus, Friends", text: "menu" }
bonusMenu: { buttons: "Get bonus, Back", text: "Get your bonus now" }
}
}
const ruLang = {
user: { whatIsYourName: "Как тебя зовут?" },
hello: "Привет!",
// not translated yet:
// onlyOnEnglish: "Only on english"
keyboards: {
mainMenu: { buttons: "Бонус, Друзья", text: "меню"}
bonusMenu: { buttons: "Получить бонус, Назад", text: "получи бонус"}
}
}
// first language is default language
Libs.Lang.setup("en", enLang); // english is default now
Libs.Lang.setup("ru", ruLang);Now default language is "english".
You can use lib now (for example, in command /test):
How to use
Change user lang
Libs.Lang.user.setLang("ru")
Get cur lang
let lang = Libs.Lang.user.getLang()
Change default lang
In BJS for command /hello:
Bot.runCommand("/hello_" + Libs.Lang.user.getLang())
Also you can use
Libs.Lang.t("translation-key", "ru")
for non user actions: in webhooks and etc
Get translation by language code
Sometimes we do not have user object. For example, on income webhooks or with Bot.runAll command and etc. Therefore, we cannot determine the current language.
The following code might be helpful in such cases.
Get all language json
Using aliases
You can set aliases for language.
For /setup
For Master command * :
Good practices
Use different language files (command) for each language.
For example "lng-en.js", "lng-fr.js" and etc. So it's easier to translate.
In /setup you can make something like this:
Make a simple command based structure
Make translation for text and keyboard:
So in /command1 you can make:
Use aliases translations
See this
Last updated
Was this helpful?