Lib for multi language support

Getting started

First - need to setup languages. For example with /setup command:

enLang = {
user: { whatIsYourName: "What is your name?" },
hello: "Hello!",
onlyOnEnglish: "Only on english"
ruLang = {
user: { whatIsYourName: "Как тебя зовут?" },
hello: "Привет!"
// not translated yet:
// onlyOnEnglish: "Only on english"
// first language is default language
Libs.Lang.setup("en", enLang);
Libs.Lang.setup("ru", ruLang);

Now default language is "english".

You can use lib now:

Bot.sendMessage(Libs.Lang.t("hello")) // Hello!
Bot.sendMessage(Libs.Lang.t("user.whatIsYourName")) // What is your name?
Bot.sendMessage(Libs.Lang.t("hello", "ru")) // Привет!
Bot.sendMessage(Libs.Lang.t("onlyOnEnglish")) // "Only on english"
// Default language text is used
// for non translated keys yet
Bot.sendMessage(Libs.Lang.t("onlyOnEnglish", "ru")) // "Only on english"

How to use

Change user lang


You can get user language code. For example in /start:

// in /start command
lang_code = request.from.language_code;

Get cur lang

var lang = Libs.Lang.user.getLang()

Change default lang

var default = Libs.Lang.default.getCurLang()

Tips. Also you can use multi lang command.

For example: /hello_en and /hello_ru

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 all language json

// get all json for default language:
// get all json for "ru":

Using aliases

You can set aliases for language.

For /setup

enLang = {
aliases: {
"home, dashboard, /main": "/start",
"alias1,alias2": "/otherCommand"
user: { whatIsYourName: "What is your name?" },
Libs.Lang.setup("en", enLang);

For Master command * :

// find multilanguage aliases
var cmd = Libs.Lang.getCommandByAlias(message);
if(cmd){{ command: cmd }) }
// also it is possible define language
// var cmd = Libs.Lang.getCommandByAlias(message, "ru");
// if(cmd){{ 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:

var languages = ["en", "es", "cn", "ru"];
for(var i in languages){
cmdName = "lng-" + languages[i].code;{ command: cmdName })
Bot.sendMessage("Multi Languages - installed");

Make a simple command based structure

enLang = {
command1: {
// keys
command2: {
// keys
Libs.Lang.setup("en", enLang);

Make translation for text and keyboard:

enLang = {
command1: {
text: "Please confirm",
keyboard: "Yes, Cancel"
Libs.Lang.setup("en", enLang);

So in /command1 you can make:


Use aliases translations

See this