我正在使用nodeJS和此google网址来翻译查询: 从英语到阿拉伯语工作正常
http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=ar&dt=t&q=Hello
但将网址翻译从阿拉伯语更改为英语时
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"
]
答案 0 :(得分:2)
试试这个。它对我有用
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);
});