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...

triangle-exclamation

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")

circle-info

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

Get cur lang

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

Change default lang

circle-info

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())

circle-check

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 commandarrow-up-right * :

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 thisarrow-up-right

Last updated

Was this helpful?