botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

PromptDialog.PromptConfirm localization issue

Open Taras-Tyrsa opened this issue 9 years ago • 8 comments

BotBuilder v.3.3.3.0

Microsoft.Bot.Builder.Dialogs.PromptDialog.PromptConfirm class has static initialization for Options and Patterns, which makes impossible to change language once initialized:

public sealed class PromptConfirm : Prompt<bool, string>
{
...
    /// <summary>
    /// The yes, no options for confirmation prompt
    /// </summary>
    public static string[] Options { set; get; } = { Resources.MatchYes.SplitList().First(), Resources.MatchNo.SplitList().First() };

    /// <summary>
    /// The patterns for matching yes/no responses in the confirmation prompt.
    /// </summary>
    public static string[][] Patterns { get; set; } = { Resources.MatchYes.SplitList(), Resources.MatchNo.SplitList() };

It will take current culture during first usage and won't change it afterwards:

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");

var options = PromptDialog.PromptConfirm.Options;
// options = [ 'Ja', 'Nein' ]

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");

options = PromptDialog.PromptConfirm.Options;
// EXPECTED: options = [ 'Yes', 'No' ]
// ACTUAL: options = [ 'Ja', 'Nein' ]

Taras-Tyrsa avatar Dec 07 '16 12:12 Taras-Tyrsa

Just a little remark: manual set of Options and Patterns can lead to race conditions when two parallel requests use it (e.g. one request sets English translations and another one - German).

Taras-Tyrsa avatar Dec 11 '16 09:12 Taras-Tyrsa

@hailiu2586 This is an issue with C# version of SDK.

Stevenic avatar Dec 12 '16 08:12 Stevenic

@Taras-Tyrsa those are static property getters (not static fields), so that code executes every time the "method" is called. Are they being cached elsewhere?

willportnoy avatar Dec 29 '16 18:12 willportnoy

public static string[] Options { set; get; } = { Resources.MatchYes.SplitList().First(), Resources.MatchNo.SplitList().First() };

is not the same as

public static string[] Options {
    get { return new [] { Resources.MatchYes.SplitList().First(), Resources.MatchNo.SplitList().First() }; }
};

In the second case code executes each time we use property. But in the first one, it executes only once, and that's what we have in Bot Framework sources.

Taras-Tyrsa avatar Dec 30 '16 08:12 Taras-Tyrsa

@Taras-Tyrsa yes, you're correct - we should use the more dynamic form. @msft-shahins should we remove the setter too since you can pass these as part of PromptOptions?

willportnoy avatar Dec 30 '16 17:12 willportnoy

@willportnoy , there is one problem however. Once you remove the setter there won't be any possibility to customize the expected options, am I right?

For instance, the prompt is used by the LocationDialog and I want to be in control of the options while the Prompt is used by the LocationDialog. As LocationDialog does not expose any API to customize the options, the only possibility I am left with is to use the setter you want to remove. However I admit this possibility is lame. Why? Because of the already mentioned race conditions - static values simply do not work for multiple parallel conversations in different locals, right?

What I think we really need is an option to inject a resource manager (or something like that) which allows us to customize the options per culture/locale.

I had only very quick look at the source codes of the PromptDialog so please let me know if I misunderstood something. Thank you in advance.

anmarcek avatar Mar 16 '17 16:03 anmarcek

@anmarcek I think you're correct. One other option is to customize the resource files, so that when the more dynamic static getter is used to look up resources by the thread ambient culture, it will get your updated resource files.

willportnoy avatar Apr 03 '17 00:04 willportnoy

https://github.com/Microsoft/BotBuilder/commit/b94957f1e84af1129b81c928e6583e410c38148f should fix this issue.

msft-shahins avatar Apr 10 '17 18:04 msft-shahins