在DialogFlow漫游器中更改日/月语言

时间:2019-05-06 19:43:04

标签: dialogflow chatbot

我正在使用DialogFlow为一家意大利餐厅创建助手。

我已将语言设置为西班牙语,并且一切似乎都很好,但是当我显示预订的最后日期时,会以英语显示(在附图中为星期五和五月)。

是否可以更改? enter image description here

这是生成上述对表预订过程的特定响应的代码:

function createBooking(agent) {
    let guests = agent.parameters.comensales;
    let time = new Date(agent.parameters.time);
    let date = new Date(agent.parameters.date);
    let bookingDate = new Date(date);
    var numeroReserva = Math.random().toString(16).slice(2, 8).toUpperCase();
    bookingDate.setHours(time.getHours());
    bookingDate.setMinutes(time.getMinutes());
    let now = new Date();

    if (guests < 1){
        agent.add('You need to reserve a table for at least one person. Please try again!');
    } else if (bookingDate < now){
        agent.add(`No puedes reservar una fecha pasada. Por favor, inténtalo de nuevo!`);
    } else if (bookingDate.getFullYear() > now.getFullYear()) {
        agent.add(`No puedes hacer una reserva para ${bookingDate.getFullYear()} todavía. Por favor, elige una fecha en ${now.getFullYear()}.`);
    } else {
        let timezone = parseInt(agent.parameters.time.toString().slice(19,22));
        bookingDate.setHours(bookingDate.getHours() + timezone);
        agent.add(`Perfecto. He reservado una mesa para ${guests} el ${bookingDate.toString().slice(0,21)}`);
        agent.add(`Tu código de reserva es: ${numeroReserva}`);
        agent.add('Nos vemos pronto!');
        agent.add('Buon appetito!');
    }
  }

3 个答案:

答案 0 :(得分:1)

运行实现的代码在Google的计算基础架构中运行,该基础架构的默认语言环境/语言为美国英语。当一个请求从Dialog流到达完全实现时,该请求将带有我们将用来响应的语言。请参见Webhook Request JSON中的languageCode。当我们在Node.js中使用API​​时,看起来该数据在agent.locale属性中可用。

在JavaScript Date对象上,我们似乎有一个名为toLocaleString()的方法,该方法将日期/时间转换为字符串,但另外提供了用于创建的语言(语言环境)语言的特定内容和格式。如果将所有这些放在一起,我们可能会发现以下代码行可能有效:

agent.add(`Perfecto. He reservado una mesa para ${guests} el ${bookingDate.toLocalString(agent.locale).slice(0,21)}`);

这可能需要一些测试才能正确。我将首先记录agent.locale作为测试,以确保它具有我们期望/希望的值。

答案 1 :(得分:1)

已经晚了,但是对于那些面对这个问题的人来说。您知道您可以这样做:

let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', 
                hour12: false, hour: 'numeric', minute: 'numeric' };
var curr_date = new Date();
agent.add(`Sono le ` + curr_date.toLocaleString('it-IT', options));

答案 2 :(得分:1)

如果有人喜欢使用moment.js

您可以从实现内联编辑器将库添加到项目中。 转到“实现”,单击“ package.json”

在依赖项中添加以下行

"moment": "^2.24.0",
"moment-timezone": "^0.5.31"

然后返回index.js并添加以下行以导入它

const moment = require('moment-timezone');

然后您可以在函数中像这样处理

if(agent.locale === 'en'){
         moment.locale('en-US');
         agent.add(`Now is ` + moment().tz(your_time_zone).format('LLLL'));
       }
       else if(agent.locale === 'it-IT' || agent.locale === 'it'){
         moment.locale('it');
         agent.add(`Sono le ` + moment().tz(your_time_zone).format('LLLL'));
       }

回应样本为“ Sono lemartedì2020 ottobre 2020 15:30” 或“现在是2020年10月20日,星期二,下午3:31”