本文所述的网页翻译功能,截取自Chrome浏览器,适合以下几种应用场景:
一、 网站开发,本站页面翻译
二、 浏览器开发(Google翻译不可用的情况)
三、 网络爬虫、数据分析
四、 后台翻译服务
基本原理
JavaScript代码取自chrome浏览器自带的网页翻译功能,替换其翻译时的网络请求接口为 bing翻译服务器。
bing翻译API
https://www.bing.com/translator/api/translate?ref=edge&from=&to=zh-Hans
request header: "Content-Type": "application/json"
method: "POST"
body: ["string1","string2",...]
google翻译API
https://translate.googleapis.com/translate_a/t?anno=3&client=te_lib&format=html&v=1.0&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&logld=vTE_20170911_00&sl=auto&tl=zh&sp=nmt&tc=1&sr=1&tk=706275.834617&mode=1&sl=auto&tl=zh&sp=nmt&tc=1&sr=1&tk=625681.1046048&mode=1
request header: "Content-Type": "application/x-www-form-urlencoded"
method: "POST"
body: q=%E2%86%91&q=W3C&q=World%20Wide%20Web%20Consortium
项目地址
https://github.com/camike/webpage-translate