From 2e2e59044ef65945adfcff060a5e962c49a55033 Mon Sep 17 00:00:00 2001 From: Kevin Cooper Date: Sat, 27 Jul 2019 09:41:01 -0700 Subject: [PATCH 1/2] Support i18n via text transform hook --- packages/botkit/src/conversation.ts | 39 ++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/botkit/src/conversation.ts b/packages/botkit/src/conversation.ts index 0450f7198..4d9840e5b 100644 --- a/packages/botkit/src/conversation.ts +++ b/packages/botkit/src/conversation.ts @@ -113,6 +113,7 @@ export class BotkitConversation extends Dialog { private _beforeHooks: {}; private _afterHooks: { (context: TurnContext, results: any): void }[]; private _changeHooks: {}; + private _transformHooks: {}; private _controller: Botkit; /** @@ -126,6 +127,7 @@ export class BotkitConversation extends Dialog { this._beforeHooks = {}; this._afterHooks = []; this._changeHooks = {}; + this._transformHooks = {}; this.script = {}; this._controller = controller; @@ -520,6 +522,34 @@ export class BotkitConversation extends Dialog { } } + /** + * Bind a function to transform message text before performing template replacement. + * Can be used for i18n, for example. + * + * ```javascript + * convo.say('Hello, {{vars.name}}!'); + * convo.transform('text', (text, vars) { + * return text.replace('Hello', 'Hola'); + * }); + * ``` + * + * ```javascript + * convo.say('translation.hello'); + * convo.transform('text', (text, vars) { + * return i18n.t(text, vars); + * }); + * ``` + * @param type type of data to transform (currently supports: text) + * @param handler a handler function that can be used to change the value + */ + public transform(type: string, handler: (text, vars) => string): void { + if (!this._transformHooks[type]) { + this._transformHooks[type] = []; + } + + this._transformHooks[type].push(handler); + } + /** * Called automatically when a dialog begins. Do not call this directly! * @ignore @@ -773,7 +803,7 @@ export class BotkitConversation extends Dialog { } outgoing.channelData = outgoing.channelData ? outgoing.channelData : {}; - + /*******************************************************************************************************************/ // allow dynamic generation of quick replies and/or attachments if (typeof(line.quick_replies)=='function') { @@ -822,6 +852,13 @@ export class BotkitConversation extends Dialog { outgoing.channelData[key] = line.channelData[key]; } + // Transform the text BEFORE doing token replacement, e.g. to allow i18n. + if (this._transformHooks['text']) { + this._transformHooks['text'].forEach((hook) => { + outgoing.text = hook(outgoing.text, vars); + }) + } + /*******************************************************************************************************************/ // Handle template token replacements if (outgoing.text) { From cd13e4b9524f2bcdecbfcb318650ed3f057c15ea Mon Sep 17 00:00:00 2001 From: Kevin Cooper Date: Sun, 28 Jul 2019 10:57:21 -0700 Subject: [PATCH 2/2] Replace transform hook with simple getter fn --- packages/botkit/src/conversation.ts | 42 ++++------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) diff --git a/packages/botkit/src/conversation.ts b/packages/botkit/src/conversation.ts index 4d9840e5b..6f421cb35 100644 --- a/packages/botkit/src/conversation.ts +++ b/packages/botkit/src/conversation.ts @@ -113,7 +113,6 @@ export class BotkitConversation extends Dialog { private _beforeHooks: {}; private _afterHooks: { (context: TurnContext, results: any): void }[]; private _changeHooks: {}; - private _transformHooks: {}; private _controller: Botkit; /** @@ -127,7 +126,6 @@ export class BotkitConversation extends Dialog { this._beforeHooks = {}; this._afterHooks = []; this._changeHooks = {}; - this._transformHooks = {}; this.script = {}; this._controller = controller; @@ -522,34 +520,6 @@ export class BotkitConversation extends Dialog { } } - /** - * Bind a function to transform message text before performing template replacement. - * Can be used for i18n, for example. - * - * ```javascript - * convo.say('Hello, {{vars.name}}!'); - * convo.transform('text', (text, vars) { - * return text.replace('Hello', 'Hola'); - * }); - * ``` - * - * ```javascript - * convo.say('translation.hello'); - * convo.transform('text', (text, vars) { - * return i18n.t(text, vars); - * }); - * ``` - * @param type type of data to transform (currently supports: text) - * @param handler a handler function that can be used to change the value - */ - public transform(type: string, handler: (text, vars) => string): void { - if (!this._transformHooks[type]) { - this._transformHooks[type] = []; - } - - this._transformHooks[type].push(handler); - } - /** * Called automatically when a dialog begins. Do not call this directly! * @ignore @@ -786,6 +756,11 @@ export class BotkitConversation extends Dialog { let outgoing; let text = ''; + // If text is a function, call the function to get the actual text value. + if (typeof line.text === 'function') { + text = await line.text(vars); + } + // if the text is just a string, use it. // otherwise, if it is an array, pick a random element if (line.text && typeof(line.text)=='string') { @@ -852,13 +827,6 @@ export class BotkitConversation extends Dialog { outgoing.channelData[key] = line.channelData[key]; } - // Transform the text BEFORE doing token replacement, e.g. to allow i18n. - if (this._transformHooks['text']) { - this._transformHooks['text'].forEach((hook) => { - outgoing.text = hook(outgoing.text, vars); - }) - } - /*******************************************************************************************************************/ // Handle template token replacements if (outgoing.text) {