如何使用 Google Actions 和 DiaglogflowApp with Firebase 函数处理不同的语言

How to handle different languages with Google Actions and DiaglogflowApp with Firebase functions

我在 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 
}

Number Genie 的 AoG GitHub 上有一个 public 样本,它有法语和英语两种版本。

在此示例中,他们为英语和法语语言环境定义了 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() 应该 return 当前语言环境,然后您可以以任何您想要的方式使用它来 return 响应。