【问题标题】:How to disable localizer in Bot Framework如何在 Bot Framework 中禁用本地化程序
【发布时间】:2020-09-19 16:21:07
【问题描述】:

有没有办法完全禁用 Bot Framework 默认本地化程序?本地化程序似乎不一致地翻译提示并且在意想不到的地方。此外,我的机器人有时无法理解常见的用户输入(帮助、退出、返回、是、否),因为它似乎期望它们使用不同的语言。

我没有配置任何本地化设置,所以我猜测这种行为是由默认的 Bot Framework 本地化引起的。我正在寻找一种方法来完全避免任何翻译尝试并让我的机器人只使用英语。

【问题讨论】:

  • 正如这个答案中所建议的:How to localize Microsoft Bot Application?,这条线应该可以帮助你Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");。到目前为止,我还没有找到完全禁用定位器的方法。

标签: c# .net botframework


【解决方案1】:

查看文档中有关本地化的专门部分:https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-formflow-localize

bot 框架会自动使用消息中的语言环境来选择正确的资源,但您可以通过设置线程的 CurrentUICultureCurrentCulture 以及最好在 MessageActivity 中设置 Locale 属性来覆盖此信息

CultureInfo lang = ...;
Thread.CurrentThread.CurrentCulture = lang;
Thread.CurrentThread.CurrentUICulture = lang;
context.Activity.AsMessageActivity().Locale = lang.ToString();

不要忘记为每个将发送消息的线程设置它,因为没有切换语言的全局解决方案。

如果你想更深入,你可以看看 bot 框架源代码:

编辑: 对于提示部分,如果我没记错的话,我必须创建自己的 public abstract class MyPrompt<T, U> : IDialog<T> 并在其中:

protected virtual IMessageActivity MakePrompt(IDialogContext context, string prompt, IReadOnlyList<U> options = null, IReadOnlyList<string> descriptions = null, string speak = null)
{
    var msg = context.MakeMessage();

    // force Culture
    CultureInfo lang = ...;
    if (lang != null)
    {
        Thread.CurrentThread.CurrentCulture = lang;
        Thread.CurrentThread.CurrentUICulture = lang;
        context.Activity.AsMessageActivity().Locale = lang.ToString();
    }

    if (options != null && options.Count > 0)
    {
        promptOptions.PromptStyler.Apply(ref msg, prompt, options, descriptions, speak);
    }
    else
    {
        promptOptions.PromptStyler.Apply(ref msg, prompt, speak);
    }
    return msg;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    相关资源
    最近更新 更多