From c0607f6e2fd338d03497fe92440acf07899d7b8e Mon Sep 17 00:00:00 2001 From: khanh Date: Mon, 1 Jul 2024 03:54:19 +0700 Subject: [PATCH] update translate --- actions/HTTPRequest.js | 8 ++++---- services/translate.js | 12 +++++++++--- utils/prompts.js | 23 ++++++++++++++++++++--- utils/utils.js | 10 ++-------- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/actions/HTTPRequest.js b/actions/HTTPRequest.js index 9d99694..4b203ae 100644 --- a/actions/HTTPRequest.js +++ b/actions/HTTPRequest.js @@ -26,10 +26,10 @@ class HttpRequest extends ComponentDialog { let config = { method: method, url: replaceData({ text: url, data: conversationData.variables }), - data: replaceObjWithParam(conversationData.variables, keyValueToObject(body) || body), - headers: replaceObjWithParam(conversationData.variables, keyValueToObject(headers) || headers), - params: replaceObjWithParam(conversationData.variables, keyValueToObject(params) || params), - }; + data: replaceObjWithParam(conversationData.variables, body), + headers: replaceObjWithParam(conversationData.variables, headers), + params: replaceObjWithParam(conversationData.variables, params), + } console.log(`[HTTP] ${name} ${JSON.stringify(config)}`); diff --git a/services/translate.js b/services/translate.js index e8f2349..1490549 100644 --- a/services/translate.js +++ b/services/translate.js @@ -2,7 +2,7 @@ const { default: axios } = require('axios'); const translate = async (text, fromLang = 'auto', toLang = 'en') => { if (!text || fromLang == toLang) return text; - + try { let config = { method: 'get', @@ -19,9 +19,15 @@ const translate = async (text, fromLang = 'auto', toLang = 'en') => { throw new Error('Can not translate text'); } - console.log(`Translated: ${fromLang} -> ${toLang} | ${text} -> ${data[0][0][0]}`); + const filterTranslateValue = data[0].map(d => d[0]).join('') || data[0][0][0]; + + const replaceInside = filterTranslateValue.replace(/\\\|/g, ''); + + console.log( + `Translated: ${fromLang} -> ${toLang} | ${text} -> ${replaceInside}` + ); - return data[0][0][0]; + return replaceInside; } catch (error) { console.log('Translate filed - ', error.message); console.log(error.response && error.response.data); diff --git a/utils/prompts.js b/utils/prompts.js index ee0fbac..a2c2f2d 100644 --- a/utils/prompts.js +++ b/utils/prompts.js @@ -1,10 +1,12 @@ const { translate } = require('../services/translate'); const Cards = require('./cards'); +const { replaceData } = require('./utils'); const formatMessage = ({ data, type, conversationData }) => { if (!conversationData) return; - if (!type || !['text', 'image'].includes(type)) return { type: 'message', text: '', channelData: {} }; + if (!type || !['text', 'image'].includes(type)) + return { type: 'message', text: '', channelData: {} }; return type === 'text' ? { type: 'message', text: data, channelData: {} } @@ -77,6 +79,7 @@ const getExtendTypeMessage = async (contents, conversationData) => { const quickReplyData = await formatQuickReply( channelId, contents.buttons, + variables, contents.language, language ); @@ -98,17 +101,24 @@ const getExtendTypeMessage = async (contents, conversationData) => { const formatQuickReply = async ( channelId, buttons, + variables, currentLanguage, defaultLanguage ) => { let result = []; switch (channelId) { case 'LIN': - result = formatQuickReplyLIN(buttons, currentLanguage, defaultLanguage); + result = await formatQuickReplyLIN( + buttons, + variables, + currentLanguage, + defaultLanguage + ); break; case 'MSG': result = await formatQuickReplyMSG( buttons, + variables, currentLanguage, defaultLanguage ); @@ -116,6 +126,7 @@ const formatQuickReply = async ( case 'WEB': result = await formatQuickReplyWEB( buttons, + variables, currentLanguage, defaultLanguage ); @@ -127,12 +138,14 @@ const formatQuickReply = async ( const formatQuickReplyMSG = async ( buttons, + variables, currentLanguage, defaultLanguage ) => { let result = []; for (let button of buttons) { try { + button.label = replaceData({text: button.label, data: variables}) const translateLabel = currentLanguage !== defaultLanguage ? await translate(button.label, currentLanguage, defaultLanguage) @@ -152,12 +165,14 @@ const formatQuickReplyMSG = async ( const formatQuickReplyLIN = async ( buttons, + variables, currentLanguage, defaultLanguage ) => { let result = []; for (let button of buttons) { try { + button.label = replaceData({text: button.label, data: variables}) const translateLabel = currentLanguage !== defaultLanguage ? await translate(button.label, currentLanguage, defaultLanguage) @@ -180,14 +195,16 @@ const formatQuickReplyLIN = async ( const formatQuickReplyWEB = async ( buttons, + variables, currentLanguage, defaultLanguage ) => { let result = []; if (!Array.isArray(buttons)) return result; - for (const button of buttons) { + for (let button of buttons) { try { + button.label = replaceData({text: button.label, data: variables}) const translateLabel = currentLanguage !== defaultLanguage ? await translate(button.label, currentLanguage, defaultLanguage) diff --git a/utils/utils.js b/utils/utils.js index 71b47c3..d784b0c 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -20,7 +20,7 @@ const replaceData = ({ text, data }) => { let value = data.find((item) => item.name === keys[0]).value || undefined; - if (keys.length === 1) return value; + if (keys.length <= 1) return typeof value === 'object' ? JSON.stringify(value) : value; return ( keys @@ -64,13 +64,7 @@ const replaceObjWithParam = (conversationData, obj) => { try { for (let key of arr) { - if ( - obj[key] && - typeof obj[key] == 'string' && - obj[key].match(/^{[\w->]+}$/) - ) { - obj[key] = accessProp(obj[key].replace(/{|}/g, ''), conversationData); - } else if (obj[key] && typeof obj[key] == 'string') { + if (obj[key] && typeof obj[key] == 'string') { obj[key] = replaceData({ text: obj[key], data: conversationData }); } }