forked from microsoft/botframework-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated T4 templates (microsoft#173)
* Updated t4 templates with a new version that dynamically determiens the path to the json file. * Reverted launchSettings to the original * Merged latest from master * Moved response management logic into ResponseManager. Moved resusable tt code into a t4 include file. Created several tests. * Changed things so we don't assume en is the default language and remove the .en from the json files. * Updated tt files and regenrated code. Renamed .en.json to .json
- Loading branch information
Showing
96 changed files
with
1,077 additions
and
5,113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Dialogs/ResponseManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, Dictionary<string, BotResponse>> _jsonResponses; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ResponseManager"/> class. | ||
/// </summary> | ||
/// <param name="resourcePath">The full path to the resource files.</param> | ||
/// <param name="resourceName">The name of the resources (e.g: MyResponses).</param> | ||
public ResponseManager(string resourcePath, string resourceName) | ||
{ | ||
_defaultJsonFile = resourceName + ".json"; | ||
_extraLanguageJsonFileSearchPattern = resourceName + ".*.json"; | ||
_jsonFilePath = resourcePath; | ||
} | ||
|
||
private Dictionary<string, Dictionary<string, BotResponse>> 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<BotResponse>(JsonConvert.SerializeObject(botResponse)); | ||
} | ||
|
||
protected virtual Dictionary<string, Dictionary<string, BotResponse>> LoadResponses() | ||
{ | ||
var jsonResponses = new Dictionary<string, Dictionary<string, BotResponse>>(); | ||
|
||
var jsonFiles = new List<string>(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<Dictionary<string, BotResponse>>(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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 25 additions & 90 deletions
115
solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Resources/CommonResponses.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// Contains bot responses. | ||
/// </summary> | ||
public static class CommonResponses | ||
{ | ||
private const string _jsonFileName = "CommonResponses.*.json"; | ||
private static readonly ResponseManager _responseManager; | ||
|
||
private static Dictionary<string, Dictionary<string, BotResponse>> _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<string, Dictionary<string, BotResponse>> JsonResponses | ||
{ | ||
get | ||
{ | ||
if (_jsonResponses != null) | ||
{ | ||
return _jsonResponses; | ||
} | ||
|
||
_jsonResponses = new Dictionary<string, Dictionary<string, BotResponse>>(); | ||
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<Dictionary<string, BotResponse>>(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<BotResponse>(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); | ||
} | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.