免费翻译 API 全攻略::google、爱词霸、腾讯云、阿里云、彩云、有道。。。
以下推荐一个插件,直接看源码拷贝粘贴就可以用,这块插件包括了
- 谷歌翻译
- 腾讯翻译
- 有道翻译
- 百度翻译
- 彩云小译
- 必应翻译
- Papago翻译
- 阿里翻译
- 爱词霸翻译
- Deepl翻译
- 腾讯AI翻译
https://greasyfork.org/zh-CN/scripts/378277-翻译机/code
下面是我重写的代码,只移植了部分翻译,完全免费,没有限制
import axios from 'axios';
import CryptoJS from 'crypto-js';
/**
* 翻译类
*/
export class Fanyi {
index: number = 0;
/**
* 彩云翻译 配置参数
*/
caiyun_id: string = '';
caiyun_jwt: string = '';
/**
* 翻译, 默认从中文到英文
* @param text
* @param from
* @param to
* @return {Promise<any>}
* @private
* @memberof Fanyi
* */
translates = [
this.translate_icib, // 爱词霸
this.translate_tencentai, // 腾讯云
this.translate_alibaba, // 阿里云
this.translate_caiyun, // 彩云
this.translate_youdao_mobile // 有道
];
async translate(text: string) {
const fanyi = this.translates[this.index % this.translates.length](text);
this.index++;
if (this.index >= this.translates.length) {
this.index = 0;
}
return fanyi;
}
/**
* 爱词霸
* @param text string
* @returns {Promise<string>}
*/
async translate_icib(text: string) {
const sign = CryptoJS.MD5(
'6key_web_fanyi' + 'ifanyiweb8hc9s98e' + text.replace(/(^\s*)|(\s*$)/g, '')
)
.toString()
.substring(0, 16);
const options = {
method: 'POST',
url: `https://ifanyi.iciba.com/index.php?c=trans&m=fy&client=6&auth_user=key_web_fanyi&sign=${sign}`,
data: 'from=auto&to=en&q=' + encodeURIComponent(text),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
return axios(options).then((res) => res?.data?.content?.out || '');
}
/**
* 判断语言
* @param text string
* @return {Promise<string>}
* @private
* @memberof Fanyi
* */
private async _check_lang(text: string) {
const res = await fetch(
'https://fanyi.baidu.com/langdetect?from=auto&to=en&query=' + encodeURIComponent(text)
);
const data = await res.json();
try {
return data;
} catch (err) {
console.log(err);
return;
}
}
/**
* 腾讯AI翻译,不支持纯英文翻译成英文
* @param text string
* @return {Promise<string>}
* @private
* @memberof Fanyi
* */
async translate_tencentai(text: string) {
// 如果是英文直接返回
const langType = await this._check_lang(text);
if (langType === 'en') {
return text;
}
const data = {
header: {
fn: 'auto_translation'
},
type: 'plain',
model_category: 'normal',
text_domain: 'general',
source: {
lang: 'auto',
text_list: [text]
},
target: {
lang: 'en'
}
};
const options = {
method: 'POST',
url: 'https://transmart.qq.com/api/imt',
data: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
Host: 'transmart.qq.com',
Origin: 'https://transmart.qq.com',
Referer: 'https://transmart.qq.com/'
},
anonymous: true,
nocache: true
};
return axios(options).then((res) => res.data?.auto_translation?.[0] || '');
}
/**
* 阿里翻译
* @param text string
* @return {Promise<string>}
* @private
* @memberof Fanyi
* */
async translate_alibaba(text: string) {
const options = {
method: 'POST',
url: 'https://translate.alibaba.com/translationopenseviceapp/trans/TranslateTextAddAlignment.do',
data: `srcLanguage=auto&tgtLanguage=en&bizType=message&srcText=${encodeURIComponent(
text
)}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
origin: 'https://translate.alibaba.com',
referer: 'https://translate.alibaba.com/',
'sec-fetch-site': 'same-origin'
}
};
return axios(options).then((res) => res?.data?.listTargetText?.[0] || '');
}
/**
* 彩云翻译
* @param text string
* @return {Promise<string>}
* @private
* @memberof Fanyi
* */
private async _translate_caiyun_startup() {
if (this.caiyun_id && this.caiyun_jwt) return;
const browser_id = CryptoJS.MD5(Math.random().toString()).toString();
this.caiyun_id = browser_id;
const options = {
method: 'POST',
url: 'https://api.interpreter.caiyunai.com/v1/user/jwt/generate',
headers: {
'Content-Type': 'application/json',
'X-Authorization': 'token:qgemv4jr1y38jyq6vhvi',
Origin: 'https://fanyi.caiyunapp.com'
},
data: JSON.stringify({ browser_id })
};
const res = await axios(options);
this.caiyun_jwt = res.data.jwt;
}
async translate_caiyun(text: string) {
{
await this._translate_caiyun_startup();
const source = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm';
const dic = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'].reduce(
(dic, current, index) => {
dic[current] = source[index];
return dic;
},
{}
);
const decoder = (line) => [...line].map((i) => dic[i] || i).join('');
const options = {
method: 'POST',
url:
'https://api.interpreter.caiyunai.com/v1/translator?source=zh&target=en&trans_type=auto2zh&browser_id=' +
this.caiyun_id,
data: {
source: [text],
trans_type: 'auto2en',
detect: true,
browser_id: this.caiyun_id
},
headers: {
'X-Authorization': 'token:qgemv4jr1y38jyq6vhvi',
'T-Authorization': this.caiyun_jwt
},
signal: controller.signal
};
return axios(options).then((res) => {
const tranText = decoder(res?.data?.target?.[0]) || '';
return Buffer.from(tranText, 'base64').toString();
});
}
}
/**
* 有道翻译
* @param text string
* @returns
*/
async translate_youdao_mobile(text: string) {
const options = {
method: 'POST',
url: 'http://m.youdao.com/translate',
data: 'inputtext=' + encodeURIComponent(text) + '&type=ZH_CN2EN',
anonymous: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
return axios(options).then(
(res) =>
/id="translateResult">\s*?<li>([\s\S]*?)<\/li>\s*?<\/ul/.exec(
res?.data || ''
)?.[1] || ''
);
}
}
评论
发表评论