Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
How to return translated text JavaScript google Translate API

I wrote the following code to translate from Arabic to English, I want the function to accept English as source and return the translated text,

can some one help me on this?

translateArtoEn(source) {
let url = `https://translation.googleapis.com/language/translate/v2?key=${API_KEY}`;
url += '&q=' + encodeURI(source);
url += `&source=ar`;
url += `&target=en`;

console.log(url)

    fetch(url, {
        method: 'GET',
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json"
        }
    })
    .then(res => res.json())
    .then((response) => {
      return response.data.translations[0]["translatedText"]
    })
    .catch(error => {
       console.log("There was an error with the translation request: ", error);
    });
}

 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The code you provided will not return any value since I assume the function is synchronous.

With this, you may use callback function to return the desired result after the parent function has already returned.

Please see below code for your reference:

const fetch = require('node-fetch');

function main(){
        var source = '<your_desired_text_to_translate>';
        translateArtoEn(source,myDisplayer);
}

function translateArtoEn(source,callback){
    var API_KEY = '<your_API_key>';
    let url = `https://translation.googleapis.com/language/translate/v2?key=${API_KEY}`;
    url += '&q=' + encodeURI(source);
    url += `&source=ar`;
    url += `&target=en`;

    fetch(url, {
        method: 'GET',
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json"
        }
    })
    .then(res => res.json())
    .then((response) => {
            callback(response.data.translations[0].translatedText)
    })
    .catch(error => {
       console.log("There was an error with the translation request: ", error);
    });
}

function myDisplayer(result) {
        // handle the result here
        console.log("Translated text: ",result);
}

if (require.main === module) {
  main();
}

In this code, callback is used to pass the returned value (after the execution of the parent function is finished) to another function that will handle the result.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda