Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

[C#][Bot-Solutions] Fix MultiProviderAuthDialog not being localized #3760

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Microsoft.Bot.Solutions.Authentication
/// </summary>
public class MultiProviderAuthDialog : ComponentDialog
{
private static readonly string[] AcceptedLocales = new string[] { "en", "de", "es", "fr", "it", "zh" };
private string _selectedAuthType = string.Empty;
private List<OAuthConnection> _authenticationConnections;
private ResponseManager _responseManager;
Expand All @@ -37,7 +38,7 @@ public MultiProviderAuthDialog(
_authenticationConnections = authenticationConnections ?? throw new ArgumentNullException(nameof(authenticationConnections));
_oauthCredentials = oauthCredentials;
_responseManager = new ResponseManager(
new string[] { "en", "de", "es", "fr", "it", "zh" },
AcceptedLocales,
new AuthenticationResponses());

var firstStep = new WaterfallStep[]
Expand All @@ -62,23 +63,13 @@ public MultiProviderAuthDialog(
{
var connection = _authenticationConnections[i];

// We ignore placeholder connections in config that don't have a Name
if (!string.IsNullOrWhiteSpace(connection.Name))
foreach (var locale in AcceptedLocales)
{
var loginButtonActivity = _responseManager.GetResponse(AuthenticationResponses.LoginButton);
var loginPromptActivity = _responseManager.GetResponse(AuthenticationResponses.LoginPrompt, new StringDictionary() { { "authType", connection.Name } });
var settings = promptSettings?[i] ?? new OAuthPromptSettings
// We ignore placeholder connections in config that don't have a Name
if (!string.IsNullOrWhiteSpace(connection.Name))
{
ConnectionName = connection.Name,
Title = loginButtonActivity.Text,
Text = loginPromptActivity.Text,
};
settings.OAuthAppCredentials = _oauthCredentials;

AddDialog(new OAuthPrompt(
connection.Name,
settings,
AuthPromptValidatorAsync));
AddDialog(GetLocalizedDialog(locale, connection.Name, promptSettings?[i]));
}
}
}

Expand Down Expand Up @@ -114,7 +105,7 @@ private async Task<DialogTurnResult> PromptForProviderAsync(WaterfallStepContext
{
if (_authenticationConnections.Count == 1)
{
var result = _authenticationConnections.First().Name;
var result = _authenticationConnections.First().Name + "_" + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
return await stepContext.NextAsync(result).ConfigureAwait(false);
}

Expand Down Expand Up @@ -256,6 +247,24 @@ private Task<bool> AuthPromptValidatorAsync(PromptValidatorContext<TokenResponse
return Task.FromResult(false);
}

private OAuthPrompt GetLocalizedDialog(string locale, string connectionName, OAuthPromptSettings settings)
{
var loginButtonActivity = _responseManager.GetResponse(AuthenticationResponses.LoginButton, locale);
var loginPromptActivity = _responseManager.GetResponse(AuthenticationResponses.LoginPrompt, locale, new StringDictionary() { { "authType", connectionName } });
settings = settings ?? new OAuthPromptSettings
{
ConnectionName = connectionName,
Title = loginButtonActivity.Text,
Text = loginPromptActivity.Text,
};
settings.OAuthAppCredentials = _oauthCredentials;

return new OAuthPrompt(
connectionName + "_" + locale,
settings,
AuthPromptValidatorAsync);
}

private static class DialogIds
{
public const string ProviderPrompt = "ProviderPrompt";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ public Activity GetResponse(
return ParseResponse(template, tokens);
}

/// <summary>
/// Gets a simple response from template with Text, Speak, InputHint, and SuggestedActions set.
/// </summary>
/// <param name="templateId">The name of the response template.</param>
/// <param name="locale">The locale for the response template.</param>
/// <param name="tokens">StringDictionary of tokens to replace in the response.</param>
/// <returns>An Activity.</returns>
public Activity GetResponse(
string templateId,
string locale,
StringDictionary tokens = null)
{
var template = GetResponseTemplate(templateId, locale);

// create the response the data items
return ParseResponse(template, tokens);
}

/// <summary>
/// Get a response with an Adaptive Card attachment.
/// </summary>
Expand Down