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 /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:
1
var enLang = {
2
user: { whatIsYourName: "What is your name?" },
3
hello: "Hello!",
4
onlyOnEnglish: "Only on english"
5
keyboards: {
6
mainMenu: { buttons: "Bonus, Friends", text: "menu" }
7
bonusMenu: { buttons: "Get bonus, Back", text: "Get your bonus now" }
8
}
9
}
10
11
var ruLang = {
12
user: { whatIsYourName: "Как тебя зовут?" },
13
hello: "Привет!",
14
// not translated yet:
15
// onlyOnEnglish: "Only on english"
16
keyboards: {
17
mainMenu: { buttons: "Бонус, Друзья", text: "меню"}
18
bonusMenu: { buttons: "Получить бонус, Назад", text: "получи бонус"}
19
}
20
}
21
22
// first language is default language
23
Libs.Lang.setup("en", enLang); // english is default now
24
Libs.Lang.setup("ru", ruLang);
Copied!
Now default language is "english".
You can use lib now (for example, in command /test):
1
var lang = Libs.Lang;
2
// send messages on default language
3
Bot.sendMessage(lang.t("hello")) // Hello!
4
Bot.sendMessage(lang.t("user.whatIsYourName")) // What is your name?
5
6
Bot.sendMessage(lang.t("onlyOnEnglish")) // "Only on english"
7
// Default language text always is used
8
// for non translated keys yet
9
10
11
// send keyboard for Main menu:
12
Bot.sendKeyboard(
13
lang.t("keyboards.mainMenu.buttons"),
14
lang.t("keyboards.mainMenu.text")
15
)
16
17
// or send Bonus menu:
18
Bot.sendKeyboard(
19
lang.t("keyboards.bonusMenu.buttons"),
20
lang.t("keyboards.bonusMenu.text")
21
)
Copied!

How to use

Change user lang

Libs.Lang.user.setLang("ru")
You can get user language code. For example in /start:
1
// in /start command
2
var lang_code = request.from.language_code;
3
Libs.Lang.user.setLang(lang_code)
Copied!

Get cur lang

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

Change default lang

1
Libs.Lang.default.setLang("en")
2
var default = Libs.Lang.default.getCurLang()
Copied!
Tips. Also you can use multi lang command. This is not recommended but sometimes a good solution
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.
1
// command /test
2
// you can pass language code in params
3
var lng = params;
4
// or in options with Bot.run(command: "/test", options: {lngCode: "fr"} )
5
var lng = options.lngCode
6
7
// So now:
8
// lng = "fr"
9
10
Bot.sendMessage(
11
Libs.Lang.t("hello", lng)
12
) // Привет!
Copied!

Get all language json

1
// get all json for default language:
2
Bot.sendMessage(Libs.Lang.get())
3
4
// get all json for "ru":
5
Bot.sendMessage(Libs.Lang.get("ru"))
Copied!

Using aliases

You can set aliases for language.
For /setup
1
var enLang = {
2
aliases: {
3
// Command /start have aliases:
4
// home, dashboard and /main:
5
"home, dashboard, /main": "/start",
6
7
// command /otherCommand have aliases:
8
// alias1, alias2
9
"alias1,alias2": "/otherCommand"
10
}
11
user: { whatIsYourName: "What is your name?" },
12
}
13
14
Libs.Lang.setup("en", enLang);
Copied!
1
// find multilanguage aliases
2
var cmd = Libs.Lang.getCommandByAlias(message);
3
if(cmd){ Bot.run({ command: cmd }) }
4
5
// also it is possible define language
6
// var cmd = Libs.Lang.getCommandByAlias(message, "ru");
7
// if(cmd){ Bot.run({ command: cmd }) }
Copied!

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:
1
var languages = ["en", "es", "cn", "ru"];
2
var cmdName;
3
4
for(var i in languages){
5
cmdName = "lng-" + languages[i].code;
6
Bot.run({ command: cmdName })
7
}
8
9
Bot.sendMessage("Multi Languages - installed");
Copied!

Make a simple command based structure

1
var enLang = {
2
command1: {
3
text: "your text",
4
// keys
5
...
6
}
7
...
8
}
9
command2: {
10
text: "your text"
11
// keys,
12
...
13
}
14
// bot menus
15
menus: {
16
mainMenu: {
17
keyboard:
18
text:
19
},
20
helpMenu: {
21
keyboad:
22
text:
23
}
24
},
25
// common links
26
links: {
27
homePage: "<a href='example.com'>Home page</a>",
28
},
29
alert: {
30
error: "sorry, we have error"
31
}
32
...
33
}
34
35
Libs.Lang.setup("en", enLang);
Copied!

Make translation for text and keyboard:

1
var enLang = {
2
command1: {
3
text: "Please confirm",
4
keyboard: "Yes, Cancel",
5
6
// you can also attach Inline keyboard:
7
inlineKeyboard: {
8
buttons: [
9
{title: "google", url: "http://google.com" },
10
{title: "other command", command: "/othercommand"}
11
],
12
text: "Please make a choice."
13
}
14
}
15
//...
16
}
17
18
Libs.Lang.setup("en", enLang);
Copied!
So in /command1 you can make:
1
Bot.sendKeyboard(
2
Libs.Lang.t("command1.keyboard"),
3
Libs.Lang.t("command1.text"),
4
)
5
6
// send inline keyboard:
7
Bot.sendInlineKeyboard(
8
Libs.Lang.t("command1.inlineKeyboard.buttons"),
9
Libs.Lang.t("command1.inlineKeyboard.text"),
10
)
Copied!

Use aliases translations

See this
Last modified 1yr ago