无法从阿拉伯语翻译成英语谷歌翻译免费API



我正在使用nodeJS和这个谷歌URL来翻译查询:从英语到阿拉伯语工作正常

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

但是当将URL翻译从阿拉伯语更改为英语时

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

返回无效的输出"E1 ('"作为翻译

从浏览器点击上面的URL将返回正确的输出,这是我的代码

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);
});

下面是输出

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


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

试试这个。它对我有用

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);
});

最新更新