npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

botbuilder-translate

v0.0.4

Published

A piece of middleware to make bots multilingual

Downloads

2

Readme

translation-middleware

This is a TypeScript/Node that project demonstrates a piece of middleware that makes a bot multilingual. See Brandon's .NET translator for the C# version.

This project includes the middleware in translate-middleware.ts and a sample bot in app.ts. This project uses TypeScript - see Setup section for compilation instructions.

Terrible ascii pseudocode:

(message) -----> translate from user language to bot language ----->

(translated message) -----> check if message is trying to update user's languge.

If so, update language and intercept message ----X

Else, pass translated message along to the rest of your middleware and bot logic (translated message) ----->

bot logic -----> bot's responses ----->

([bot's responses in bot language]) -----> Then, translate bot's responses from bot language to user language ----->

([bot's messages in user's language]) -----> message to user (in user's language)

Usage

As with any SDKv4 middleware, you include the translator middleware with a bot.use declaration:

    .use(new Translator(process.env.MICROSOFT_TRANSLATOR_KEY, "en", getUserLanguage, setUserLanguage))

This middleware takes four arguments:

translatorKey

This middleware uses the Microsoft Translator API to translate incoming and outgoing messages. Follow linked instructions to get your key!

botLanguage

The language that your bot speaks. This is the language that the middleware will translate incoming messages into and outgoing messages out of. If your bot uses an NLP service like LUIS.ai, this should be the language (or one of the languages) that your model is trained on. Note that you must pass in the language code of the language you're using (e.g. "en" or "es") - see supported language codes here. Note, this middleware assumes that the user's language starts off as the bot's language. You can change this by adding custom logic in your getUserLanguage function.

getUserLanguage/setUserLanguage

Your bot needs to know where to get/set user language preferences. This middleware could have been opinionated by getting and setting this info in memory, but was built on the philosophy that developers should be able to choose how, where and when state should be stored. getUserLanguage and setUserLanguage are then developer-defined functions that get passed into this middleware

getUserLanguage

A function that takes the bot context and returns the user's language. In the example app.ts, those preferences are stored in the bot's state: context.state.user.translateTo.

setUserLanguage

A function that updates the user's language and returns a boolean promise. The promise resolves to true if the language was updated, ergo short circuiting the conversation (the message will not flow into the rest of the middleware or your bot logic).

In the example app.ts, this uses a pretrained LUIS model with a trained intent of "changeLanguage" and an entity of "language::toLanguage". It calls the model, and if the changeLanguage intent is triggered, the conversation the middleware will take over the conversation by returning Promise.resolve(true). If LUIS also found an entity of type "language::toLanguage", then it knows what language to update to and sets that language in the same place that getUserLanguage will pull it from. Note, the sample also has some helpers (isSupportedLanguage, getLanguageCode and setLanguage) that setUserLanguage uses to set language.

If the language is not changed and the intent "changeLanguage" is not triggered, then we want the message to fall through to the rest of our middleware and bot logic, so we return Promise.resolve(false).

Setup

Install packages

npm install

Add keys to your .env file

Open the .env file in your root directory. Add your Microsoft Translator API key, your Luis App Id and your Luis App Password.

If you're using the sample app.ts, you need to host a LUIS app with the "changeLanguage" intent and the "language::toLanguage" entity. Remember that you can define your setUserLanguage to change languages based on whatever trigger you like (An NLP model triggering a specific intent, a button being pressed, a regular expression firing, etc.).

The implementation I here is just one way of doing things :)

Compile Typescript

Compile:

tsc

or compile and watch:

tsc -w

These commands will create a lib directory with the js source code and associated sourcemaps (for debugging the TS code).

Run Sample

node lib/app.js