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

Commit db57d22

Browse files
authored
Merge pull request #3762 from southworks/feature/southworks/fix-MultiProviderAuthDialog-localization-ts
[TypeScript][Bot-Solutions] Fix MultiProviderAuthDialog not being localized
2 parents e449675 + d7a92d9 commit db57d22

9 files changed

Lines changed: 150 additions & 19 deletions

sdk/typescript/libraries/bot-solutions/src/authentication/authenticationResponses.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ export class AuthenticationResponses implements IResponseIdCollection {
1616
public static readonly configuredAuthProvidersPrompt: string = 'ConfiguredAuthProvidersPrompt';
1717
public static readonly errorMessageAuthFailure: string = 'ErrorMessageAuthFailure';
1818
public static readonly noLinkedAccount: string = 'NoLinkedAccount';
19+
public static readonly loginButton: string = 'LoginButton';
20+
public static readonly loginPrompt: string = 'LoginPrompt';
1921
}

sdk/typescript/libraries/bot-solutions/src/authentication/multiProviderAuthDialog.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import { BotFrameworkAdapter, TurnContext } from 'botbuilder';
77
import { Choice, ChoicePrompt, ComponentDialog, DialogTurnResult, DialogTurnStatus, FoundChoice,
88
OAuthPrompt, PromptValidatorContext, WaterfallDialog, WaterfallStep, WaterfallStepContext,
9-
OAuthPromptSettings } from 'botbuilder-dialogs';
9+
OAuthPromptSettings,
10+
Dialog} from 'botbuilder-dialogs';
1011
import { TokenStatus } from 'botframework-connector/lib/tokenApi/models';
1112
import { ActionTypes, Activity, ActivityTypes, TokenResponse } from 'botframework-schema';
1213
import i18next from 'i18next';
@@ -16,7 +17,6 @@ import { TokenEvents } from '../tokenEvents';
1617
import { AuthenticationResponses } from './authenticationResponses';
1718
import { OAuthProviderExtensions } from './oAuthProviderExtensions';
1819
import { IProviderTokenResponse } from './providerTokenResponse';
19-
import { OAuthProvider } from './oAuthProvider';
2020

2121
enum DialogIds {
2222
providerPrompt = 'ProviderPrompt',
@@ -28,6 +28,7 @@ enum DialogIds {
2828
* Provides the ability to prompt for which Authentication provider the user wishes to use.
2929
*/
3030
export class MultiProviderAuthDialog extends ComponentDialog {
31+
private static readonly acceptedLocales: string[] = ['en', 'de', 'es', 'fr', 'it', 'zh'];
3132
private selectedAuthType: string = '';
3233
private authenticationConnections: IOAuthConnection[];
3334
private responseManager: ResponseManager;
@@ -42,7 +43,7 @@ export class MultiProviderAuthDialog extends ComponentDialog {
4243
this.authenticationConnections = authenticationConnections;
4344

4445
this.responseManager = new ResponseManager(
45-
['en', 'de', 'es', 'fr', 'it', 'zh'],
46+
MultiProviderAuthDialog.acceptedLocales,
4647
[AuthenticationResponses]
4748
);
4849

@@ -65,29 +66,35 @@ export class MultiProviderAuthDialog extends ComponentDialog {
6566
for (var i = 0; i < this.authenticationConnections.length; ++i) {
6667
let connection = this.authenticationConnections[i];
6768

68-
// We ignore placeholder connections in config that don't have a Name
69-
if (connection.name !== undefined && connection.name.trim().length > 0) {
70-
const settings: OAuthPromptSettings = promptSettings[i] || {
71-
connectionName: connection.name,
72-
title: i18next.t('common:login'),
73-
text: i18next.t('common:loginDescription', connection.name)
74-
};
75-
76-
this.addDialog(new OAuthPrompt(
77-
connection.name,
78-
settings,
79-
this.authPromptValidator.bind(this)
80-
));
81-
}
69+
MultiProviderAuthDialog.acceptedLocales.forEach((locale): void => {
70+
this.addDialog(this.getLocalizedDialog(locale, connection.name, promptSettings[i]));
71+
});
8272
};
8373

84-
this.addDialog(new WaterfallDialog(DialogIds.firstStepPrompt, authSteps));
74+
this.addDialog(new WaterfallDialog(DialogIds.authPrompt, authSteps));
8575
this.addDialog(new ChoicePrompt(DialogIds.providerPrompt));
8676
} else {
8777
throw new Error('There is no authenticationConnections value');
8878
}
8979
}
9080

81+
private getLocalizedDialog(locale: string, connectionName: string, settings: OAuthPromptSettings): Dialog {
82+
const loginButtonActivity: string = this.responseManager.getLocalizedResponse(AuthenticationResponses.loginButton, locale).text;
83+
const loginPromptActivity: string = this.responseManager.getLocalizedResponse(AuthenticationResponses.loginPrompt, locale, new Map<string, string>([['authType', connectionName]])).text;
84+
85+
settings = settings || {
86+
connectionName: connectionName,
87+
title: loginButtonActivity,
88+
text: loginPromptActivity
89+
};
90+
91+
return new OAuthPrompt(
92+
connectionName + '_' + locale,
93+
settings,
94+
this.authPromptValidator.bind(this)
95+
);
96+
}
97+
9198
// Validators
9299
protected async tokenResponseValidator(promptContext: PromptValidatorContext<Activity>): Promise<boolean> {
93100
const activity: Activity | undefined = promptContext.recognized.value;
@@ -107,7 +114,7 @@ export class MultiProviderAuthDialog extends ComponentDialog {
107114

108115
private async promptForProvider(stepContext: WaterfallStepContext): Promise<DialogTurnResult> {
109116
if (this.authenticationConnections.length === 1) {
110-
const result: string = this.authenticationConnections[0].name;
117+
const result: string = this.authenticationConnections[0].name + '_' + i18next.language.split('-')[0];
111118

112119
return await stepContext.next(result);
113120
}

sdk/typescript/libraries/bot-solutions/src/authentication/resources/AuthenticationResponses.de.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@
4343
}
4444
],
4545
"inputHint": "expectingInput"
46+
},
47+
"LoginButton": {
48+
"replies": [
49+
{
50+
"text": "Einloggen",
51+
"speak": "Einloggen"
52+
}
53+
],
54+
"inputHint": "acceptingInput"
55+
},
56+
"LoginPrompt": {
57+
"replies": [
58+
{
59+
"text": "Melden Sie sich in Ihrem {authType}-Konto an",
60+
"speak": "Melden Sie sich in Ihrem {authType}-Konto an"
61+
}
62+
],
63+
"inputHint": "acceptingInput"
4664
}
4765
}

sdk/typescript/libraries/bot-solutions/src/authentication/resources/AuthenticationResponses.es.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@
4343
}
4444
],
4545
"inputHint": "expectingInput"
46+
},
47+
"LoginButton": {
48+
"replies": [
49+
{
50+
"text": "Iniciar sesión",
51+
"speak": "Iniciar sesión"
52+
}
53+
],
54+
"inputHint": "acceptingInput"
55+
},
56+
"LoginPrompt": {
57+
"replies": [
58+
{
59+
"text": "Inicia sesión en tu cuenta de {authType}",
60+
"speak": "Inicia sesión en tu cuenta de {authType}"
61+
}
62+
],
63+
"inputHint": "acceptingInput"
4664
}
4765
}

sdk/typescript/libraries/bot-solutions/src/authentication/resources/AuthenticationResponses.fr.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@
4343
}
4444
],
4545
"inputHint": "expectingInput"
46+
},
47+
"LoginButton": {
48+
"replies": [
49+
{
50+
"text": "s'identifier",
51+
"speak": "s'identifier"
52+
}
53+
],
54+
"inputHint": "acceptingInput"
55+
},
56+
"LoginPrompt": {
57+
"replies": [
58+
{
59+
"text": "connectez-vous à votre compte {authType}",
60+
"speak": "connectez-vous à votre compte {authType}"
61+
}
62+
],
63+
"inputHint": "acceptingInput"
4664
}
4765
}

sdk/typescript/libraries/bot-solutions/src/authentication/resources/AuthenticationResponses.it.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@
4343
}
4444
],
4545
"inputHint": "expectingInput"
46+
},
47+
"LoginButton": {
48+
"replies": [
49+
{
50+
"text": "Accesso",
51+
"speak": "Accesso"
52+
}
53+
],
54+
"inputHint": "acceptingInput"
55+
},
56+
"LoginPrompt": {
57+
"replies": [
58+
{
59+
"text": "Accedi al tuo account {authType}",
60+
"speak": "Accedi al tuo account {authType}"
61+
}
62+
],
63+
"inputHint": "acceptingInput"
4664
}
4765
}

sdk/typescript/libraries/bot-solutions/src/authentication/resources/AuthenticationResponses.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@
4343
}
4444
],
4545
"inputHint": "acceptingInput"
46+
},
47+
"LoginButton": {
48+
"replies": [
49+
{
50+
"text": "Login",
51+
"speak": "Login"
52+
}
53+
],
54+
"inputHint": "acceptingInput"
55+
},
56+
"LoginPrompt": {
57+
"replies": [
58+
{
59+
"text": "Sign in to your {authType} account",
60+
"speak": "Sign in to your {authType} account"
61+
}
62+
],
63+
"inputHint": "acceptingInput"
4664
}
4765
}

sdk/typescript/libraries/bot-solutions/src/authentication/resources/AuthenticationResponses.zh.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@
4343
}
4444
],
4545
"inputHint": "expectingInput"
46+
},
47+
"LoginButton": {
48+
"replies": [
49+
{
50+
"text": "登錄",
51+
"speak": "登錄"
52+
}
53+
],
54+
"inputHint": "acceptingInput"
55+
},
56+
"LoginPrompt": {
57+
"replies": [
58+
{
59+
"text": "登錄到您的{authType}帳戶",
60+
"speak": "登錄到您的{authType}帳戶"
61+
}
62+
],
63+
"inputHint": "acceptingInput"
4664
}
4765
}

sdk/typescript/libraries/bot-solutions/src/responses/responseManager.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ export class ResponseManager {
5656
return this.parseResponse(template, tokens);
5757
}
5858

59+
/**
60+
* Gets a simple response from template with Text, Speak, InputHint, and SuggestedActions set.
61+
* @param templateId The name of the response template.
62+
* @param locale The locale for the response template.
63+
* @param tokens string map of tokens to replace in the response.
64+
* @returns An Activity.
65+
*/
66+
public getLocalizedResponse(templateId: string, locale: string, tokens?: Map<string, string>): Partial<Activity> {
67+
const template: ResponseTemplate = this.getResponseTemplate(templateId, locale);
68+
69+
// create the response the data items
70+
return this.parseResponse(template, tokens);
71+
}
72+
5973
/**
6074
* Gets the Text of a response.
6175
* @param templateId The name of the response template.

0 commit comments

Comments
 (0)