1

At the start I'm asking the user to choose his/her preferred language and then saving it in a database. On every dialog I'm switching the saved value with an if-else. It's a big project and I plan to do this with every dialog.

My question is: Is this optimal or is there a better way to do this?

  private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        var userstate = await (stepContext.Context.TurnState["BasicAccessors"] as BasicAccessors).BasicUserStateAccessor.GetAsync(stepContext.Context);

        if (userstate.IsLanguageTagalog)
        {
            await stepContext.Context.SendActivityAsync(
                MessageFactory.Text(
                    $"Kumusta {userstate.FirstName}."), cancellationToken);
        }
        else
        {
            await stepContext.Context.SendActivityAsync(
                MessageFactory.Text(
                    $"Hi how are you {userstate.FirstName}."), cancellationToken);
        }

        return await stepContext.EndDialogAsync();
    }
user10860402
  • 912
  • 1
  • 10
  • 34

2 Answers2

1

You could use resource files as suggested by xdt if there are only a limited number of strings that you want multi-lingual support for e.g Hello, How can I help you, Goodbye etc. This is the approach taken by the Virtual Assistant Template and it works well. See how the OnBoardingResponses class is used to look up the appropriate response using the locale, then easily accessed within your bot code, the locale is currently set within the adapter code but this could be moved elsewhere.

An alternative is to have translation on the fly using middleware and Microsoft Translator, this sample multi-lingual bot shows how the middleware can be implemented and includes instructions on getting everything setup and deployed.

Matt Stannett
  • 2,700
  • 1
  • 15
  • 36
0

I'm not sure this fully answers your question, but I'll still try.

A bot runs on a web app, which is basically an ASP.NET MVC Web API application. The common practice in such applications is to change the locale of the request thread right at the start, such as at the Request_Start event in Global.asax.cs or equivalent, to the locale requested by the client. Then, you store your strings in resource files named by the locale, such as Resources.tlPH.resx and Resources.resx as general fallback, and pull the string from there by key. The ASP.NET runtime will pull the string from the right file based on the current thread culture.

That's the direction you should take. You should research more about MVC and localization to work out the details.

Tsahi Asher
  • 1,767
  • 15
  • 28
  • I am using botframework v4 and i don't have those files you mention. Should i manually create it? I will still do a little research and wait on more answer. Thank you – user10860402 Jul 04 '19 at 11:27
  • Yes, you should add them by yourself. The project properties has a Resources tab, but in my experience it's better to add a folder to the project and add resources there from the Add New Item dialog. See also the links in the comments to your question. – Tsahi Asher Jul 04 '19 at 12:49
  • What if i need some data in the middle of the string? like "Hi {user} how are you?" how can i insert the data in the middle of the string in the .resx file? – user10860402 Jul 04 '19 at 22:25
  • 1
    @user10860402 add a placeholder like {0}, then when you retrieve the string use string.Format(stringWithUserPlaceholder, userName). The documentation is available [here](https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8#System_String_Format_System_String_System_Object_). – Matt Stannett Jul 06 '19 at 01:57