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 /menu command
All 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:
constenLang= { 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" } }}construLang= { user: { whatIsYourName:"Как тебя зовут?" }, hello:"Привет!",// not translated yet:// onlyOnEnglish: "Only on english" keyboards: { mainMenu: { buttons:"Бонус, Друзья", text:"меню"} bonusMenu: { buttons:"Получить бонус, Назад", text:"получи бонус"} }}// first language is default languageLibs.Lang.setup("en", enLang); // english is default nowLibs.Lang.setup("ru", ruLang);
Now default language is "english".
You can use lib now (for example, in command /test):
constlang=Libs.Lang;// send messages on default languageBot.sendMessage(lang.t("hello")) // Hello!Bot.sendMessage(lang.t("user.whatIsYourName")) // What is your name?Bot.sendMessage(lang.t("onlyOnEnglish")) // "Only on english"// Default language text always is used// for non translated keys yet// send keyboard for Main menu:Bot.sendKeyboard(lang.t("keyboards.mainMenu.buttons"),lang.t("keyboards.mainMenu.text"))// or send Bonus menu:Bot.sendKeyboard(lang.t("keyboards.bonusMenu.buttons"),lang.t("keyboards.bonusMenu.text"))
How to use
Change user lang
Libs.Lang.user.setLang("ru")
You can get user language code. For example in /start:
// in /start commandlet lang_code =request.from.language_code;Libs.Lang.user.setLang(lang_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.
// command /test// you can pass language code in paramslet lng = params;// or in options with Bot.run(command: "/test", options: {lngCode: "fr"} )let lng =options.lngCode// So now:// lng = "fr"Bot.sendMessage(Libs.Lang.t("hello", lng)) // Привет!
Get all language json
// get all json for default language:Bot.sendMessage(Libs.Lang.get())// get all json for "ru":Bot.sendMessage(Libs.Lang.get("ru"))
Using aliases
You can set aliases for language.
For /setup
constenLang= { aliases: {// Command /start have aliases:// home, dashboard and /main:"home, dashboard, /main":"/start",// command /otherCommand have aliases:// alias1, alias2"alias1,alias2":"/otherCommand" } user: { whatIsYourName:"What is your name?" },}Libs.Lang.setup("en", enLang);
// find multilanguage aliaseslet cmd =Libs.Lang.getCommandByAlias(message);if(cmd){ Bot.run({ command: cmd }) }// also it is possible define language// let cmd = Libs.Lang.getCommandByAlias(message, "ru");// if(cmd){ Bot.run({ command: cmd }) }
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:
let languages = ["en","es","cn","ru"];let cmdName;for(let i in languages){ cmdName ="lng-"+ languages[i].code;Bot.run({ command: cmdName })}Bot.sendMessage("Multi Languages - installed");