Permalink
Browse files

introduces translateHelper.getAllAlternatives() to return all alterna…

…tives for a key instead of a sample one
  • Loading branch information...
antoniusostermann committed Nov 15, 2018
1 parent 2cfb8fa commit aa72ac72aaf36e0216a61840b8a25562859561ab
@@ -1,4 +1,5 @@
version 0.4.0
- introduces translateHelper's getAllAlternatives() method which returns all alternatives instead of a sample one. Useful for suggestion chips etc and as an alternative to getTranslationValuesFor.
- changed the "assistant" binary to load the new application-initializer.js instead of index.js. To migrate, install the new assistant-source version globally.
- the entry files for utterances/translations/entities now may be either .json, .js or .ts files
- now supports generation of custom entities
@@ -6,17 +6,15 @@ describe("TranslateHelper", function() {
configureI18nLocale(this.container, false);
createRequestScope(this.specHelper);
this.stateMachine = this.container.inversifyInstance.get("core:state-machine:current-state-machine");
});
describe("t", function() {
beforeEach(function() {
this.context = this.container.inversifyInstance.get("core:i18n:current-context");
this.context.intent = "testIntent";
this.context.state = "mainState";
this.context = this.container.inversifyInstance.get("core:i18n:current-context");
this.context.intent = "testIntent";
this.context.state = "mainState";
this.translateHelper = this.container.inversifyInstance.get("core:i18n:current-translate-helper");
});
this.translateHelper = this.container.inversifyInstance.get("core:i18n:current-translate-helper");
});
describe("t", function() {
it("supports explicit keys", async function() {
expect(await this.translateHelper.t("mySpecificKeys.keyOne")).toEqual("keyOneResult");
});
@@ -213,4 +211,30 @@ describe("TranslateHelper", function() {
});
});
});
describe("getAllAlternatives", function() {
describe("with array syntax", function() {
it("returns all alternatives as array", async function() {
expect(await this.translateHelper.getAllAlternatives("getAllAlternatives.withArray")).toEqual(["alternative 1", "alternative 2"]);
});
});
describe("with templating syntax", function() {
it("returns all alternatives as array", async function() {
expect(await this.translateHelper.getAllAlternatives("getAllAlternatives.withTemplating")).toEqual(["one", "two"]);
});
});
describe("with mixed syntax", function() {
it("returns all alternatives as array", async function() {
expect(await this.translateHelper.getAllAlternatives("getAllAlternatives.withBoth")).toEqual(["alternative 1", "alternative 2", "alternative 3"]);
});
});
describe("with only one entry", function() {
it("returns this entry as array", async function() {
expect(await this.translateHelper.getAllAlternatives("getAllAlternatives.withOne")).toEqual(["no other alternatives"]);
});
});
});
});
@@ -99,5 +99,17 @@
"intentB": "FilterCState - filterTestBIntent"
}
},
"noInterpolation": "no interpolation"
"noInterpolation": "no interpolation",
"getAllAlternatives": {
"withOne": "no other alternatives",
"withArray": [
"alternative 1",
"alternative 2"
],
"withTemplating": "{one|two}",
"withBoth": [
"alternative 1",
"alternative {2|3}"
]
}
}
@@ -1,12 +1,24 @@
export const processor = {
type: "postProcessor",
name: "arrayReturnsSample",
process(value: string, key, options, translator) {
// tslint:disable-next-line:variable-name
process(value: string, _key, options, _translator) {
const dialogOptions = value.split(arraySplitter);
// Return all dialog options if called by getAllAlternatives()
if (options && options[optionsObjectName] && options[optionsObjectName][optionEnablingArrayReturn] === true) {
// Return it joined with arraySplitter to make is usable by other plugins
return dialogOptions.join(arraySplitter);
}
if (dialogOptions.length === 1) return value;
// Return sample option as default
return dialogOptions[Math.floor(Math.random() * dialogOptions.length)]; // Returns random element
},
};
export const arraySplitter = "||||||||||||||||||||||||||||||";
export const optionsObjectName = "__assistantJsOptions";
export const optionEnablingArrayReturn = "enableArrayReturn";
@@ -1,7 +1,7 @@
import { Configuration } from "./private-interfaces";
/** Uses I18next to get translations for keys */
export interface TranslateHelper {
/** Describes translateHelper's t() method */
export interface ShortT {
/**
* Translates the given key using your json translations and a convention-over-configuration-approach.
* First try is `currentState.currentIntent.platform.device`.
@@ -19,6 +19,28 @@ export interface TranslateHelper {
t(key?: string, locals?: { [name: string]: string | number | object }): Promise<string>;
}
/** Uses I18next to get translations for keys */
export interface TranslateHelper extends ShortT {
/**
* Works the same as regular t(), but returns a list of all examples instead of a sample one.
* Translates the given key using your json translations.
* @param key String of the key to look for. If you pass a relative key (beginning with '.'),
* this method will apply several conventions, first looking for a translation for "currentState.currentIntent.KEY.platform.device".
* If you pass an absolute key (without "." at beginning), this method will look at given absolute key.
* @param locals Variables to use in reponse
* @return string array of all alternatives
*/
getAllAlternatives(key?: string, locals?: { [name: string]: string | number | object }): Promise<string[]>;
/**
* Works the same as regular t(), but returns a list of all examples instead of a sample one.
* Translates the given key using your json translations and a convention-over-configuration-approach.
* First try is `currentState.currentIntent.platform.device`.
* @param locals If given: variables to use in response
*/
getAllAlternatives(locals?: { [name: string]: string | number | object }): Promise<string[]>;
}
export interface InterpolationResolver {
/**
* resolves all missing interpolations in the given translation iteratively by executing missingInterpolation extensions
@@ -7,6 +7,7 @@ import { MinimalRequestExtraction, OptionalExtractions } from "../unifier/public
import { componentInterfaces } from "./component-interfaces";
import { I18nContext } from "./context";
import { arraySplitter, optionEnablingArrayReturn, optionsObjectName } from "./plugins/array-returns-sample.plugin";
import { InterpolationResolver, MissingInterpolationExtension, TranslateHelper as TranslateHelperInterface } from "./public-interfaces";
@injectable()
@@ -93,6 +94,19 @@ export class TranslateHelper implements TranslateHelperInterface {
return this.interpolationResolver.resolveMissingInterpolations(translatedValue, this);
}
public async getAllAlternatives(key?: string, locals?: {});
public async getAllAlternatives(key: {});
public async getAllAlternatives(key?: string | {}, locals = {}): Promise<string[]> {
// Set internal assistantjs option for array-returns-sample.plugin
locals[optionsObjectName] = { [optionEnablingArrayReturn]: true };
// Get regular translation string. Multiple translations are concatenated by arraySplitter per default...
const translation = await this.t(key as any, locals);
// ... so we have to split to return in array format
return translation.split(arraySplitter);
}
/**
* Finds first existing locale or throws exception if none of the lookups exist.
* i18n.exists() won't work here: it returns true for keys returning an object, even if returnObjectTrees is false. t() then returns undefined.
@@ -1,5 +1,5 @@
import { injectable, unmanaged } from "inversify";
import { TranslateHelper } from "../i18n/public-interfaces";
import { ShortT, TranslateHelper } from "../i18n/public-interfaces";
import { Logger } from "../root/public-interfaces";
import { featureIsAvailable } from "../unifier/feature-checker";
import { MinimalRequestExtraction, OptionalExtractions, Voiceable } from "../unifier/public-interfaces";
@@ -14,7 +14,7 @@ import { State, Transitionable } from "./public-interfaces";
@injectable()
export abstract class BaseState<MergedAnswerTypes extends BasicAnswerTypes, MergedHandler extends BasicHandable<MergedAnswerTypes>>
implements State.Required, Voiceable, TranslateHelper {
implements State.Required, Voiceable, ShortT {
/** Current response handler */
public responseHandler: MergedHandler;

0 comments on commit aa72ac7

Please sign in to comment.