Skip to content

Commit

Permalink
add: dictionary of translate resulte from "Bing"
Browse files Browse the repository at this point in the history
  • Loading branch information
chunibyocola committed Jun 18, 2021
1 parent 34b2cc5 commit 1716827
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/public/translate/bing/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,49 @@ export const translate = async ({ text, from = '', to = '', preferredLanguage =
result: [data[0].translations[0].text]
};

let dict = undefined;
try {
if (!text.includes(' ') && (result.from === 'en' || to === 'en')) {
const dictRes = await fetchDictFromBing({ text, from: result.from, to, com });
const dictData = await dictRes.json();

const dictObject = dictData[0]?.translations.reduce((t, c) => ({ ...t, [c.posTag]: t[c.posTag] ? t[c.posTag].concat(c.normalizedTarget) : [c.normalizedTarget] }), {});
dict = dictObject && Object.keys(dictObject).map(v => `${v}: ${dictObject[v].join(', ')}`);
}
}
catch {
dict = undefined;
}

result.dict = dict;

return result;
} catch (err) {
throw getError(RESULT_ERROR);
}
};

const fetchDictFromBing = async ({ text, from, to, com }) => {
const url = `https://${com ? 'www' : 'cn'}.bing.com/tlookupv3`;

const { token, key } = await getTokenAndKey(com);

const searchParams = new URLSearchParams();
searchParams.append('from', from);
searchParams.append('text', text);
searchParams.append('to', to);
searchParams.append('token', token);
searchParams.append('key', key);

return await fetchData(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: searchParams.toString()
});
};

const fetchResultFromBing = async ({ text, from, to, com }) => {
const url = `https://${com ? 'www' : 'cn'}.bing.com/ttranslatev3`;

Expand Down

0 comments on commit 1716827

Please sign in to comment.