In a recent project, a translation function needs to be done to access the API of Baidu Translate for translation. Now the steps are organized as follows:
The following is an excerpt from Baidu's official documentation:
HTTP access documentation
Baidu Translate API provides multilingual translation services to the outside world through the HTTP interface. You only need to call the Baidu Translate API, pass in the content to be translated, and specify the source language to be translated (supports automatic detection of the source language) and the target language type, and you can get the corresponding translation result.
Translate API HTTP address:
http://api.fanyi.baidu.com/api/trans/vip/translate
Translate API HTTPS address:
https://fanyi-api.baidu.com/api/trans/vip/translate
You need to send the following fields to this address via POST or GET method to access the service
field name | Types of | Required parameters | describe | Remark |
---|---|---|---|---|
q | TEXT | AND | request translation query | UTF-8 encoding |
from | TEXT | AND | Translation source language | Language list (can be set to auto) |
to | TEXT | AND | target language | Language list (cannot be set to auto) |
appid | INT | AND | APP ID | Can be viewed in the admin console |
salt | INT | AND | random number | |
sign | TEXT | AND | sign | appid+q+salt+MD5 value of key |
The signature is a string generated by the MD5 algorithm to ensure the security of the call. The length of the generated signature is 32 bits, and the English characters in the signature are in lowercase format.
To ensure translation quality, please limit the length of a single request to 6000 bytes. (about 2000 Chinese characters)
The signature generation method is as follows:
1. Convert the APPID (appid) in the request parameters, translate the query (q, note that it is UTF-8 encoded), the random number (salt), and the key assigned by the platform (can be viewed in the management console )
String 1 is obtained by concatenating in the order of appid+q+salt+key.
2. Do md5 on string 1 to get a 32-bit lowercase sign.
Notice:
1. Please first convert the text to be translated into UTF-8 encoding
2、在发送HTTP请求之前需要对各字段做URL encode。
3、在生成签名拼接 appid+q+salt+密钥 字符串时,q不需要做URL encode,在生成签名之后,发送HTTP请求之前才需要对要发送的待翻译文本字段q做URL encode。
返回结果是json格式,包含以下字段:
字段名 | 类型 | 描述 |
---|---|---|
from | TEXT | 翻译源语言 |
to | TEXT | 译文语言 |
trans_result | MIXED LIST | 翻译结果 |
src | TEXT | 原文 |
dst | TEXT | 译文 |
其中 trans_result 包含了 src 和 dst 字段。
例:将apple从英文翻译成中文:
q=apple
from=en
to=zh
appid=2015063000000001
salt=1435660288
平台分配的密钥: 12345678
生成sign:
>拼接字符串1
拼接appid=2015063000000001+q=apple+salt=1435660288+密钥=12345678
得到字符串1 =2015063000000001apple143566028812345678
>计算签名sign(对字符串1做md5加密,注意计算md5之前,串1必须为UTF-8编码)
sign=md5(2015063000000001apple143566028812345678)
sign=f89f9594663708c1605f3d736d01d2d4
完整请求为:
也可以使用POST方法传送需要的参数。
当翻译结果无法正常返回时,请参考下表处理:
错误码 | 含义 | 解决方法 |
---|---|---|
52000 | 成功 | |
52001 | 请求超时 | 重试 |
52002 | 系统错误 | 重试 |
52003 | 未授权用户 | 检查您的appid是否正确 |
54000 | 必填参数为空 | 检查是否少传参数 |
58000 | 客户端IP非法 | 检查您填写的IP地址是否正确 可修改您填写的服务器IP地址 |
54001 | 签名错误 | 请检查您的签名生成方法 |
54003 | 访问频率受限 | 请降低您的调用频率 |
58001 | 译文语言方向不支持 | 检查译文语言是否在语言列表里 |
54004 | 账户余额不足 | 前往管理控制台为账户充值 |
54005 | 长query请求频繁 | 请降低长query的发送频率,3s后再试 |
源语言语种不确定时可设置为 auto,目标语言语种不可设置为 auto。
语言简写 | 名称 |
---|---|
auto | 自动检测 |
zh | 中文 |
en | 英语 |
yue | 粤语 |
wyw | 文言文 |
jp | 日语 |
kor | 韩语 |
fra | 法语 |
spa | 西班牙语 |
th | 泰语 |
ara | 阿拉伯语 |
ru | 俄语 |
pt | 葡萄牙语 |
de | 德语 |
it | 意大利语 |
el | 希腊语 |
nl | 荷兰语 |
pl | 波兰语 |
bul | 保加利亚语 |
est | 爱沙尼亚语 |
dan | 丹麦语 |
fin | 芬兰语 |
cs | 捷克语 |
rom | 罗马尼亚语 |
slo | 斯洛文尼亚语 |
swe | 瑞典语 |
hu | 匈牙利语 |
cht | 繁体中文 |
vie | 越南语 |