本文所述的網頁翻譯功能,截取自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