Skip to content

Commit

Permalink
Merge branch 'master' into fixIssue2079
Browse files Browse the repository at this point in the history
  • Loading branch information
songcs authored Jun 16, 2020
2 parents e7b3109 + 1af14c9 commit 4005bd6
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "../../../../schemas/sdk.schema",
"$kind": "Microsoft.Test.Script",
"dialog":
{
"$kind": "Microsoft.AdaptiveDialog",
"generator": {
"$kind": "Microsoft.ResourceMultiLanguageGenerator",
"resourceId": "test.lg",
"languagePolicy":
{
"en-us": ["xxx", "fr"]
}
},
"triggers": [
{
"$kind": "Microsoft.OnBeginDialog",
"actions": [
{
"$kind": "Microsoft.SendActivity",
"activity": "${test()}"
}
]
}
]
},
"script": [
{
"$kind": "Microsoft.Test.UserConversationUpdate"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "french"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ describe('ActionScopeTests', function() {
it('No Language Generator', async () => {
await testRunner.runTestScript('NoLanguageGeneration');
});

it('Customize Language Policy', async () => {
await testRunner.runTestScript('CustomizeLanguagePolicy');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,45 @@ import {LanguagePolicy} from '../languagePolicy';
* This class automatically updates the cache when resource change events occure.
*/
export abstract class MultiLanguageGeneratorBase implements LanguageGenerator{
public languagePolicy = LanguagePolicy.defaultPolicy;
public languagePolicy: any;

public abstract tryGetGenerator(context: TurnContext, locale: string): {exist: boolean; result: LanguageGenerator};

public constructor() {};
public constructor(languagePolicy: any = undefined) {
this.languagePolicy = languagePolicy;
};

public async generate(turnContext: TurnContext, template: string, data: object): Promise<string> {
const targetLocale = turnContext.activity.locale? turnContext.activity.locale.toLocaleLowerCase() : '';
let locales: string[] = [''];
if (this.languagePolicy[targetLocale] === undefined) {
if (this.languagePolicy[''] === undefined) {
throw Error(`No supported language found for ${ targetLocale }`);

// priority
// 1. local policy
// 2. shared policy in turnContext
// 3. default policy
if (!this.languagePolicy) {
this.languagePolicy = turnContext.turnState.get('languagePolicy');
if (!this.languagePolicy) {
this.languagePolicy = LanguagePolicy.defaultPolicy;
}
} else {
locales = this.languagePolicy[targetLocale];
}

// see if we have any locales that match
let fallbackLocales = [];
if (targetLocale in this.languagePolicy) {
this.languagePolicy[targetLocale].forEach((u: string): number => fallbackLocales.push(u));
}

// append empty as fallback to end
if (targetLocale !== '' && '' in this.languagePolicy) {
this.languagePolicy[''].forEach((u: string): number => fallbackLocales.push(u));
}

if (fallbackLocales.length === 0) {
throw Error(`No supported language found for ${ targetLocale }`);
}

const generators: LanguageGenerator[] = [];
for (const locale of locales) {
for (const locale of fallbackLocales) {
if (this.tryGetGenerator(turnContext, locale).exist) {
generators.push(this.tryGetGenerator(turnContext, locale).result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { LanguageGeneratorManager } from './languageGeneratorManager';
export class ResourceMultiLanguageGenerator extends MultiLanguageGeneratorBase {
public resourceId: string;

public constructor(resourceId: string = undefined) {
super();
public constructor(resourceId: string = undefined, languagePolicy: any = undefined) {
super(languagePolicy);
this.resourceId = resourceId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ export class LanguageGeneratorMiddleWare implements Middleware {
private readonly _defaultLg: string;
private readonly languageGeneratorManagerKey = 'LanguageGeneratorManager';
private readonly languageGeneratorKey = 'LanguageGenerator';
private readonly languagePolicyKey = 'languagePolicy';
private _languageGeneratorManager: LanguageGeneratorManager;
private _languageGenerator: LanguageGenerator;
private _languagePolicy: object;

public constructor(resourceExpolrer: ResourceExplorer = undefined, defaultLg: string = undefined) {
public constructor(resourceExpolrer: ResourceExplorer = undefined, defaultLg: string = undefined, defaultLanguagePolicy = undefined) {
this._resourceExplorer = resourceExpolrer? resourceExpolrer : new ResourceExplorer();
this._defaultLg = defaultLg? defaultLg : 'main.lg';
this._languagePolicy = defaultLanguagePolicy;
}

/**
Expand Down Expand Up @@ -59,6 +62,10 @@ export class LanguageGeneratorMiddleWare implements Middleware {
} else{
context.turnState.set(this.languageGeneratorKey, this._languageGenerator);
}

if (this._languagePolicy !== undefined) {
context.turnState.set(this.languagePolicyKey, this._languagePolicy);
}

if (next) {
await next();
Expand Down
13 changes: 10 additions & 3 deletions libraries/botbuilder-dialogs-adaptive/src/languagePolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ export class LanguagePolicy {
// "en" -> ""
// So that when we get a locale such as en-gb, we can try to resolve to "en-gb" then "en" then ""
// See commented section for full sample of output of this function
public static getDefaultPolicy(): any{
public static getDefaultPolicy(defaultLanguages?: string[]): any{
var result = {};
if (defaultLanguages === undefined) {
defaultLanguages = [''];
}

for (const locale of LanguagePolicy.locales) {
let lang = locale.toLowerCase();
const fallback: string[] = [];
Expand All @@ -93,8 +97,11 @@ export class LanguagePolicy {
break;
}
}

fallback.push('');

if (locale === '') {
defaultLanguages.forEach((u): number => fallback.push(u));
}

result[locale] = fallback;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ export class MultiLanguageRecognizer implements Recognizer {

public async recognize(dialogContext: DialogContext, activity: Activity): Promise<RecognizerResult> {
const locale = activity.locale || '';
let policy: string[];
let policy: string[] = [];
if (this.languagePolicy.hasOwnProperty(locale)) {
policy = this.languagePolicy[locale];
} else {
policy = [''];
this.languagePolicy[locale].forEach((u: string): number => policy.push(u));
}

if (locale !== '' && this.languagePolicy.hasOwnProperty('')) {
// we now explictly add defaultPolicy instead of coding that into target's policy
this.languagePolicy[''].forEach((u: string): number => policy.push(u));
}

for (let i = 0; i < policy.length; i++) {
Expand Down

0 comments on commit 4005bd6

Please sign in to comment.