forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator-middleware.js
More file actions
92 lines (80 loc) · 3.68 KB
/
Copy pathtranslator-middleware.js
File metadata and controls
92 lines (80 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const fetch = require('node-fetch');
const ENGLISH_LANGUAGE = 'en';
const SPANISH_LANGUAGE = 'es';
const DEFAULT_LANGUAGE = ENGLISH_LANGUAGE;
class TranslatorMiddleware {
/**
* Creates a translation middleware.
* @param {BotStatePropertyAccessor} languagePreferenceProperty Accessor for language preference property in the user state.
* @param {string} translatorKey Microsoft Text Translation API key.
*/
constructor(languagePreferenceProperty, translatorKey) {
this.languagePreferenceProperty = languagePreferenceProperty;
this.translatorKey = translatorKey;
}
/**
* Called via BotAdapter.runMiddleware(), this method adds handlers to the TurnContext's sendActivities method to
* translate the outgoing text.
* @param {TurnContext} turnContext A TurnContext instance containing all the data needed for processing this conversation turn.
* @param {Function} next The next middleware or business logic of the bot to run.
*/
async onTurn(turnContext, next) {
if (turnContext.activity.type === 'message') {
const userLanguage = await this.languagePreferenceProperty.get(turnContext, DEFAULT_LANGUAGE);
const shouldTranslate = userLanguage !== DEFAULT_LANGUAGE;
if (shouldTranslate) {
turnContext.activity.text = await this.translate(turnContext.activity.text, DEFAULT_LANGUAGE);
}
turnContext.onSendActivities(async (context, activities, next) => {
// Translate messages sent to the user to user language
const userLanguage = await this.languagePreferenceProperty.get(turnContext, DEFAULT_LANGUAGE);
const shouldTranslate = userLanguage !== DEFAULT_LANGUAGE;
if (shouldTranslate) {
for (const activity of activities) {
activity.text = await this.translate(activity.text, userLanguage);
}
}
await next();
});
}
await next();
}
/**
* Helper method to translate text to a specified language.
* @param {string} text Text that will be translated
* @param {string} to Two character langauge code, e.g. "en", "es"
*/
async translate(text, to) {
// Check to make sure "en" is not translated to "in", or "es" to "it"
// In a production bot scenario, this would be replaced for a method call that detects
// language names in utterances.
if (text.toLowerCase() === ENGLISH_LANGUAGE || text.toLowerCase() === SPANISH_LANGUAGE) {
return text;
}
// From Microsoft Text Translator API docs;
// https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-nodejs-translate
const host = 'https://api.cognitive.microsofttranslator.com';
const path = '/translate?api-version=3.0';
const params = '&to=';
const url = host + path + params + to;
return fetch(url, {
method: 'post',
body: JSON.stringify([{ 'Text': text }]),
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': this.translatorKey
}
})
.then(res => res.json())
.then(jsonResponse => {
if (jsonResponse && jsonResponse.length > 0) {
return jsonResponse[0].translations[0].text;
} else {
return text;
}
});
}
}
module.exports.TranslatorMiddleware = TranslatorMiddleware;