點擊上方“秋風的筆記”關注我們
分享一個免費且功能強大谷歌翻譯api,具有以下方式:
批量翻譯
免費無限制
快速可靠,和 translate.google.com 相同源
瀏覽器插件/node/瀏覽器
支持代理
使用方式
npm install google-translate-open-api
單段文本
import translate from 'google-translate-open-api';
const result = await translate(`I'm fine.`, {
tld: "cn",
to: "zh-CN",
});
const data = result.data[0];
// 我很好。
多段文本
import translate from 'google-translate-open-api';
const result = await translate([`I'm fine.`, `I'm ok.`], {
tld: "cn",
to: "zh-CN",
});
const data = result.data[0];
// [[[["我很好。"]],null,"en"],[[["我可以。"]],null,"en"]]
注意: 多段文本的返回值和單段文本的返回值不同,你需要額外的注意
多段文本中含有多個句子
import translate, { parseMultiple } from 'google-translate-open-api';
const result = await translate([`I'm fine. And you?`,`I'm ok.`], {
tld: "cn",
to: "zh-CN",
});
// [[[[["<i>I'm fine.</i> <b>我很好。</b> <i>And you?</i> <b>你呢?</b>"]],null,"en"],[[["我可以。"]],null,"en"]]]
// use parseMultiple
const data = result.data[0];
const parseData = parseMultiple(data);
// ["我很好。你呢?","我可以。"]
代理
proxy-config https://github.com/axios/axios#request-config
const result = await translate([`I'm fine. And you?`,`I'm ok.`], {
tld: "cn",
to: "zh-CN",
proxy: {
host: '127.0.0.1',
port: 9000,
auth: {
username: 'mikeymike',
password: 'rapunz3l'
}
}
});
瀏覽器
const result = await translate([`I'm fine. And you?`,`I'm ok.`], {
tld: "cn",
to: "zh-CN",
browers: true
});
const data = result.data[0];
// 我很好。
github地址
https://github.com/hua1995116/google-translate-open-api
更多請關注
友情鏈接:https://blog.qiufengh.com/blog/
長按二維碼,可以關注我喲
萬水千山總是情,點個小贊行不行