diff --git a/solutions/Virtual-Assistant/src/csharp/VirtualAssistant.sln b/solutions/Virtual-Assistant/src/csharp/VirtualAssistant.sln index 19b97be8de..f05a00bec8 100644 --- a/solutions/Virtual-Assistant/src/csharp/VirtualAssistant.sln +++ b/solutions/Virtual-Assistant/src/csharp/VirtualAssistant.sln @@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VirtualAssistant", "assista EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Solutions", "Microsoft.Bot.Solutions\Microsoft.Bot.Solutions.csproj", "{0106AD2E-C570-42DF-B54B-3ACA50D58D80}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Skills", "skills", "{1F423A0A-64D9-400C-B2B0-3AC378A5AB02}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Skills", "Skills", "{1F423A0A-64D9-400C-B2B0-3AC378A5AB02}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CalendarSkill", "skills\calendarskill\CalendarSkill.csproj", "{6BD03130-4931-410F-83EB-03E37E17E2B9}" EndProject @@ -23,12 +23,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PointOfInterestSkill", "ski EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToDoSkill", "skills\todoskill\ToDoSkill.csproj", "{3DE6A4B2-9241-4605-9E8C-BD17A1F71502}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "test", "{546AC410-598F-49FD-9583-F912DF35BDA5}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{546AC410-598F-49FD-9583-F912DF35BDA5}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFramework", "skills\tests\testframework\TestFramework.csproj", "{1B4F6C4B-C813-476D-84C5-D9FD51678065}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EmailSkillTest", "skills\tests\emailskilltest\EmailSkillTest.csproj", "{699BA894-1429-4DB8-ABF6-826E3F143F49}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{DFCBFFD7-4882-4C3B-9E21-83EE29CE59BF}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Solutions.Tests", "tests\Microsoft.Bot.Solutions.Tests\Microsoft.Bot.Solutions.Tests.csproj", "{18366252-CEA5-4FB9-A022-ED957B491237}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -67,6 +71,10 @@ Global {699BA894-1429-4DB8-ABF6-826E3F143F49}.Debug|Any CPU.Build.0 = Debug|Any CPU {699BA894-1429-4DB8-ABF6-826E3F143F49}.Release|Any CPU.ActiveCfg = Release|Any CPU {699BA894-1429-4DB8-ABF6-826E3F143F49}.Release|Any CPU.Build.0 = Release|Any CPU + {18366252-CEA5-4FB9-A022-ED957B491237}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {18366252-CEA5-4FB9-A022-ED957B491237}.Debug|Any CPU.Build.0 = Debug|Any CPU + {18366252-CEA5-4FB9-A022-ED957B491237}.Release|Any CPU.ActiveCfg = Release|Any CPU + {18366252-CEA5-4FB9-A022-ED957B491237}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -79,6 +87,7 @@ Global {546AC410-598F-49FD-9583-F912DF35BDA5} = {1F423A0A-64D9-400C-B2B0-3AC378A5AB02} {1B4F6C4B-C813-476D-84C5-D9FD51678065} = {546AC410-598F-49FD-9583-F912DF35BDA5} {699BA894-1429-4DB8-ABF6-826E3F143F49} = {546AC410-598F-49FD-9583-F912DF35BDA5} + {18366252-CEA5-4FB9-A022-ED957B491237} = {DFCBFFD7-4882-4C3B-9E21-83EE29CE59BF} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7569C2D0-1323-45B5-8CFF-3ECACD9E0B82} diff --git a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Dialogs/Reply.cs b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Dialogs/Reply.cs index f4e0d5a7fe..d63ce07330 100644 --- a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Dialogs/Reply.cs +++ b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Dialogs/Reply.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using Microsoft.Bot.Schema; + namespace Microsoft.Bot.Solutions.Dialogs { using Newtonsoft.Json; @@ -13,6 +15,9 @@ public class Reply [JsonProperty("speak")] public string Speak { get; set; } + /// + /// Gets or sets the property of an . + /// [JsonProperty("text")] public string Text { get; set; } } diff --git a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Dialogs/ResponseManager.cs b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Dialogs/ResponseManager.cs new file mode 100644 index 0000000000..1a00481f9d --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Dialogs/ResponseManager.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Runtime.CompilerServices; +using System.Text; +using Newtonsoft.Json; + +namespace Microsoft.Bot.Solutions.Dialogs +{ + public class ResponseManager + { + private const string _defaultLocaleKey = "Default"; + private readonly string _defaultJsonFile; + private readonly string _extraLanguageJsonFileSearchPattern; + private readonly string _jsonFilePath; + private Dictionary> _jsonResponses; + + /// + /// Initializes a new instance of the class. + /// + /// The full path to the resource files. + /// The name of the resources (e.g: MyResponses). + public ResponseManager(string resourcePath, string resourceName) + { + _defaultJsonFile = resourceName + ".json"; + _extraLanguageJsonFileSearchPattern = resourceName + ".*.json"; + _jsonFilePath = resourcePath; + } + + private Dictionary> JsonResponses + { + get + { + if (_jsonResponses != null) + { + return _jsonResponses; + } + + _jsonResponses = LoadResponses(); + + return _jsonResponses; + } + } + + public virtual BotResponse GetBotResponse([CallerMemberName] string propertyName = null) + { + var locale = CultureInfo.CurrentUICulture.Name; + var key = GetJsonResponseKeyForLocale(locale, propertyName); + + // try parent language + if (key == null) + { + locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); + key = GetJsonResponseKeyForLocale(locale, propertyName); + + // fall back to default + if (key == null) + { + locale = _defaultLocaleKey; + key = GetJsonResponseKeyForLocale(locale, propertyName); + } + } + + var botResponse = JsonResponses[locale][key ?? throw new KeyNotFoundException($"Unable to find response \"{propertyName}\".")]; + return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); + } + + protected virtual Dictionary> LoadResponses() + { + var jsonResponses = new Dictionary>(); + + var jsonFiles = new List(Directory.GetFiles(_jsonFilePath, _extraLanguageJsonFileSearchPattern)); + + var defaultFile = Path.Combine(_jsonFilePath, _defaultJsonFile); + if (!File.Exists(defaultFile)) + { + throw new FileNotFoundException($"Unable to find \"{_defaultJsonFile}\" under \"{_jsonFilePath}\".", Path.Combine(_jsonFilePath, _extraLanguageJsonFileSearchPattern)); + } + + jsonFiles.Add(defaultFile); + + foreach (var file in jsonFiles) + { + try + { + string jsonData; + using (var sr = new StreamReader(file, Encoding.GetEncoding("iso-8859-1"))) + { + jsonData = sr.ReadToEnd(); + } + + var responses = JsonConvert.DeserializeObject>(jsonData); + + var fileInfo = new FileInfo(file); + var localeKey = string.Equals(fileInfo.Name, _defaultJsonFile, StringComparison.CurrentCultureIgnoreCase) ? _defaultLocaleKey : fileInfo.Name.Split(".")[1].ToLower(); + jsonResponses.Add(localeKey, responses); + } + catch (JsonSerializationException ex) + { + throw new JsonSerializationException($"Error deserializing {file}. {ex.Message}", ex); + } + } + + return jsonResponses; + } + + private string GetJsonResponseKeyForLocale(string locale, string propertyName) + { + if (JsonResponses.ContainsKey(locale)) + { + return JsonResponses[locale].ContainsKey(propertyName) ? propertyName : null; + } + + return null; + } + } +} \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Microsoft.Bot.Solutions.csproj b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Microsoft.Bot.Solutions.csproj index 7200eefab0..fefd12b3d2 100644 --- a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Microsoft.Bot.Solutions.csproj +++ b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Microsoft.Bot.Solutions.csproj @@ -2,6 +2,7 @@ netcoreapp2.1 + Microsoft.Bot.Solutions @@ -51,7 +52,7 @@ Always - + Always @@ -59,6 +60,9 @@ CommonResponses.cs TextTemplatingFileGenerator + + Always + diff --git a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.cs b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.cs index 5ff5a41566..48e290beae 100644 --- a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.cs @@ -1,107 +1,42 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; +using System.IO; +using System.Runtime.CompilerServices; +using Microsoft.Bot.Solutions.Dialogs; + namespace Microsoft.Bot.Solutions.Resources { - using System; - using System.Collections.Generic; - using System.Globalization; - using System.IO; - using System.Linq; - using System.Runtime.CompilerServices; - using Microsoft.Bot.Solutions.Dialogs; - using Newtonsoft.Json; - + /// + /// Contains bot responses. + /// public static class CommonResponses { - private const string _jsonFileName = "CommonResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> _jsonResponses; + static CommonResponses() + { + var dir = Path.GetDirectoryName(typeof(CommonResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Resources"); + _responseManager = new ResponseManager(resDir, "CommonResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse ConfirmUserInfo => GetBotResponse(); - + public static BotResponse ConfirmSaveInfoFailed => GetBotResponse(); - + public static BotResponse ErrorMessage => GetBotResponse(); - + public static BotResponse SkillAuthenticationTitle => GetBotResponse(); - + public static BotResponse SkillAuthenticationPrompt => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(CommonResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Resources"); - - var jsonFiles = Directory.GetFiles(resDir, _jsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var jsonResponses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - if (_jsonResponses.ContainsKey(key)) - { - _jsonResponses[key] = jsonResponses; - } - else - { - _jsonResponses.Add(key, jsonResponses); - } - } - - return _jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.en.json b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.tt b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.tt index aa06ac6b3c..b33fa43587 100644 --- a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.tt @@ -1,110 +1,4 @@ <#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. -namespace <#= namespaceName #> -{ - using System; - using System.Collections.Generic; - using System.Globalization; - using System.IO; - using System.Linq; - using System.Runtime.CompilerServices; - using Microsoft.Bot.Solutions.Dialogs; - using Newtonsoft.Json; - - public static class <#= className #> - { - private const string _jsonFileName = "<#=className#>.*.json"; - - private static Dictionary> _jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Resources"); - - var jsonFiles = Directory.GetFiles(resDir, _jsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var jsonResponses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - if (_jsonResponses.ContainsKey(key)) - { - _jsonResponses[key] = jsonResponses; - } - else - { - _jsonResponses.Add(key, jsonResponses); - } - } - - return _jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/ResponsesTemplate.t4 b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/ResponsesTemplate.t4 new file mode 100644 index 0000000000..00599836f4 --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/ResponsesTemplate.t4 @@ -0,0 +1,46 @@ +<#@ assembly name="Newtonsoft.Json.dll" #> +<# + var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); + var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; + string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".json")); + var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); + var resourcePath = System.IO.Path.GetDirectoryName(Host.TemplateFile); + var projectPath = Host.ResolveAssemblyReference("$(ProjectDir)"); + var relativeResourcePath = resourcePath.Substring(projectPath.Length); +#> +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +using System; +using System.IO; +using System.Runtime.CompilerServices; +using Microsoft.Bot.Solutions.Dialogs; + +namespace <#= namespaceName #> +{ + /// + /// Contains bot responses. + /// + public static class <#= className #> + { + private static readonly ResponseManager _responseManager; + + static <#= className #>() + { + var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); + var resDir = Path.Combine(dir, @"<#=relativeResourcePath#>"); + _responseManager = new ResponseManager(resDir, "<#=className#>"); + } + + // Generated accessors<# + // This code runs in the text json: + foreach (var propertyName in json) { #> + public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); + <# } #> + + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) + { + return _responseManager.GetBotResponse(propertyName); + } + } +} \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/CalendarSkill.csproj b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/CalendarSkill.csproj index 1fc761837e..466e51ee2e 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/CalendarSkill.csproj +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/CalendarSkill.csproj @@ -150,25 +150,25 @@ Always - + Always Always - + Always Always - + Always Always - + Always @@ -186,19 +186,19 @@ Always - + Always Always - + Always Always - + Always diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.cs index f172fab9f4..2a2f177e81 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.cs @@ -1,129 +1,66 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace CalendarSkill.Dialogs.CreateEvent.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class CreateEventResponses { - private const string JsonFileName = "CreateEventResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static CreateEventResponses() + { + var dir = Path.GetDirectoryName(typeof(CreateEventResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\CreateEvent\Resources"); + _responseManager = new ResponseManager(resDir, "CreateEventResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse NoTitle => GetBotResponse(); - + public static BotResponse NoContent => GetBotResponse(); - + public static BotResponse NoLocation => GetBotResponse(); - + public static BotResponse ConfirmCreate => GetBotResponse(); - + public static BotResponse ConfirmCreateFailed => GetBotResponse(); - + public static BotResponse EventCreated => GetBotResponse(); - + public static BotResponse EventCreationFailed => GetBotResponse(); - + public static BotResponse NoAttendeesMS => GetBotResponse(); - + public static BotResponse WrongAddress => GetBotResponse(); - + public static BotResponse NoAttendees => GetBotResponse(); - + public static BotResponse PromptTooManyPeople => GetBotResponse(); - + public static BotResponse PromptPersonNotFound => GetBotResponse(); - + public static BotResponse NoStartDate => GetBotResponse(); - + public static BotResponse NoStartTime => GetBotResponse(); - + public static BotResponse NoDuration => GetBotResponse(); - + public static BotResponse FindUserErrorMessage => GetBotResponse(); - + public static BotResponse ConfirmRecipient => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(CreateEventResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\CreateEvent\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.tt index 274e537649..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/Resources/CreateEventResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\CreateEvent\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.cs index 40e79a740c..c471fce4a5 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.cs @@ -1,109 +1,46 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace CalendarSkill.Dialogs.DeleteEvent.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class DeleteEventResponses { - private const string JsonFileName = "DeleteEventResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static DeleteEventResponses() + { + var dir = Path.GetDirectoryName(typeof(DeleteEventResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\DeleteEvent\Resources"); + _responseManager = new ResponseManager(resDir, "DeleteEventResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse ConfirmDelete => GetBotResponse(); - + public static BotResponse ConfirmDeleteFailed => GetBotResponse(); - + public static BotResponse EventDeleted => GetBotResponse(); - + public static BotResponse EventWithStartTimeNotFound => GetBotResponse(); - + public static BotResponse NoDeleteStartTime => GetBotResponse(); - + public static BotResponse NoUpdateStartTime => GetBotResponse(); - + public static BotResponse MultipleEventsStartAtSameTime => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(DeleteEventResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\DeleteEvent\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.tt index 1eed631ce9..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/DeleteEvent/Resources/DeleteEventResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\DeleteEvent\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.cs index 089c6d470f..09158e64b5 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.cs @@ -1,107 +1,44 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace CalendarSkill.Dialogs.Main.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class CalendarMainResponses { - private const string JsonFileName = "CalendarMainResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static CalendarMainResponses() + { + var dir = Path.GetDirectoryName(typeof(CalendarMainResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Main\Resources"); + _responseManager = new ResponseManager(resDir, "CalendarMainResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse CalendarWelcomeMessage => GetBotResponse(); - + public static BotResponse HelpMessage => GetBotResponse(); - + public static BotResponse GreetingMessage => GetBotResponse(); - + public static BotResponse GoodbyeMessage => GetBotResponse(); - + public static BotResponse LogOut => GetBotResponse(); - + public static BotResponse FeatureNotAvailable => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(CalendarMainResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Main\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.tt index 3e815afb84..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Main/Resources/CalendarMainResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Main\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.cs index d5a4480e7a..fd50d2e384 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.cs @@ -1,103 +1,40 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace CalendarSkill.Dialogs.NextMeeting.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class NextMeetingResponses { - private const string JsonFileName = "NextMeetingResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static NextMeetingResponses() + { + var dir = Path.GetDirectoryName(typeof(NextMeetingResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\NextMeeting\Resources"); + _responseManager = new ResponseManager(resDir, "NextMeetingResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse ShowNoMeetingMessage => GetBotResponse(); - + public static BotResponse ShowNextMeetingNoLocationMessage => GetBotResponse(); - + public static BotResponse ShowNextMeetingMessage => GetBotResponse(); - + public static BotResponse ShowMultipleNextMeetingMessage => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(NextMeetingResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\NextMeeting\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.tt index 5a4bd898c8..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/NextMeeting/Resources/NextMeetingResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\NextMeeting\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.cs index 18b136bedb..6998f2a41d 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.cs @@ -1,124 +1,44 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace CalendarSkill.Dialogs.Shared.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class CalendarSharedResponses { - private const string JsonFileName = "CalendarSharedResponses.*.json"; - private static Exception resourceLoadingException; + private static readonly ResponseManager _responseManager; - private static Dictionary> _jsonResponses; + static CalendarSharedResponses() + { + var dir = Path.GetDirectoryName(typeof(CalendarSharedResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Shared\Resources"); + _responseManager = new ResponseManager(resDir, "CalendarSharedResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse DidntUnderstandMessage => GetBotResponse(); - + public static BotResponse CancellingMessage => GetBotResponse(); - + public static BotResponse NoAuth => GetBotResponse(); - + public static BotResponse AuthFailed => GetBotResponse(); - + public static BotResponse ActionEnded => GetBotResponse(); - + public static BotResponse CalendarErrorMessage => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(CalendarSharedResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Shared\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - try - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - _jsonResponses.Add(key, responses); - } - catch (JsonReaderException ex) - { - _jsonResponses = null; - resourceLoadingException = new Exception($"Deserialization exception when deserializing response resource file {file}: {ex.Message}"); - break; - } - catch (Exception ex) - { - _jsonResponses = null; - resourceLoadingException = ex; - break; - } - } - - resourceLoadingException = null; - return _jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - // warm up the JsonResponses loading to see if it actually exist. If not, throw with the loading time exception that's actually helpful - var jsonResponses = JsonResponses; - if (jsonResponses == null && resourceLoadingException != null) - { - throw resourceLoadingException; - } - - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.tt index 4af1ccb55a..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Shared/Resources/CalendarSharedResponses.tt @@ -1,125 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - private static Exception resourceLoadingException; - - private static Dictionary> _jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Shared\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - try - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - _jsonResponses.Add(key, responses); - } - catch (JsonReaderException ex) - { - _jsonResponses = null; - resourceLoadingException = new Exception($"Deserialization exception when deserializing response resource file {file}: {ex.Message}"); - break; - } - catch (Exception ex) - { - _jsonResponses = null; - resourceLoadingException = ex; - break; - } - } - - resourceLoadingException = null; - return _jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - // warm up the JsonResponses loading to see if it actually exist. If not, throw with the loading time exception that's actually helpful - var jsonResponses = JsonResponses; - if (jsonResponses == null && resourceLoadingException != null) - { - throw resourceLoadingException; - } - - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.cs index 8ab6a19d21..36281d7ac5 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.cs @@ -1,109 +1,46 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace CalendarSkill.Dialogs.Summary.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class SummaryResponses { - private const string JsonFileName = "SummaryResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static SummaryResponses() + { + var dir = Path.GetDirectoryName(typeof(SummaryResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Summary\Resources"); + _responseManager = new ResponseManager(resDir, "SummaryResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse CalendarNoMoreEvent => GetBotResponse(); - + public static BotResponse CalendarNoPreviousEvent => GetBotResponse(); - + public static BotResponse ShowNoMeetingMessage => GetBotResponse(); - + public static BotResponse ShowOneMeetingSummaryMessage => GetBotResponse(); - + public static BotResponse ReadOutPrompt => GetBotResponse(); - + public static BotResponse ReadOutMorePrompt => GetBotResponse(); - + public static BotResponse ReadOutMessage => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(SummaryResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Summary\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.tt index bfac6a4a68..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/Summary/Resources/SummaryResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Summary\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.cs index a50fd1afcd..7ba4439377 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.cs @@ -1,113 +1,50 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace CalendarSkill.Dialogs.UpdateEvent.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class UpdateEventResponses { - private const string JsonFileName = "UpdateEventResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static UpdateEventResponses() + { + var dir = Path.GetDirectoryName(typeof(UpdateEventResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\UpdateEvent\Resources"); + _responseManager = new ResponseManager(resDir, "UpdateEventResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse NotEventOrganizer => GetBotResponse(); - + public static BotResponse ConfirmUpdate => GetBotResponse(); - + public static BotResponse ConfirmUpdateFailed => GetBotResponse(); - + public static BotResponse EventUpdated => GetBotResponse(); - + public static BotResponse NoNewTime => GetBotResponse(); - + public static BotResponse EventWithStartTimeNotFound => GetBotResponse(); - + public static BotResponse NoDeleteStartTime => GetBotResponse(); - + public static BotResponse NoUpdateStartTime => GetBotResponse(); - + public static BotResponse MultipleEventsStartAtSameTime => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(UpdateEventResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\UpdateEvent\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.tt index 006673ab81..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/UpdateEvent/Resources/UpdateEventResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\UpdateEvent\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.cs index c5c87705c6..046e8d53cb 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.cs @@ -1,105 +1,42 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace EmailSkill.Dialogs.ConfirmRecipient.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class ConfirmRecipientResponses { - private const string JsonFileName = "ConfirmRecipientResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static ConfirmRecipientResponses() + { + var dir = Path.GetDirectoryName(typeof(ConfirmRecipientResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\ConfirmRecipient\Resources"); + _responseManager = new ResponseManager(resDir, "ConfirmRecipientResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse PromptTooManyPeople => GetBotResponse(); - + public static BotResponse PromptPersonNotFound => GetBotResponse(); - + public static BotResponse ConfirmRecipient => GetBotResponse(); - + public static BotResponse ConfirmRecipientNotFirstPage => GetBotResponse(); - + public static BotResponse ConfirmRecipientLastPage => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(ConfirmRecipientResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ConfirmRecipient\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.tt index 6fdface391..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ConfirmRecipient/Resources/ConfirmRecipientResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ConfirmRecipient\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.cs index 8056b26d7c..c100c5f261 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.cs @@ -1,101 +1,38 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace EmailSkill.Dialogs.DeleteEmail.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class DeleteEmailResponses { - private const string JsonFileName = "DeleteEmailResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static DeleteEmailResponses() + { + var dir = Path.GetDirectoryName(typeof(DeleteEmailResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\DeleteEmail\Resources"); + _responseManager = new ResponseManager(resDir, "DeleteEmailResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse DeletePrompt => GetBotResponse(); - + public static BotResponse DeleteConfirm => GetBotResponse(); - + public static BotResponse DeleteSuccessfully => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(DeleteEmailResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\DeleteEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.tt index dd0e9a02f2..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/DeleteEmail/Resources/DeleteEmailResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\DeleteEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.cs index 09bb4246cf..ed72112aae 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.cs @@ -1,95 +1,32 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace EmailSkill.Dialogs.ForwardEmail.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class ForwardEmailResponses { - private const string JsonFileName = "ForwardEmailResponses.*.json"; - - private static Dictionary> jsonResponses; + private static readonly ResponseManager _responseManager; - // Generated code: - // This code runs in the text json: - private static Dictionary> JsonResponses + static ForwardEmailResponses() { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(ForwardEmailResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ForwardEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } + var dir = Path.GetDirectoryName(typeof(ForwardEmailResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\ForwardEmail\Resources"); + _responseManager = new ResponseManager(resDir, "ForwardEmailResponses"); } + // Generated accessors private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.tt index 034a75898f..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ForwardEmail/Resources/ForwardEmailResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ForwardEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.cs index 6db4ccd65b..ed1e5459b1 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.cs @@ -1,105 +1,42 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace EmailSkill.Dialogs.Main.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class EmailMainResponses { - private const string JsonFileName = "EmailMainResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static EmailMainResponses() + { + var dir = Path.GetDirectoryName(typeof(EmailMainResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Main\Resources"); + _responseManager = new ResponseManager(resDir, "EmailMainResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse EmailWelcomeMessage => GetBotResponse(); - + public static BotResponse HelpMessage => GetBotResponse(); - + public static BotResponse GreetingMessage => GetBotResponse(); - + public static BotResponse LogOut => GetBotResponse(); - + public static BotResponse FeatureNotAvailable => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(EmailMainResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Main\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.tt index 3e815afb84..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Main/Resources/EmailMainResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Main\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.cs index 3fb469a103..c4c3319f2f 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.cs @@ -1,129 +1,32 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace EmailSkill.Dialogs.ReplyEmail.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class ReplyEmailResponses { - private const string JsonFileName = "ReplyEmailResponses.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: - public static BotResponse NoTitle => GetBotResponse(); - - public static BotResponse NoContent => GetBotResponse(); - - public static BotResponse NoLocation => GetBotResponse(); - - public static BotResponse ConfirmCreate => GetBotResponse(); - - public static BotResponse ConfirmCreateFailed => GetBotResponse(); - - public static BotResponse EventCreated => GetBotResponse(); - - public static BotResponse EventCreationFailed => GetBotResponse(); - - public static BotResponse NoAttendeesMS => GetBotResponse(); - - public static BotResponse WrongAddress => GetBotResponse(); - - public static BotResponse NoAttendees => GetBotResponse(); - - public static BotResponse PromptTooManyPeople => GetBotResponse(); - - public static BotResponse PromptPersonNotFound => GetBotResponse(); - - public static BotResponse NoStartDate => GetBotResponse(); + private static readonly ResponseManager _responseManager; - public static BotResponse NoStartTime => GetBotResponse(); - - public static BotResponse NoDuration => GetBotResponse(); - - public static BotResponse FindUserErrorMessage => GetBotResponse(); - - public static BotResponse ConfirmRecipient => GetBotResponse(); - - private static Dictionary> JsonResponses + static ReplyEmailResponses() { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(ReplyEmailResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ReplyEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } + var dir = Path.GetDirectoryName(typeof(ReplyEmailResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\ReplyEmail\Resources"); + _responseManager = new ResponseManager(resDir, "ReplyEmailResponses"); } + // Generated accessors private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.tt index da6a7ede35..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ReplyEmail/Resources/ReplyEmailResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ReplyEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.cs index e4f79605cc..a54973d546 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.cs @@ -1,101 +1,38 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace EmailSkill.Dialogs.SendEmail.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class SendEmailResponses { - private const string JsonFileName = "SendEmailResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static SendEmailResponses() + { + var dir = Path.GetDirectoryName(typeof(SendEmailResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\SendEmail\Resources"); + _responseManager = new ResponseManager(resDir, "SendEmailResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse RecipientConfirmed => GetBotResponse(); - + public static BotResponse NoSubject => GetBotResponse(); - + public static BotResponse NoMessageBody => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(SendEmailResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\SendEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.tt index e696b61972..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/SendEmail/Resources/SendEmailResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\SendEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.cs index aafe2a6cb9..3ec0a8570e 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.cs @@ -1,144 +1,64 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace EmailSkill.Dialogs.Shared.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class EmailSharedResponses { - private const string JsonFileName = "EmailSharedResponses.*.json"; - private static Exception resourceLoadingException; + private static readonly ResponseManager _responseManager; - private static Dictionary> _jsonResponses; + static EmailSharedResponses() + { + var dir = Path.GetDirectoryName(typeof(EmailSharedResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Shared\Resources"); + _responseManager = new ResponseManager(resDir, "EmailSharedResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse DidntUnderstandMessage => GetBotResponse(); - + public static BotResponse DidntUnderstandMessageIgnoringInput => GetBotResponse(); - + public static BotResponse CancellingMessage => GetBotResponse(); - + public static BotResponse NoAuth => GetBotResponse(); - + public static BotResponse AuthFailed => GetBotResponse(); - + public static BotResponse ActionEnded => GetBotResponse(); - + public static BotResponse EmailErrorMessage => GetBotResponse(); - + public static BotResponse SentSuccessfully => GetBotResponse(); - + public static BotResponse NoRecipients => GetBotResponse(); - + public static BotResponse NoEmailContent => GetBotResponse(); - + public static BotResponse RecipientConfirmed => GetBotResponse(); - + public static BotResponse ConfirmSend => GetBotResponse(); - + public static BotResponse ConfirmSendFailed => GetBotResponse(); - + public static BotResponse EmailNotFound => GetBotResponse(); - + public static BotResponse NoFocusMessage => GetBotResponse(); - + public static BotResponse ShowEmailPrompt => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(EmailSharedResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Shared\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - try - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - _jsonResponses.Add(key, responses); - } - catch (JsonReaderException ex) - { - _jsonResponses = null; - resourceLoadingException = new Exception($"Deserialization exception when deserializing response resource file {file}: {ex.Message}"); - break; - } - catch (Exception ex) - { - _jsonResponses = null; - resourceLoadingException = ex; - break; - } - } - - resourceLoadingException = null; - return _jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - // warm up the JsonResponses loading to see if it actually exist. If not, throw with the loading time exception that's actually helpful - var jsonResponses = JsonResponses; - if (jsonResponses == null && resourceLoadingException != null) - { - throw resourceLoadingException; - } - - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.tt index 4af1ccb55a..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/Shared/Resources/EmailSharedResponses.tt @@ -1,125 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - private static Exception resourceLoadingException; - - private static Dictionary> _jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Shared\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - try - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - _jsonResponses.Add(key, responses); - } - catch (JsonReaderException ex) - { - _jsonResponses = null; - resourceLoadingException = new Exception($"Deserialization exception when deserializing response resource file {file}: {ex.Message}"); - break; - } - catch (Exception ex) - { - _jsonResponses = null; - resourceLoadingException = ex; - break; - } - } - - resourceLoadingException = null; - return _jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - // warm up the JsonResponses loading to see if it actually exist. If not, throw with the loading time exception that's actually helpful - var jsonResponses = JsonResponses; - if (jsonResponses == null && resourceLoadingException != null) - { - throw resourceLoadingException; - } - - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.cs index 0bb46a725d..b13f770f56 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.cs @@ -1,103 +1,40 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace EmailSkill.Dialogs.ShowEmail.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class ShowEmailResponses { - private const string JsonFileName = "ShowEmailResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static ShowEmailResponses() + { + var dir = Path.GetDirectoryName(typeof(ShowEmailResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\ShowEmail\Resources"); + _responseManager = new ResponseManager(resDir, "ShowEmailResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse EmailNotFound => GetBotResponse(); - + public static BotResponse ReadOutPrompt => GetBotResponse(); - + public static BotResponse ReadOutMessage => GetBotResponse(); - + public static BotResponse ReadOutMorePrompt => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(ShowEmailResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ShowEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.tt index c2f82af8b6..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/Dialogs/ShowEmail/Resources/ShowEmailResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ShowEmail\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/EmailSkill.csproj b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/EmailSkill.csproj index 960eaf15fb..f081706881 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/emailskill/EmailSkill.csproj +++ b/solutions/Virtual-Assistant/src/csharp/skills/emailskill/EmailSkill.csproj @@ -120,37 +120,37 @@ Always - + Always Always - + Always Always - + Always Always - + Always Always - + Always Always - + Always @@ -168,13 +168,13 @@ Always - + Always Always - + Always diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.cs index d2c71805b9..b829b8d8a3 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.cs @@ -1,99 +1,36 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace PointOfInterestSkill.Dialogs.CancelRoute.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class CancelRouteResponses { - private const string JsonFileName = "CancelRouteResponses.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: - public static BotResponse CancelActiveRoute => GetBotResponse(); - - public static BotResponse CannotCancelActiveRoute => GetBotResponse(); + private static readonly ResponseManager _responseManager; - private static Dictionary> JsonResponses + static CancelRouteResponses() { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(CancelRouteResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\CancelRoute\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } + var dir = Path.GetDirectoryName(typeof(CancelRouteResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\CancelRoute\Resources"); + _responseManager = new ResponseManager(resDir, "CancelRouteResponses"); } + // Generated accessors + public static BotResponse CancelActiveRoute => GetBotResponse(); + + public static BotResponse CannotCancelActiveRoute => GetBotResponse(); + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.tt index 889bcdde81..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/CancelRoute/Resources/CancelRouteResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\CancelRoute\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.cs index e7a8096113..ad034f0904 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.cs @@ -1,95 +1,32 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace PointOfInterestSkill.Dialogs.FindPointOfInterest.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class FindPointOfInterestResponses { - private const string JsonFileName = "FindPointOfInterestResponses.*.json"; - - private static Dictionary> jsonResponses; + private static readonly ResponseManager _responseManager; - // Generated code: - // This code runs in the text json: - private static Dictionary> JsonResponses + static FindPointOfInterestResponses() { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(FindPointOfInterestResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\FindPointOfInterest\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } + var dir = Path.GetDirectoryName(typeof(FindPointOfInterestResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\FindPointOfInterest\Resources"); + _responseManager = new ResponseManager(resDir, "FindPointOfInterestResponses"); } + // Generated accessors private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.tt index 18dc0ce755..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/FindPointOfInterest/Resources/FindPointOfInterestResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\FindPointOfInterest\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.cs index 454f1e6cdb..65629310c3 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.cs @@ -1,107 +1,44 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace PointOfInterestSkill.Dialogs.Main.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class POIMainResponses { - private const string JsonFileName = "POIMainResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static POIMainResponses() + { + var dir = Path.GetDirectoryName(typeof(POIMainResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Main\Resources"); + _responseManager = new ResponseManager(resDir, "POIMainResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse PointOfInterestWelcomeMessage => GetBotResponse(); - + public static BotResponse HelpMessage => GetBotResponse(); - + public static BotResponse GreetingMessage => GetBotResponse(); - + public static BotResponse GoodbyeMessage => GetBotResponse(); - + public static BotResponse LogOut => GetBotResponse(); - + public static BotResponse FeatureNotAvailable => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(POIMainResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Main\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.tt index 3e815afb84..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Main/Resources/POIMainResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Main\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.cs index f718389ddc..1efbbf2eac 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.cs @@ -1,103 +1,40 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace PointOfInterestSkill.Dialogs.Route.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class RouteResponses { - private const string JsonFileName = "RouteResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static RouteResponses() + { + var dir = Path.GetDirectoryName(typeof(RouteResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Route\Resources"); + _responseManager = new ResponseManager(resDir, "RouteResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse MissingActiveLocationErrorMessage => GetBotResponse(); - + public static BotResponse PromptToStartRoute => GetBotResponse(); - + public static BotResponse SendingRouteDetails => GetBotResponse(); - + public static BotResponse AskAboutRouteLater => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(RouteResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Route\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.tt index 37ed35d404..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Route/Resources/RouteResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Route\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.cs index 866fdc37db..c3224a3b6a 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.cs @@ -1,142 +1,62 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace PointOfInterestSkill.Dialogs.Shared.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class POISharedResponses { - private const string JsonFileName = "POISharedResponses.*.json"; - private static Exception resourceLoadingException; + private static readonly ResponseManager _responseManager; - private static Dictionary> _jsonResponses; + static POISharedResponses() + { + var dir = Path.GetDirectoryName(typeof(POISharedResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Shared\Resources"); + _responseManager = new ResponseManager(resDir, "POISharedResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse DidntUnderstandMessage => GetBotResponse(); - + public static BotResponse CancellingMessage => GetBotResponse(); - + public static BotResponse NoAuth => GetBotResponse(); - + public static BotResponse AuthFailed => GetBotResponse(); - + public static BotResponse ActionEnded => GetBotResponse(); - + public static BotResponse PointOfInterestErrorMessage => GetBotResponse(); - + public static BotResponse PromptToGetRoute => GetBotResponse(); - + public static BotResponse GetRouteToActiveLocationLater => GetBotResponse(); - + public static BotResponse MultipleLocationsFound => GetBotResponse(); - + public static BotResponse SingleLocationFound => GetBotResponse(); - + public static BotResponse MultipleLocationsFoundAlongActiveRoute => GetBotResponse(); - + public static BotResponse SingleLocationFoundAlongActiveRoute => GetBotResponse(); - + public static BotResponse NoLocationsFound => GetBotResponse(); - + public static BotResponse MultipleRoutesFound => GetBotResponse(); - + public static BotResponse SingleRouteFound => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(POISharedResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Shared\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - try - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - _jsonResponses.Add(key, responses); - } - catch (JsonReaderException ex) - { - _jsonResponses = null; - resourceLoadingException = new Exception($"Deserialization exception when deserializing response resource file {file}: {ex.Message}"); - break; - } - catch (Exception ex) - { - _jsonResponses = null; - resourceLoadingException = ex; - break; - } - } - - resourceLoadingException = null; - return _jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - // warm up the JsonResponses loading to see if it actually exist. If not, throw with the loading time exception that's actually helpful - var jsonResponses = JsonResponses; - if (jsonResponses == null && resourceLoadingException != null) - { - throw resourceLoadingException; - } - - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.tt index 4af1ccb55a..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/Dialogs/Shared/Resources/POISharedResponses.tt @@ -1,125 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - private static Exception resourceLoadingException; - - private static Dictionary> _jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Shared\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - try - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - _jsonResponses.Add(key, responses); - } - catch (JsonReaderException ex) - { - _jsonResponses = null; - resourceLoadingException = new Exception($"Deserialization exception when deserializing response resource file {file}: {ex.Message}"); - break; - } - catch (Exception ex) - { - _jsonResponses = null; - resourceLoadingException = ex; - break; - } - } - - resourceLoadingException = null; - return _jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - // warm up the JsonResponses loading to see if it actually exist. If not, throw with the loading time exception that's actually helpful - var jsonResponses = JsonResponses; - if (jsonResponses == null && resourceLoadingException != null) - { - throw resourceLoadingException; - } - - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/PointOfInterestSkill.csproj b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/PointOfInterestSkill.csproj index 5e43bd2675..3e956e4be9 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/PointOfInterestSkill.csproj +++ b/solutions/Virtual-Assistant/src/csharp/skills/pointofinterestskill/PointOfInterestSkill.csproj @@ -111,25 +111,25 @@ Always - + Always Always - + Always Always - + Always Always - + Always @@ -153,7 +153,7 @@ Always - + Always diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.cs index d987873cd7..f19dd7f3bd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.cs @@ -1,103 +1,40 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace ToDoSkill.Dialogs.DeleteToDo.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class DeleteToDoResponses { - private const string JsonFileName = "DeleteToDoResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static DeleteToDoResponses() + { + var dir = Path.GetDirectoryName(typeof(DeleteToDoResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\DeleteToDo\Resources"); + _responseManager = new ResponseManager(resDir, "DeleteToDoResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse AfterTaskDeleted => GetBotResponse(); - + public static BotResponse AfterAllTasksDeleted => GetBotResponse(); - + public static BotResponse AskDeletionConfirmation => GetBotResponse(); - + public static BotResponse AskDeletionAllConfirmation => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(DeleteToDoResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\DeleteToDo\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.tt index 324a5c1235..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/DeleteToDo/Resources/DeleteToDoResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\DeleteToDo\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/TODoMainResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/TODoMainResponses.cs index fd77783f3d..c1453aee9f 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/TODoMainResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/TODoMainResponses.cs @@ -1,107 +1,44 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace ToDoSkill.Dialogs.Main.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class ToDoMainResponses { - private const string JsonFileName = "ToDoMainResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static ToDoMainResponses() + { + var dir = Path.GetDirectoryName(typeof(ToDoMainResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Main\Resources"); + _responseManager = new ResponseManager(resDir, "ToDoMainResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse ToDoWelcomeMessage => GetBotResponse(); - + public static BotResponse HelpMessage => GetBotResponse(); - + public static BotResponse GreetingMessage => GetBotResponse(); - + public static BotResponse GoodbyeMessage => GetBotResponse(); - + public static BotResponse LogOut => GetBotResponse(); - + public static BotResponse FeatureNotAvailable => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(ToDoMainResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Main\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/ToDoMainResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/ToDoMainResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/ToDoMainResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/ToDoMainResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/ToDoMainResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/ToDoMainResponses.tt index 3e815afb84..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/ToDoMainResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Main/Resources/ToDoMainResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Main\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.cs index 575119021c..8dff28b065 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.cs @@ -1,99 +1,36 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace ToDoSkill.Dialogs.MarkToDo.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class MarkToDoResponses { - private const string JsonFileName = "MarkToDoResponses.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: - public static BotResponse AfterToDoTaskCompleted => GetBotResponse(); - - public static BotResponse AfterAllToDoTasksCompleted => GetBotResponse(); + private static readonly ResponseManager _responseManager; - private static Dictionary> JsonResponses + static MarkToDoResponses() { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(MarkToDoResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\MarkToDo\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } + var dir = Path.GetDirectoryName(typeof(MarkToDoResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\MarkToDo\Resources"); + _responseManager = new ResponseManager(resDir, "MarkToDoResponses"); } + // Generated accessors + public static BotResponse AfterToDoTaskCompleted => GetBotResponse(); + + public static BotResponse AfterAllToDoTasksCompleted => GetBotResponse(); + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.tt index 0a89b0dd85..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/MarkToDo/Resources/MarkToDoResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\MarkToDo\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.cs index 3780292e1a..483fa1ff20 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.cs @@ -1,136 +1,56 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace ToDoSkill.Dialogs.Shared.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class ToDoSharedResponses { - private const string JsonFileName = "ToDoSharedResponses.*.json"; - private static Exception resourceLoadingException; + private static readonly ResponseManager _responseManager; - private static Dictionary> _jsonResponses; + static ToDoSharedResponses() + { + var dir = Path.GetDirectoryName(typeof(ToDoSharedResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\Shared\Resources"); + _responseManager = new ResponseManager(resDir, "ToDoSharedResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse DidntUnderstandMessage => GetBotResponse(); - + public static BotResponse CancellingMessage => GetBotResponse(); - + public static BotResponse NoAuth => GetBotResponse(); - + public static BotResponse AuthFailed => GetBotResponse(); - + public static BotResponse ActionEnded => GetBotResponse(); - + public static BotResponse ToDoErrorMessage => GetBotResponse(); - + public static BotResponse SettingUpOneNoteMessage => GetBotResponse(); - + public static BotResponse ShowToDoTasks => GetBotResponse(); - + public static BotResponse AskToDoTaskIndex => GetBotResponse(); - + public static BotResponse AskToDoContentText => GetBotResponse(); - + public static BotResponse AfterToDoTaskAdded => GetBotResponse(); - + public static BotResponse NoTasksInList => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(ToDoSharedResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Shared\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - try - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - _jsonResponses.Add(key, responses); - } - catch (JsonReaderException ex) - { - _jsonResponses = null; - resourceLoadingException = new Exception($"Deserialization exception when deserializing response resource file {file}: {ex.Message}"); - break; - } - catch (Exception ex) - { - _jsonResponses = null; - resourceLoadingException = ex; - break; - } - } - - resourceLoadingException = null; - return _jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - // warm up the JsonResponses loading to see if it actually exist. If not, throw with the loading time exception that's actually helpful - var jsonResponses = JsonResponses; - if (jsonResponses == null && resourceLoadingException != null) - { - throw resourceLoadingException; - } - - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.tt index 63decfd69a..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/Shared/Resources/ToDoSharedResponses.tt @@ -1,125 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - private static Exception resourceLoadingException; - - private static Dictionary> _jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (_jsonResponses != null) - { - return _jsonResponses; - } - - _jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\Shared\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - try - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - _jsonResponses.Add(key, responses); - } - catch (JsonReaderException ex) - { - _jsonResponses = null; - resourceLoadingException = new Exception($"Deserialization exception when deserializing response resource file {file}: {ex.Message}"); - break; - } - catch (Exception ex) - { - _jsonResponses = null; - resourceLoadingException = ex; - break; - } - } - - resourceLoadingException = null; - return _jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - // warm up the JsonResponses loading to see if it actually exist. If not, throw with the loading time exception that's actually helpful - var jsonResponses = JsonResponses; - if (jsonResponses == null && resourceLoadingException != null) - { - throw resourceLoadingException; - } - - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.cs b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.cs index b802c35d4f..0a2cd7fb00 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.cs +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.cs @@ -1,105 +1,42 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; namespace ToDoSkill.Dialogs.ShowToDo.Resources { /// - /// Calendar bot responses class. + /// Contains bot responses. /// public static class ShowToDoResponses { - private const string JsonFileName = "ShowToDoResponses.*.json"; + private static readonly ResponseManager _responseManager; - private static Dictionary> jsonResponses; + static ShowToDoResponses() + { + var dir = Path.GetDirectoryName(typeof(ShowToDoResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Dialogs\ShowToDo\Resources"); + _responseManager = new ResponseManager(resDir, "ShowToDoResponses"); + } - // Generated code: - // This code runs in the text json: + // Generated accessors public static BotResponse ReadToDoTasks => GetBotResponse(); - + public static BotResponse ShowNextToDoTasks => GetBotResponse(); - + public static BotResponse ShowPreviousToDoTasks => GetBotResponse(); - + public static BotResponse ShowingMoreTasks => GetBotResponse(); - + public static BotResponse NoToDoTasksPrompt => GetBotResponse(); - - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(ShowToDoResponses).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ShowToDo\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } + return _responseManager.GetBotResponse(propertyName); } } } \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.en.json b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.json similarity index 100% rename from solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.en.json rename to solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.json diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.tt b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.tt index 1cf3bfa306..ab7611c3fd 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.tt +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/Dialogs/ShowToDo/Resources/ShowToDoResponses.tt @@ -1,108 +1,3 @@ -<#@ assembly name="Newtonsoft.Json.dll" #> -<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> -<# - var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile); - var namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");; - string myFile = System.IO.File.ReadAllText(this.Host.ResolvePath(className + ".en.json")); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(myFile); -#> -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Bot.Solutions.Dialogs; -using Newtonsoft.Json; - -namespace <#= namespaceName #> -{ - /// - /// Calendar bot responses class. - /// - public static class <#= className #> - { - private const string JsonFileName = "<#=className#>.*.json"; - - private static Dictionary> jsonResponses; - - // Generated code: - // This code runs in the text json: -<# foreach (var propertyName in json) { #> - public static BotResponse <#= propertyName.Key.Substring(0, 1).ToUpperInvariant() + propertyName.Key.Substring(1) #> => GetBotResponse(); - -<# } #> - private static Dictionary> JsonResponses - { - get - { - if (jsonResponses != null) - { - return jsonResponses; - } - - jsonResponses = new Dictionary>(); - var dir = Path.GetDirectoryName(typeof(<#= className #>).Assembly.Location); - var resDir = Path.Combine(dir, "Dialogs\\ShowToDo\\Resources"); - - var jsonFiles = Directory.GetFiles(resDir, JsonFileName); - foreach (var file in jsonFiles) - { - var jsonData = File.ReadAllText(file); - var responses = JsonConvert.DeserializeObject>(jsonData); - var key = new FileInfo(file).Name.Split(".")[1].ToLower(); - - jsonResponses.Add(key, responses); - } - - return jsonResponses; - } - } - - private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) - { - var locale = CultureInfo.CurrentUICulture.Name; - var theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to parent language - if (theK == null) - { - locale = CultureInfo.CurrentUICulture.Name.Split("-")[0].ToLower(); - theK = GetJsonResponseKeyForLocale(locale, propertyName); - - // fall back to en - if (theK == null) - { - locale = "en"; - theK = GetJsonResponseKeyForLocale(locale, propertyName); - } - } - - var botResponse = JsonResponses[locale][theK ?? throw new ArgumentNullException(nameof(propertyName))]; - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(botResponse)); - } - - private static string GetJsonResponseKeyForLocale(string locale, string propertyName) - { - try - { - if (JsonResponses.ContainsKey(locale)) - { - return JsonResponses[locale].ContainsKey(propertyName) ? - JsonResponses[locale].Keys.FirstOrDefault(k => string.Compare(k, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : - null; - } - - return null; - } - catch (KeyNotFoundException) - { - return null; - } - } - } -} \ No newline at end of file +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/ToDoSkill.csproj b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/ToDoSkill.csproj index 92e6cd21de..71c653c7b2 100644 --- a/solutions/Virtual-Assistant/src/csharp/skills/todoskill/ToDoSkill.csproj +++ b/solutions/Virtual-Assistant/src/csharp/skills/todoskill/ToDoSkill.csproj @@ -97,19 +97,19 @@ Always - + Always Always - + Always Always - + Always @@ -118,7 +118,7 @@ Always - + Always @@ -127,7 +127,7 @@ Always - + Always diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/BrokenJson.json b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/BrokenJson.json new file mode 100644 index 0000000000..1050a6a153 --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/BrokenJson.json @@ -0,0 +1,14 @@ +{ + "GetResponseText": { + "replies": [ + { + "text": "The text", + "speak": "The speak", + "cardText": "The card text" + } + ], + "inputHint": "expectingInput", + "suggestedActions": [ + "Suggestion 1", + "Suggestion 2" + ] diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/ResponseManagerTests.cs b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/ResponseManagerTests.cs new file mode 100644 index 0000000000..709cf31636 --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/ResponseManagerTests.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Reflection; +using Microsoft.Bot.Schema; +using Microsoft.Bot.Solutions.Dialogs; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; + +namespace Microsoft.Bot.Solutions.Tests.Dialogs +{ + [TestClass] + public class ResponseManagerTests + { + private string _resourceDir; + private CultureInfo _currentCulture; + + [TestInitialize] + public void Initialize() + { + _currentCulture = CultureInfo.CurrentUICulture; + _resourceDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Dialogs"); + } + + [TestCleanup] + public void Cleanup() + { + // restore culture in case we changed it. + CultureInfo.CurrentUICulture = _currentCulture; + + } + + [TestMethod] + public void ReturnedResponseIsClone() + { + var rm = new ResponseManager(_resourceDir, "TestResponses"); + var copy1 = rm.GetBotResponse("GetResponseText"); + var copy2 = rm.GetBotResponse("GetResponseText"); + Assert.AreEqual(copy1.Replies[0].Text, copy2.Replies[0].Text); + + copy2.Replies[0].Text = "Something different"; + Assert.AreNotEqual(copy1.Replies[0].Text, copy2.Replies[0].Text); + } + + [TestMethod] + public void JsonNotFoundThrowsException() + { + Console.WriteLine(Directory.GetCurrentDirectory()); + Assert.ThrowsException(() => + { + var rm = new ResponseManager(_resourceDir, "NotThere"); + rm.GetBotResponse("Test"); + }); + } + + [TestMethod] + public void KeyNotFoundThrowsException() + { + Assert.ThrowsException(() => + { + var rm = new ResponseManager(_resourceDir, "TestResponses"); + rm.GetBotResponse("NotThere"); + }); + } + + [TestMethod] + public void MalFormedJsonThrowsException() + { + Assert.ThrowsException(() => + { + var rm = new ResponseManager(_resourceDir, "BrokenJson"); + rm.GetBotResponse("GetResponseText"); + }); + } + + [TestMethod] + public void InputHintDefaultsToAcceptingInput() + { + var rm = new ResponseManager(_resourceDir, "TestResponses"); + var response = rm.GetBotResponse("NoInputHint"); + Assert.AreEqual(InputHints.AcceptingInput, response.InputHint); + } + + [TestMethod] + public void LanguageFallback() + { + CultureInfo.CurrentUICulture = new CultureInfo("es"); + var rm = new ResponseManager(_resourceDir, "TestResponses"); + var response = rm.GetBotResponse("GetResponseText"); + Assert.AreEqual("El texto", response.Reply.Text); + + response = rm.GetBotResponse("EnglishOnly"); + Assert.AreEqual("This wasn't found in spanish so the fallback answer is returned", response.Reply.Text); + + } + + [TestMethod] + public void MultiLanguage() + { + CultureInfo.CurrentUICulture = new CultureInfo("en-US"); + var rm = new ResponseManager(_resourceDir, "TestResponses"); + var response = rm.GetBotResponse("MultiLanguage"); + Assert.AreEqual("This is in English", response.Reply.Text); + + CultureInfo.CurrentUICulture = new CultureInfo("es-MX"); + response = rm.GetBotResponse("MultiLanguage"); + Assert.AreEqual("Esto sería en español", response.Reply.Text); + } + } +} \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/TestResponses.es.json b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/TestResponses.es.json new file mode 100644 index 0000000000..e57649817a --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/TestResponses.es.json @@ -0,0 +1,26 @@ +{ + "GetResponseText": { + "replies": [ + { + "text": "El texto", + "speak": "El habla", + "cardText": "El texto de la tarjeta" + } + ], + "inputHint": "expectingInput", + "suggestedActions": [ + "Sugerencia 1", + "Sugerencia 2" + ] + }, + "MultiLanguage": { + "replies": [ + { + "text": "Esto sería en español", + "speak": "Esto es en Español", + "cardText": "Esto es en Español" + } + ], + "inputHint": "expectingInput" + } +} \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/TestResponses.json b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/TestResponses.json new file mode 100644 index 0000000000..25f8fd523e --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Dialogs/TestResponses.json @@ -0,0 +1,45 @@ +{ + "GetResponseText": { + "replies": [ + { + "text": "The text", + "speak": "The speak", + "cardText": "The card text" + } + ], + "inputHint": "expectingInput", + "suggestedActions": [ + "Suggestion 1", + "Suggestion 2" + ] + }, + "MultiLanguage": { + "replies": [ + { + "text": "This is in English", + "speak": "This is in English", + "cardText": "This is in English" + } + ], + "inputHint": "expectingInput" + }, + "EnglishOnly": { + "replies": [ + { + "text": "This wasn't found in spanish so the fallback answer is returned", + "speak": "This is in English", + "cardText": "This is in English" + } + ], + "inputHint": "expectingInput" + }, + "NoInputHint": { + "replies": [ + { + "text": "This is in English", + "speak": "This is in English", + "cardText": "This is in English" + } + ] + } +} \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Microsoft.Bot.Solutions.Tests.csproj b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Microsoft.Bot.Solutions.Tests.csproj new file mode 100644 index 0000000000..197b7ec6d8 --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Microsoft.Bot.Solutions.Tests.csproj @@ -0,0 +1,50 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + + + Always + + + Always + + + Always + + + Always + + + TextTemplatingFileGenerator + TestResponses.cs + + + + + + + + + + True + True + TestResponses.tt + + + + diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/ResponseGenerationTests.cs b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/ResponseGenerationTests.cs new file mode 100644 index 0000000000..528096fc84 --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/ResponseGenerationTests.cs @@ -0,0 +1,24 @@ +using System; +using Microsoft.Bot.Schema; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.Bot.Solutions.Tests.Resources +{ + [TestClass] + public class ResponseGenerationTests + { + [TestMethod] + public void GetResponseUsingGeneratedAccessor() + { + var response = TestResponses.GetResponseText; + Assert.AreEqual(InputHints.ExpectingInput, response.InputHint); + Assert.AreEqual(response.SuggestedActions[0], "Suggestion 1"); + Assert.AreEqual(response.SuggestedActions[1], "Suggestion 2"); + + var reply = response.Reply; + Assert.AreEqual("The text", reply.Text); + Assert.AreEqual("The speak", reply.Speak); + Assert.AreEqual("The card text", reply.CardText); + } + } +} \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.cs b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.cs new file mode 100644 index 0000000000..661a2932e4 --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.cs @@ -0,0 +1,34 @@ + +// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017 +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +using System; +using System.IO; +using System.Runtime.CompilerServices; +using Microsoft.Bot.Solutions.Dialogs; + +namespace Microsoft.Bot.Solutions.Tests.Resources +{ + /// + /// Contains bot responses. + /// + public static class TestResponses + { + private static readonly ResponseManager _responseManager; + + static TestResponses() + { + var dir = Path.GetDirectoryName(typeof(TestResponses).Assembly.Location); + var resDir = Path.Combine(dir, @"Resources"); + _responseManager = new ResponseManager(resDir, "TestResponses"); + } + + // Generated accessors + public static BotResponse GetResponseText => GetBotResponse(); + + private static BotResponse GetBotResponse([CallerMemberName] string propertyName = null) + { + return _responseManager.GetBotResponse(propertyName); + } + } +} \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.json b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.json new file mode 100644 index 0000000000..7754dfcd1b --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.json @@ -0,0 +1,16 @@ +{ + "GetResponseText": { + "replies": [ + { + "text": "The text", + "speak": "The speak", + "cardText": "The card text" + } + ], + "inputHint": "expectingInput", + "suggestedActions": [ + "Suggestion 1", + "Suggestion 2" + ] + } +} \ No newline at end of file diff --git a/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.tt b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.tt new file mode 100644 index 0000000000..ab7611c3fd --- /dev/null +++ b/solutions/Virtual-Assistant/src/csharp/tests/Microsoft.Bot.Solutions.Tests/Resources/TestResponses.tt @@ -0,0 +1,3 @@ +<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ output extension=".cs" #> +<#@ include file="$(ProjectDir)$(OutDir)Resources\ResponsesTemplate.t4"#> \ No newline at end of file