Skip to content

Commit

Permalink
detect
Browse files Browse the repository at this point in the history
  • Loading branch information
transkarpation committed Mar 13, 2024
1 parent 6735cd4 commit 3b78f67
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion bots/translateBot/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,29 @@ export const updateApiData = async (id, data) => {
export const translateText = async(text) => {
const API_KEY = process.env.GOOGLE_TOKEN;

const langsMap = {
'en': 'es',
'es': 'en'
}

try{
let detectResponse = await axios.post(`https://translation.googleapis.com/language/translate/v2/detect?key=${API_KEY}`)

let lang = ''

if (detectResponse.data.data.detections[0]) {
lang = detectResponse.data.data.detections[0][0].language
}

let target = langsMap[lang]

if (!target) {
target = 'es'
}

let res = await axios.post(
`https://translation.googleapis.com/language/translate/v2?key=${API_KEY}`,
{ q: text, target: "es" }
{ q: text, target: target }
);
return res.data.data.translations[0].translatedText;
}catch (error) {
Expand Down

0 comments on commit 3b78f67

Please sign in to comment.