Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support i18n via 'text' functions #1747

Merged
merged 3 commits into from
Oct 28, 2019
Merged

Conversation

cooperka
Copy link
Contributor

@cooperka cooperka commented Jul 27, 2019

Motivation

Our team needed per-user internationalization, so I went with a quick and easy solution that works for us. If this deserves more thought before being integrated into the official codebase, I'm happy to wait on the PR and help do it "right". Just let me know what you need.

Related issues: #41, #947

Changes

Adds support for text as a function, in addition to the existing string or array options, e.g.

// Simple string:
convo.say('Hello');

// Randomly picks one possibility from the array:
convo.say({ text: ['Hi', 'Hello'] });

// New function-based text:
convo.say({ text: (vars) => vars.isMorning ? 'Good morning' : 'Good evening' });

Example

Code snippets below for a multilingual bot using the polyglot lib for translations (though other libs like i18next would work just as easily).

First set the user's locale on every message via middleware (perhaps you've saved their preference in your database):

function setUserLocale(bot, message, next) {
  const locale = determineUserLocale(message.user);
  bot.getConfig('activity').userDeclaredLocale = locale;
  next();
}

controller.middleware.ingest.use(setUserLocale);

Then save the locale as a conversation variable at the beginning of the conversation:

const convo = new BotkitConversation('myConvo', controller);

convo.before('default', (convo, bot) => {
  convo.setVar('locale', bot.getConfig('activity').userDeclaredLocale);
});

Finally, use whatever method you want to in order to translate each outgoing message:

convo.say({ text: (vars) => i18n[vars.locale].t(text) });

It does indeed work with our fork -- just modify your package.json dependency to:

"botkit": "sassafrastech/botkit#2019-07-28-textFn-v4.5.0-2"

Note: Full docs/testing not yet implemented, since there will probably be some discussion before accepting this PR. Again, I'm happy to make changes to allow wider use of this feature.

@cooperka cooperka changed the title Support i18n via text transform hook Support i18n via 'text' functions Jul 28, 2019
@benbrown benbrown self-requested a review July 29, 2019 16:09
@benbrown
Copy link
Contributor

Interesting solution, well documented!

I'll have to ponder this a bit to make sure it doesn't break the world.

@smoyte
Copy link

smoyte commented Sep 13, 2019

@benbrown Any updates here? Any support we can provide? This is going to be a core part of our use of botkit so if it's going to "break the world" we'll need to know soon :D

I see another person opened a similar PR: #1799

Hopefully we can figure something out...

@benbrown
Copy link
Contributor

I am planning on including this in the next release!

@benbrown benbrown self-assigned this Sep 13, 2019
@smoyte
Copy link

smoyte commented Sep 13, 2019

Wow, great!

@benbrown benbrown added this to the 4.6 milestone Sep 13, 2019
@benbrown benbrown merged commit 8504e69 into howdyai:master Oct 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants