在Microsoft Bot框架中使用自适应卡支持多语言功能

时间:2019-01-22 06:03:39

标签: bots

我正在测试从Microsoft机器人框架下载的Multilingual Bot。这样做时,我的某些内容没有得到翻译。

Image link

您可以在下面的代码中看到几行,询问用户是否有什么我可以帮助的内容?它将翻译成用户选择的语言。但是,CardAction()对象标题“是”和“否”中的内容未得到翻译。

如何在中间件中处理此类翻译?

bool translate = userLanguage != TranslationSettings.DefaultLanguage;
if (IsLanguageChangeRequested(turnContext.Activity.Text))
{
    await _accessors.LanguagePreference.SetAsync(turnContext, turnContext.Activity.Text);
    var reply = turnContext.Activity.CreateReply($"Your current language code is: {turnContext.Activity.Text}");
    await turnContext.SendActivityAsync(reply, cancellationToken);
    await _accessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);

    // This content is getting partially translated.
    var newRply = turnContext.Activity.CreateReply("Is there anything else I can help you with?"); 
    newRply.SuggestedActions = new SuggestedActions()
    {
        Actions = new List<CardAction>()
        {
        // The title is not getting translated
        new CardAction() { Title = "Yes", Type = ActionTypes.PostBack, Value = Spanish },
        // The title is not getting translated
        new CardAction() { Title = "No", Type = ActionTypes.PostBack, Value = English },
        },
    };
    await turnContext.SendActivityAsync(newRply);
}
    else
{
    var reply = turnContext.Activity.CreateReply("Choose your language:");
    reply.SuggestedActions = new SuggestedActions()
    {
        Actions = new List<CardAction>()
        {
            new CardAction() { Title = "Español", Type = ActionTypes.PostBack, Value = Spanish },
            new CardAction() { Title = "English", Type = ActionTypes.PostBack, Value = English },
        },
    };

    await turnContext.SendActivityAsync(reply);
    }
}

还应将CardAction()中的字符串转换为用户选择的语言。

1 个答案:

答案 0 :(得分:0)

我假设您正在使用示例随附的Microsoft Translator类。在同一示例中,我通过继承CardAction类实现了一个新类(MultilingualCardAction)。

这对我有用,但也许还有更好的方法。

public class MultilingualCardAction : CardAction
{
private readonly MicrosoftTranslator _translator;

    private string _language;
    public MultilingualCardAction(string language)
    {
        _language = language;
        _translator = new MicrosoftTranslator(<<YOUR TRANSLATION KEY>>);
    }

    public string cardTitle
    {
        get
        {
            return this.Title;
        }

        set
        {
            this.Title = getTranslatedText(value).Result;
        }
    }
    async Task<string> getTranslatedText(string title)
    {
        return await _translator.TranslateAsync(title, _language);
    }
}

然后我以这种方式创建CardAction对象。

var newRply = turnContext.Activity.CreateReply("Is there anything else I can help you with?");
newRply.SuggestedActions = new SuggestedActions()
{
     Actions = new List<CardAction>()
     {
          new MultilingualCardAction('es') { cardTitle = "Yes", Type = ActionTypes.PostBack, Value = "Yes" },
          new MultilingualCardAction('es') { cardTitle = "No, thanks!", Type = ActionTypes.PostBack, Value = "No" },
      },
 };
 await turnContext.SendActivityAsync(newRply);

请参考下图。

image

相关问题