Weili Gao
Weili Gao

Reputation: 207

How to implement localization with dialogs exported as libraries

I have packaged some of my dialogs as libraries by following the instructions from the basics-libraries example. Before, I had all dialogs in app.js and I used localization following the instructions from the documentation. For example, I used

var chatBot = new builder.UniversalBot(chatConnector, {
  localizerSettings: { 
    botLocalePath: "./locale", 
    defaultLocale: "de"
  }
});

and

session.preferredLocale("de");

to use German for all the prompt labels, and stored all the German translations in ./locale/de/index.json and ./locale/de/BotBuilder.json.

However, the localization doesn't work with the dialogs I have packaged into libraries. Instead of the localized strings only the message IDs are displayed.

It works if I use session.localizer.gettext(session.preferredLocale(), "message ID") on each string. However, that is very tedious, and I am wondering if there is a way to localize all strings inside a library at once.

Upvotes: 0

Views: 318

Answers (1)

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

In order to this to work, your locale file should have the same name as your library.

./my_bot_library.js
./locale/en/my_bot_library.json

Then, to get your localized text you can either use:

session.localizer.gettext(session.preferredLocale(), 'message ID', 'my_bot_library')

Please note that the the third parameter is the namespace that represents the library name

or just the following which internally resolves the preferred Locale and current namespace/library name

session.gettext('message_id') or session.send('message_id')

Upvotes: 1

Related Questions