Search code examples
javascriptnode.jsgoogle-translategoogle-translation-api

unable to translate from Arabic to English Google translate free api


I am using nodeJS and this google URL to translate query: from English to Arabic works fine

http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=ar&dt=t&q=Hello

but when changing URL Translation from Arabic to English

http://translate.googleapis.com/translate_a/single?client=gtx&sl=ar&tl=en&dt=t&q=مرحبا

returns invalid output "E1 ('" as a translation

while hitting above URL from the browser will return correct output, Here is my code

const request = require('request');

let endPoint = null;

if (language == 'english') {
    endPoint = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=ar&tl=en&dt=t&ie=UTF-8&oe=UTF-8&q="+text;
} else {
    endPoint = 'http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=ar&dt=t&q=' + text;
}
return request(endPoint, function (error, response, body) {
    console.log(body);
});

Below is the output

[
  [
    [
        "مرحبا",
        "Hello",
        null,
        null,
        1
    ]
  ],
  null,
  "en"
]




[
  [
    [
        "E1 ('",
        "E1-('",
        null,
        null,
        3
    ]
  ],
  null,
  "ar"
]

Solution

  • Try this. It worked for me

    let text = "مرحب"
    let endPoint = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=ar&tl=en&dt=t&ie=UTF-8&oe=UTF-8&q=' + encodeURIComponent(text);
    request(endPoint, function (error, response, body) {
        console.log(body);
    });