Skip to content

Commit

Permalink
update translate
Browse files Browse the repository at this point in the history
  • Loading branch information
khanh1902 committed Jun 30, 2024
1 parent 0fa8c06 commit c0607f6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
8 changes: 4 additions & 4 deletions actions/HTTPRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);

Expand Down
12 changes: 9 additions & 3 deletions services/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
Expand Down
23 changes: 20 additions & 3 deletions utils/prompts.js
Original file line number Diff line number Diff line change
@@ -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: {} }
Expand Down Expand Up @@ -77,6 +79,7 @@ const getExtendTypeMessage = async (contents, conversationData) => {
const quickReplyData = await formatQuickReply(
channelId,
contents.buttons,
variables,
contents.language,
language
);
Expand All @@ -98,24 +101,32 @@ 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
);
break;
case 'WEB':
result = await formatQuickReplyWEB(
buttons,
variables,
currentLanguage,
defaultLanguage
);
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 });
}
}
Expand Down

0 comments on commit c0607f6

Please sign in to comment.