我在Dialogflow代理中配置了多种语言。我无法弄清楚如何在我的firebase函数中检测请求的语言,以便用正确的语言回答。有没有一种标准方法来处理这个问题?我在https://github.com/actions-on-google/actions-on-google-nodejs
中看不到任何检测语言的功能我希望能够做到这样的事情:
const app = new DialogflowApp({request: request, response: response});
if (app.getLang == 'en') {
\\ Do something in english
}
else if (app.getLang == 'es') {
\\ Do something in spanish
}
答案 0 :(得分:2)
AoG GitHub上有Number Genie的公开示例,包括法语和英语。
在此示例中,他们为英语和法语语言环境定义JSON objects:
{
"images": {
"cold": {
"url": "COLD.gif",
"altText": "cold genie",
"cardText": [
"Freezing like an ice cave in Antarctica?",
"I can't feel my face anymore",
"Hurry, before I turn into an icicle"
]
},
...
{
"images": {
"cold": {
"url": "COLD.gif",
"altText": "Génie froid",
"cardText": [
"Je me gèle comme un glaçon en Antartique",
"Je ne sens plus mon visage",
"Dépêchez-vous avant que je ne me transforme en glaçon"
]
},
...
然后有一个中心strings.js文件,它将为该语言环境提取正确的字符串。
const i18n = require("i18n");
i18n.configure({
"directory": __dirname + "/locales",
"objectNotation": true,
"fallbacks": {
"fr-FR": "fr",
"fr-CA": "fr"
}
});
const prompts = () => ({
"welcome": {
"visual": {
"elements": [
[i18n.__("variants.greeting"), i18n.__("variants.invocation")],
i18n.__("variants.invocationGuess"),
i18n.__("images.intro")
],
"suggestions": onlyNumberSuggestions
}
},
...
然后用于map to each intent:
[Actions.GENERATE_ANSWER] () {
this.data.answer = strings.getRandomNumber(strings.numbers.min,
strings.numbers.max); this.data.guessCount = 0; this.data.fallbackCount = 0; this.data.steamSoundCount = 0; this.ask(strings.prompts.welcome,strings.numbers.min,strings.numbers.max); }
通过从app.getUserLocale()
方法获取语言环境来设置语言环境:
/**
* Get the Dialogflow intent and handle it using the appropriate method
*/
run () {
strings.setLocale(this.app.getUserLocale());
/** @type {*} */
const map = this;
const action = this.app.getIntent();
console.log(action);
if (!action) {
return this.app.ask(`I didn't hear a number. What's your guess?`);
}
map[action]();
}
这里肯定有很多,你不需要以完全相同的方式做到这一点。 app.getUserLocale()
应该返回当前的区域设置,然后您可以以任何方式使用该区域设置来返回响应。