Google & Baidu 翻译 api
免费 & 无限制的google & baidu 翻译 api
安装
npm install google-baidu-translate-api
使用示例
const translate = require('google-baidu-translate-api')
translate('我是谁', 'en').then(res => {
console.log(res.dist)
console.log(res)
})
translate.google('我是谁', 'en').then(console.log)
translate.baidu('我是谁', 'en').then(console.log)
API
translate()
先尝试使用 google 翻译,如果失败再使用 baidu 翻译
参数如下:
translate(
word: string,
to: string,
from: string
): Promise<{
type: string,
from: string,
to: string,
src: string,
dist: string,
targets: string[]
}>
translate.google()
使用 google 翻译,参数同 translate()
translate.baidu()
使用 baidu 翻译,参数同 translate()
支持的语言
baidu 支持的语言列表:http://api.fanyi.baidu.com/api/trans/product/apidoc#languageList
google 支持的语言列表:https://cloud.google.com/translate/docs/languages
思路
使用 http 请求获取 google 翻译 和 baidu 翻译页面,模拟页面翻译,google 翻译的host 默认为 translate.google.cn
,无需梯子
Thanks
google-translate-api: https://github.com/matheuss/google-translate-api
baidu-translate-api: https://github.com/TimLuo465/baidu-translate-api
本项目基于以上两个项目扩展,代码大部分来自于以上两个项目