帮助与文档 > 产品文档 > 自然语言翻译 > API文档 > 富文本翻译服务
富文本翻译服务

富文本翻译 API 简介

概念解释

富文本翻译:可对网页html标签自动识别,翻译所需内容。

说明

Hi,您好,欢迎使用有道智云网页翻译API接口服务。

本文档主要针对需要集成HTTP API的技术开发工程师,详细描述网页翻译能力相关的技术内容。

如果您有与我们商务合作的需求,可以通过以下方式联系我们:

商务邮箱: AIcloud_Business@corp.youdao.com

如果您对文档内容有任何疑问,可以通过以下几种方式联系我们:

客服QQ:1906538062

智云翻译技术交流QQ 1群: 652880659

智云翻译技术交流QQ 2群: 669384425

智云翻译技术交流QQ 3群: 807539209

智云翻译技术交流QQ 4群: 936752411

联系邮箱: zhiyun@corp.youdao.com

温馨提示:

  • 本文档主要针对开发人员,接入测试前需要获取 应用ID应用密钥 ,并绑定实例;如果您还没有,请按照 新手指南 获取。
  • 平台向每个账户赠送50元的体验金,供用户集成前测试所用,具体资费规则详见 富文本翻译服务报价

接口说明

网页翻译API接口提供有道的翻译服务,可对网页html标签自动识别,翻译所需内容,目前支持中英、中日、中韩双向共6个语言方向。

网页翻译API HTTPS地址:

https://openapi.youdao.com/translate_html

协议须知

调用方在集成网页翻译API时,请遵循以下规则。

规则描述
传输方式HTTPS
请求方式GET/POST
字符编码统一使用UTF-8 编码
请求格式表单
响应格式JSON

接口调用参数

调用API需要向接口发送以下字段来访问服务。

字段名类型含义必填备注
qtext待翻译文本True必须是UTF-8编码
fromtext源语言True参考下方 支持语言 (按量计费用户可不设置from的参数值,资源包计费客户必须设置from的参数值,不能默认为auto)
totext目标语言True参考下方 支持语言
appKeytext应用IDTrue可在 应用管理 查看
salttextUUIDTrueuuid,唯一通用识别码
signtext签名Truesha256(应用ID+input+salt+curtime+应用密钥)
signTypetext签名类型Truev3
curtimetext当前UTC时间戳(秒)trueTimeStamp

签名生成方法如下:
signType=v3;
sign=sha256(应用ID+input+salt+curtime+应用密钥);
其中,input的计算方式为:input=q前10个字符 + q长度 + q后10个字符(当q长度大于20)或 input=q字符串(当q长度小于等于20);

注意:

  1. 接口salt+curtime来防重放(即一个请求不可以被请求2次),所以salt最好为UUID。

不同语言获取时间戳,请参看此链接

如果对签名有疑问,可以参看各语言demo。

输出结果

正常返回

字段名含义
data翻译结果
errorCode错误码
errorMessage成功信息

异常返回

字段名含义
errorCode错误码
errorMessage错误信息

支持语言

下表为各语言对应代码:

语言代码
中文zh-CHS
中文繁体zh-CHT
英文en
日文ja
韩文ko
法文fr
西班牙文es
葡萄牙文pt
意大利文it
俄文ru
越南文vi
德文de
阿拉伯文ar
印尼文id
南非荷兰语af
波斯尼亚语bs
保加利亚语bg
粤语yue
加泰隆语ca
克罗地亚语hr
捷克语cs
丹麦语da
荷兰语nl
爱沙尼亚语et
斐济语fj
芬兰语fi
希腊语el
海地克里奥尔语ht
希伯来语he
印地语hi
白苗语mww
匈牙利语hu
斯瓦希里语sw
克林贡语tlh
拉脱维亚语lv
立陶宛语lt
马来语ms
马耳他语mt
挪威语no
波斯语fa
波兰语pl
克雷塔罗奥托米语otq
罗马尼亚语ro
塞尔维亚语(西里尔文)sr-Cyrl
塞尔维亚语(拉丁文)sr-Latn
斯洛伐克语sk
斯洛文尼亚语sl
瑞典语sv
塔希提语ty
泰语th
汤加语to
土耳其语tr
乌克兰语uk
乌尔都语ur
威尔士语cy
尤卡坦玛雅语yua
阿尔巴尼亚语sq
阿姆哈拉语am
亚美尼亚语hy
阿塞拜疆语az
孟加拉语bn
巴斯克语eu
白俄罗斯语be
宿务语ceb
科西嘉语co
世界语eo
菲律宾语tl
弗里西语fy
加利西亚语gl
格鲁吉亚语ka
古吉拉特语gu
豪萨语ha
夏威夷语haw
冰岛语is
伊博语ig
爱尔兰语ga
爪哇语jw
卡纳达语kn
哈萨克语kk
高棉语km
库尔德语ku
柯尔克孜语ky
老挝语lo
拉丁语la
卢森堡语lb
马其顿语mk
马尔加什语mg
马拉雅拉姆语ml
毛利语mi
马拉地语mr
蒙古语mn
缅甸语my
尼泊尔语ne
齐切瓦语ny
普什图语ps
旁遮普语pa
萨摩亚语sm
苏格兰盖尔语gd
塞索托语st
修纳语sn
信德语sd
僧伽罗语si
索马里语so
巽他语su
塔吉克语tg
泰米尔语ta
泰卢固语te
乌兹别克语uz
南非科萨语xh
意第绪语yi
约鲁巴语yo
南非祖鲁语zu
自动识别auto

错误代码列表

错误码含义
0请求成功
101缺少必填的参数,首先确保必填参数齐全,然后确认参数书写是否正确。
303服务内部错误
405鉴权失败
411访问频率受限,请稍后重试
500翻译失败,请参考errorMessage

errorMessage说明:

错误码含义
11不支持的语言方向
12输入的html文本超过长度限制
14不支持的翻译内容
30翻译失败

常用语言 Demo

Java 示例

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;


public class Translate_htmlV3Demo {

private static Logger logger = LoggerFactory.getLogger(Translate_htmlV3Demo.class);

private static final String YOUDAO_URL = "https://openapi.youdao.com/translate_html";

private static final String APP_KEY = "您的应用ID";

private static final String APP_SECRET = "您的应用密钥";

public static void main(String[] args) throws IOException {

    Map<String,String> params = new HashMap<String,String>();
    String q = "待输入的文字";
    String salt = String.valueOf(System.currentTimeMillis());
    String from = "源语言";
    String to = "目标语言";
    params.put("from",from);
    params.put("to",to);
    params.put("signType", "v3");
    String curtime = String.valueOf(System.currentTimeMillis() / 1000);
    params.put("curtime", curtime);
    String signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET;
    String sign = getDigest(signStr);
    params.put("appKey", APP_KEY);
    params.put("q", q);
    params.put("salt", salt);
    params.put("sign", sign);
    /** 处理结果 */
    requestForHttp(YOUDAO_URL,params);
}

public static void requestForHttp(String url,Map<String,String> params) throws IOException {

    /** 创建HttpClient */
    CloseableHttpClient httpClient = HttpClients.createDefault();

    /** httpPost */
    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
    Iterator<Map.Entry<String,String>> it = params.entrySet().iterator();
    while(it.hasNext()){
        Map.Entry<String,String> en = it.next();
        String key = en.getKey();
        String value = en.getValue();
        paramsList.add(new BasicNameValuePair(key,value));
    }
    httpPost.setEntity(new UrlEncodedFormEntity(paramsList,"UTF-8"));
    CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
    try{
        Header[] contentType = httpResponse.getHeaders("Content-Type");
        logger.info("Content-Type:" + contentType[0].getValue());
        if("audio/mp3".equals(contentType[0].getValue())){
            //如果响应是wav
            HttpEntity httpEntity = httpResponse.getEntity();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            httpResponse.getEntity().writeTo(baos);
            byte[] result = baos.toByteArray();
            EntityUtils.consume(httpEntity);
            if(result != null){//合成成功
                String file = "合成的音频存储路径"+System.currentTimeMillis() + ".mp3";
                byte2File(result,file);
            }
        }else{
            /** 响应不是音频流,直接显示结果 */
            HttpEntity httpEntity = httpResponse.getEntity();
            String json = EntityUtils.toString(httpEntity,"UTF-8");
            EntityUtils.consume(httpEntity);
            logger.info(json);
            System.out.println(json);
        }
    }finally {
        try{
            if(httpResponse!=null){
                httpResponse.close();
            }
        }catch(IOException e){
            logger.info("## release resouce error ##" + e);
        }
    }
}

/**
 * 生成加密字段
 */
public static String getDigest(String string) {
    if (string == null) {
        return null;
    }
    char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    byte[] btInput = string.getBytes();
    try {
        MessageDigest mdInst = MessageDigest.getInstance("SHA-256");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();
        int j = md.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (byte byte0 : md) {
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

/**
*
* @param result 音频字节流
* @param file 存储路径
*/
private static void byte2File(byte[] result, String file) {
    File audioFile = new File(file);
    FileOutputStream fos = null;
    try{
        fos = new FileOutputStream(audioFile);
        fos.write(result);

    }catch (Exception e){
        logger.info(e.toString());
    }finally {
        if(fos != null){
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
    public static String truncate(String q) {
        if (q == null) {
            return null;
        }
        int len = q.length();
        String result;
        return len <= 20 ? q : (q.substring(0, 10) + len + q.substring(len - 10, len));
    }
}

python2示例

# -*- coding: utf-8 -*-
import sys
import uuid
import requests
import hashlib
import time
import time

reload(sys)
sys.setdefaultencoding('utf-8')

YOUDAO_URL = 'https://openapi.youdao.com/translate_html'
APP_KEY = '您的应用ID'
APP_SECRET = '您的应用密钥'


def encrypt(signStr):
    hash_algorithm = hashlib.sha256()
    hash_algorithm.update(signStr.encode('utf-8'))
    return hash_algorithm.hexdigest()


def truncate(q):
    if q is None:
        return None
    size = len(q)
    return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]


def do_request(data):
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    return requests.post(YOUDAO_URL, data=data, headers=headers)


def connect():
    q = "待输入的文字"

    data = {}
    data['from'] = '源语言'
    data['to'] = '目标语言'
    data['signType'] = 'v3'
    curtime = str(int(time.time()))
    data['curtime'] = curtime
    salt = str(uuid.uuid1())
    signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
    sign = encrypt(signStr)
    data['appKey'] = APP_KEY
    data['q'] = q
    data['salt'] = salt
    data['sign'] = sign

    response = do_request(data)
    contentType = response.headers['Content-Type']
    if contentType == "audio/mp3":
        millis = int(round(time.time() * 1000))
        filePath = "合成的音频存储路径" + str(millis) + ".mp3"
        fo = open(filePath, 'wb')
        fo.write(response.content)
        fo.close()
    else:
        print response.content


if __name__ == '__main__':
    connect()

python3示例

# -*- coding: utf-8 -*-
import sys
import uuid
import requests
import hashlib
import time
from imp import reload

import time

reload(sys)

YOUDAO_URL = 'https://openapi.youdao.com/translate_html'
APP_KEY = '您的应用ID'
APP_SECRET = '您的应用密钥'


def encrypt(signStr):
    hash_algorithm = hashlib.sha256()
    hash_algorithm.update(signStr.encode('utf-8'))
    return hash_algorithm.hexdigest()


def truncate(q):
    if q is None:
        return None
    size = len(q)
    return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]


def do_request(data):
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    return requests.post(YOUDAO_URL, data=data, headers=headers)


def connect():
    q = "待输入的文字"

    data = {}
    data['from'] = '源语言'
    data['to'] = '目标语言'
    data['signType'] = 'v3'
    curtime = str(int(time.time()))
    data['curtime'] = curtime
    salt = str(uuid.uuid1())
    signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
    sign = encrypt(signStr)
    data['appKey'] = APP_KEY
    data['q'] = q
    data['salt'] = salt
    data['sign'] = sign

    response = do_request(data)
    contentType = response.headers['Content-Type']
    if contentType == "audio/mp3":
        millis = int(round(time.time() * 1000))
        filePath = "合成的音频存储路径" + str(millis) + ".mp3"
        fo = open(filePath, 'wb')
        fo.write(response.content)
        fo.close()
    else:
        print(response.content)


if __name__ == '__main__':
    connect()

php示例

<?php
define("CURL_TIMEOUT",   2000);
define("URL",            "https://openapi.youdao.com/translate_html");
define("APP_KEY",        "您的应用ID"); // 替换为您的应用ID
define("SEC_KEY",        "您的应用密钥"); // 替换为您的密钥

function do_request($q)
{
    $salt = create_guid();
    $args = array(
        'q' => $q,
        'appKey' => APP_KEY,
        'salt' => $salt,
    );
    $from = '源语言';
    $to = '目标语言';
    $args['from'] = $from;
    $args['to'] = $to;
    $args['signType'] = 'v3';
    $curtime = strtotime("now");
    $args['curtime'] = $curtime;
    $signStr = APP_KEY . truncate($q) . $salt . $curtime . SEC_KEY;
    $args['sign'] = hash("sha256", $signStr);
    $ret = call(URL, $args);
    return $ret;
}

// 发起网络请求
function call($url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $headers=array())
{
    $ret = false;
    $i = 0;
    while($ret === false)
    {
        if($i > 1)
            break;
        if($i > 0)
        {
            sleep(1);
        }
        $ret = callOnce($url, $args, $method, false, $timeout, $headers);
        $i++;
    }
    return $ret;
}

function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = CURL_TIMEOUT, $headers=array())
{
    $ch = curl_init();
    if($method == "post")
    {
        $data = convert($args);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    else
    {
        $data = convert($args);
        if($data)
        {
            if(stripos($url, "?") > 0)
            {
                $url .= "&$data";
            }
            else
            {
                $url .= "?$data";
            }
        }
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(!empty($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    if($withCookie)
    {
        curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
    }
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}

function convert(&$args)
{
    $data = '';
    if (is_array($args))
    {
        foreach ($args as $key=>$val)
        {
            if (is_array($val))
            {
                foreach ($val as $k=>$v)
                {
                    $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                }
            }
            else
            {
                $data .="$key=".rawurlencode($val)."&";
            }
        }
        return trim($data, "&");
    }
    return $args;
}

// uuid generator
function create_guid(){
    $microTime = microtime();
    list($a_dec, $a_sec) = explode(" ", $microTime);
    $dec_hex = dechex($a_dec* 1000000);
    $sec_hex = dechex($a_sec);
    ensure_length($dec_hex, 5);
    ensure_length($sec_hex, 6);
    $guid = "";
    $guid .= $dec_hex;
    $guid .= create_guid_section(3);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= $sec_hex;
    $guid .= create_guid_section(6);
    return $guid;
}

function create_guid_section($characters){
    $return = "";
    for($i = 0; $i < $characters; $i++)
    {
        $return .= dechex(mt_rand(0,15));
    }
    return $return;
}

function truncate($q) {
    $len = strlen($q);
    return $len <= 20 ? $q : (substr($q, 0, 10) . $len . substr($q, $len - 10, $len));
}

function ensure_length(&$string, $length){
    $strlen = strlen($string);
    if($strlen < $length)
    {
        $string = str_pad($string, $length, "0");
    }
    else if($strlen > $length)
    {
        $string = substr($string, 0, $length);
    }
}

// 输入
$q = "待输入的文字";

$ret = do_request($q);

print $ret;
?>

c#示例

using System;
using System.IO;
using System.Web;
using System.Net;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;

namespace zhiyun_csharp_demo
{
    class Translate_htmlV3Demo
    {
        public static void Main()
        {
            Dictionary<String, String> dic = new Dictionary<String, String>();
            string url = "https://openapi.youdao.com/translate_html";
            string q = "待输入的文字";
            string appKey = "您的应用ID";
            string appSecret = "您的应用密钥";
            string salt = DateTime.Now.Millisecond.ToString();
            string from = "源语言";
            string to = "目标语言";
            dic.Add("from",from);
            dic.Add("to",to);
            dic.Add("signType", "v3");
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            long millis = (long) ts.TotalMilliseconds;
            string curtime = Convert.ToString(millis / 1000);
            dic.Add("curtime", curtime);
            string signStr = appKey + Truncate(q) + salt + curtime + appSecret;;
            string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());
            dic.Add("q", System.Web.HttpUtility.UrlEncode(q));
            dic.Add("appKey", appKey);
            dic.Add("salt", salt);
            dic.Add("sign", sign);
            Post(url, dic);
        }

        protected static string ComputeHash(string input, HashAlgorithm algorithm)
        {
          Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
          Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
          return BitConverter.ToString(hashedBytes).Replace("-", "");
        }

        protected static void Post(string url, Dictionary<String, String> dic)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
          if (resp.ContentType.ToLower().Equals("audio/mp3"))
            {
                SaveBinaryFile(resp, "合成的音频存储路径");
            }
            else {
                Stream stream = resp.GetResponseStream();
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                result = reader.ReadToEnd();
                }
                Console.WriteLine(result);
            }
        }

        protected static string Truncate(string q)
        {
            if (q == null)
            {
               return null;
            }
            int len = q.Length;
            return len <= 20 ? q : (q.Substring(0, 10) + len + q.Substring(len - 10, 10));
        }

        private static bool SaveBinaryFile(WebResponse response, string FileName)
        {
            string FilePath = FileName + DateTime.Now.Millisecond.ToString() + ".mp3";
            bool Value = true;
            byte[] buffer = new byte[1024];

            try
            {
                if (File.Exists(FilePath))
                    File.Delete(FilePath);
                Stream outStream = System.IO.File.Create(FilePath);
                Stream inStream = response.GetResponseStream();

                int l;
                do
                {
                    l = inStream.Read(buffer, 0, buffer.Length);
                    if (l > 0)
                        outStream.Write(buffer, 0, l);
                }
                while (l > 0);

                outStream.Close();
                inStream.Close();
            }
            catch
            {
                Value = false;
            }
            return Value;
        }
    }
}