diff --git a/Dialogs/LanguageConst.cs b/Dialogs/LanguageConst.cs new file mode 100644 index 0000000..6ebaa2e --- /dev/null +++ b/Dialogs/LanguageConst.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Web; + +namespace EmergencyServicesBot.Dialogs +{ + internal static class LanguageConst + { + //TODO change this to have dictionary with language info object (culture + id + display name) + + //Set the language to be used; you can change this on-demand to change the langauage across the app + //You will pass this everytime you request a value from the resx file + internal static readonly CultureInfo ciEnglish = new CultureInfo("en-US"); + internal static readonly CultureInfo ciSpanish = new CultureInfo("es-US"); + internal static readonly CultureInfo ciChinese = new CultureInfo("zh-CN"); + internal static readonly CultureInfo ciFrench = new CultureInfo("fr-FR"); + + internal const string enLanguageId = "en"; + internal const string esLanguageId = "es"; + internal const string frLanguageId = "fr"; + internal const string zhLanguageId = "zh-CN"; + + internal const string enLanguageName = "English"; + internal const string esLanguageName = "Español"; + internal const string zhLanguageName = "中文"; + internal const string frLanguageName = "Français"; + + internal static readonly string[] languages = new[] { enLanguageName, esLanguageName, zhLanguageName, frLanguageName }; + internal static readonly string[] smsLanguages = new[] { $"1 -{enLanguageName}", $"2 -{esLanguageName}", $"3 -{zhLanguageName}", $"4 -{frLanguageName}" }; + } +} \ No newline at end of file diff --git a/Dialogs/QandADialog.cs b/Dialogs/QandADialog.cs index 75f13c9..a2eb7ab 100644 --- a/Dialogs/QandADialog.cs +++ b/Dialogs/QandADialog.cs @@ -161,10 +161,12 @@ static async Task GetAuthenticationToken(string key) } } + //TODO change to use resources private bool IsDoneCommand(string commandText) => commandText.Equals(@"done", StringComparison.OrdinalIgnoreCase) || commandText.StartsWith(@"no", StringComparison.OrdinalIgnoreCase) || commandText.Equals(@"exit", StringComparison.OrdinalIgnoreCase) + || commandText.Equals(@"quitter", StringComparison.OrdinalIgnoreCase) || commandText.Equals(@"quit", StringComparison.OrdinalIgnoreCase); } } \ No newline at end of file diff --git a/Dialogs/RootDialog.cs b/Dialogs/RootDialog.cs index f69396d..45c583e 100644 --- a/Dialogs/RootDialog.cs +++ b/Dialogs/RootDialog.cs @@ -14,6 +14,7 @@ using System.Configuration; using System.Net.Http; using System.Web; +using System.Diagnostics; namespace EmergencyServicesBot.Dialogs { @@ -22,11 +23,7 @@ public class RootDialog : IDialog { static ResourceManager translateDialog = new ResourceManager("EmergencyServicesBot.Resources.Resources", Assembly.GetExecutingAssembly()); - //Set the language to be used; you can change this on-demand to change the langauage across the app - //You will pass this everytime you request a value from the resx file - static CultureInfo ciEnglish = new CultureInfo("en-US"); - static CultureInfo ciSpanish = new CultureInfo("es-US"); - static CultureInfo ciChinese = new CultureInfo("zh-CN"); + private const string userDataCultureKey = @"cultureInfo"; public async Task StartAsync(IDialogContext context) { @@ -62,7 +59,7 @@ private async Task MessageReceivedAsync(IDialogContext context, IAwaitable("userLanguage") == "en") - { - context.UserData.SetValue(@"cultureInfo", ciEnglish); - } - else if (context.UserData.GetValue("userLanguage") == "es") - { - context.UserData.SetValue(@"cultureInfo", ciSpanish); - } - else if (context.UserData.GetValue("userLanguage") == "zh-CN") - { - context.UserData.SetValue(@"cultureInfo", ciChinese); - } - else { context.UserData.SetValue(@"cultureInfo", ciEnglish); } - + context.UserData.SetValue(userDataCultureKey, userCulture); + } + } + + private CultureInfo GetCultureInfoFromLanguageId(string languageId) + { + if (string.IsNullOrWhiteSpace(languageId)) + { + return LanguageConst.ciEnglish; + } + + CultureInfo userCulture = LanguageConst.ciEnglish; + + switch (languageId) + { + case LanguageConst.esLanguageId: + userCulture = LanguageConst.ciSpanish; + break; + case LanguageConst.zhLanguageId: + userCulture = LanguageConst.ciChinese; + break; + case LanguageConst.frLanguageId: + userCulture = LanguageConst.ciFrench; + break; + case LanguageConst.enLanguageId: + default: + userCulture = LanguageConst.ciEnglish; + break; + } + + return userCulture; } private async Task UserChoiceMade(IDialogContext context, IAwaitable result) { var choice = await result; + //TODO change with resource entry directly if ((choice.IndexOf(@"get answers", 0, StringComparison.OrdinalIgnoreCase) != -1) || (choice.IndexOf(@"Obtener Respuestas", 0, StringComparison.OrdinalIgnoreCase) != -1) || + (choice.IndexOf(@"Obtenir les réponses", 0, StringComparison.OrdinalIgnoreCase) != -1) || (choice.IndexOf(@"其他问题", 0, StringComparison.OrdinalIgnoreCase) != -1)) context.Call(new QandADialog(), DoneWithSubdialog); else if ((choice.IndexOf(@"Select language", 0, StringComparison.OrdinalIgnoreCase) != -1) || (choice.IndexOf(@"Seleccione el idioma", 0, StringComparison.OrdinalIgnoreCase) != -1) || + (choice.IndexOf(@"Sélectionner la langue", 0, StringComparison.OrdinalIgnoreCase) != -1) || (choice.IndexOf(@"选择语言", 0, StringComparison.OrdinalIgnoreCase) != -1)) context.Call(new SetLanguage(), DoneWithSubdialog); } @@ -165,26 +176,26 @@ public static async Task SendWelcomeMessage(IMessageActivity activity) ConnectorClient client = new ConnectorClient(new Uri(activity.ServiceUrl)); - var title = translateDialog.GetString("WelcomeTitle", ciEnglish); - + var title = translateDialog.GetString("WelcomeTitle", LanguageConst.ciEnglish); + IList cardsAttachment = new List(); var reply = ((Activity)activity).CreateReply(); - + CardImage CI = new CardImage { - Url = translateDialog.GetString("WelcomeImageUrl", ciEnglish), + Url = translateDialog.GetString("WelcomeImageUrl", LanguageConst.ciEnglish), }; - + //TODO change by adding resources instead of hardcoded text var heroCard = new HeroCard { Title = title, - Subtitle = "Hello. Hola. 你好.", - Text = "Say \"hi\" to begin, diga \"hola\" para comenzar, 说“嗨”开始", + Subtitle = "Hello. Hola. 你好. Bonjour.", + Text = "Say \"hi\" to begin, diga \"hola\" para comenzar, 说“嗨”开始, dites \"Bonjour\" pour commencer", Images = new List { CI } }; - + cardsAttachment.Add(heroCard.ToAttachment()); reply.Attachments = cardsAttachment; @@ -203,6 +214,6 @@ public class BasicQnAMakerDialog : QnAMakerDialog // Required: subscriptionKey, knowledgebaseId, // Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0] public BasicQnAMakerDialog() : base(new QnAMakerService(new QnAMakerAttribute(Utils.GetAppSetting("QnASubscriptionKey"), Utils.GetAppSetting("QnAKnowledgebaseId"), "I could not find an answer to your question. Please try again or contact Houston 311.", 0.5))) - {} + { } } } \ No newline at end of file diff --git a/Dialogs/SetLanguage.cs b/Dialogs/SetLanguage.cs index 1e3b00d..bbaf02a 100644 --- a/Dialogs/SetLanguage.cs +++ b/Dialogs/SetLanguage.cs @@ -17,24 +17,15 @@ public class SetLanguage : IDialog //Set the language to be used; you can change this on-demand to change the langauage across the app //You will pass this everytime you request a value from the resx file - static CultureInfo ciEnglish = new CultureInfo("en-US"); - static CultureInfo ciSpanish = new CultureInfo("es-US"); - static CultureInfo ciChinese = new CultureInfo("zh-CN"); + public async Task StartAsync(IDialogContext context) { - // offer user languages currently supported - string[] choices = new[] { @"English", @"Español", @"中文" }; - if (context.Activity.ChannelId == ChannelIds.Sms) - { // on SMS, communicate they can choose by replying with "1" or "2" - choices = new[] { @"1 - English", @"2 - Español", @"3 - 中文" }; - } - PromptDialog.Choice( - context, - this.onLanguageSelect, - choices, - translateDialog.GetString("SetLanguage", context.UserData.GetValue("cultureInfo"))); + context, + this.onLanguageSelect, + context.Activity.ChannelId == ChannelIds.Sms ? LanguageConst.smsLanguages : LanguageConst.languages, + translateDialog.GetString("SetLanguage", context.UserData.GetValue("cultureInfo"))); } private async Task onLanguageSelect(IDialogContext context, IAwaitable result) @@ -44,29 +35,35 @@ private async Task onLanguageSelect(IDialogContext context, IAwaitable r var choice = await result; CultureInfo culture; - if (choice.IndexOf(@"english", 0, StringComparison.OrdinalIgnoreCase) != -1) + if (choice.IndexOf(LanguageConst.enLanguageName, 0, StringComparison.OrdinalIgnoreCase) != -1) + { + choice = LanguageConst.enLanguageId; + selectedLanguage = LanguageConst.enLanguageName; + culture = LanguageConst.ciEnglish; + } + else if (choice.IndexOf(LanguageConst.esLanguageName, 0, StringComparison.OrdinalIgnoreCase) != -1) { - choice = "en"; - selectedLanguage = "English"; - culture = ciEnglish; + choice = LanguageConst.esLanguageId; + selectedLanguage = LanguageConst.esLanguageName; + culture = LanguageConst.ciSpanish; } - else if (choice.IndexOf(@"español", 0, StringComparison.OrdinalIgnoreCase) != -1) + else if (choice.IndexOf(LanguageConst.zhLanguageName, 0, StringComparison.OrdinalIgnoreCase) != -1) { - choice = "es"; - selectedLanguage = "Español"; - culture = ciSpanish; + choice = LanguageConst.zhLanguageId; + selectedLanguage = LanguageConst.zhLanguageName; + culture = LanguageConst.ciChinese; } - else if (choice.IndexOf(@"中文", 0, StringComparison.OrdinalIgnoreCase) != -1) + else if (choice.IndexOf(LanguageConst.frLanguageName, 0, StringComparison.OrdinalIgnoreCase) != -1) { - choice = "zh-CN"; - selectedLanguage = "中文"; - culture = ciChinese; + choice = LanguageConst.frLanguageId; + selectedLanguage = LanguageConst.frLanguageName; + culture = LanguageConst.ciFrench; } else { - choice = "en"; - selectedLanguage = "English"; - culture = ciEnglish; + choice = LanguageConst.enLanguageId; + selectedLanguage = LanguageConst.enLanguageName; + culture = LanguageConst.ciEnglish; } context.UserData.SetValue(@"userLanguage", choice); diff --git a/EmergencyServicesBot.csproj b/EmergencyServicesBot.csproj index a084936..5573ca9 100644 --- a/EmergencyServicesBot.csproj +++ b/EmergencyServicesBot.csproj @@ -1,7 +1,7 @@  + - @@ -175,6 +175,7 @@ + @@ -183,6 +184,11 @@ Global.asax + + Resources.fr-FR.resx + True + True + True True @@ -211,6 +217,10 @@ + + ResXFileCodeGenerator + Resources.fr-FR.Designer.cs + ResXFileCodeGenerator Resources.en-US.Designer.cs @@ -256,9 +266,9 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Voici une réponse qui je pense vous aidera : + + + Bien sûr, Comment puis-je vous aider ? Vous pouvez demander quelque chose comme “Quelle démarche dois-je faire pour une déclaration d'assurance ?” ou “Comment puis-je contacter un centre d'urgence pour une assistance ?” + + + Obtenir les réponses + + + 1 - Obtenir les réponses + + + 2 - 🌐 Sélectionner la langue + + + Auriez-vous d'autres questions auxquelles je pourrais répondre ? + + + 🌐 Sélectionner la langue + + + Langue définie à + + + Bienvenue dans le bot de Service d'Urgences ! Comment puis-je vous aider ? + + + J'ai rencontré une erreur dans la demande de réponse à votre question. Peut-être une autre question marchera mieux. Y-a-t-il quelque chose d'autre que je puisse faire pour vous aider ? + + + Désolé, je n'ai pas trouvé de réponse correspondant à votre demande. + + + Plus d'informations + + + https://pbs.twimg.com/profile_images/378800000210718633/4a028ce30e9238501be08f97ddbfd9f8_400x400.png + + + Service d'Urgences bot + + \ No newline at end of file diff --git a/packages.config b/packages.config index 2104e0b..205addc 100644 --- a/packages.config +++ b/packages.config @@ -23,7 +23,7 @@ - +