i18-fe-automator
Version:
前端代码提取中文并替换成$t函数
53 lines (52 loc) • 1.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.chatGPTTranslate = void 0;
const proxy_agent_1 = __importDefault(require("proxy-agent"));
const got = require('got');
async function chatGPTTranslate(word, originLang, targetLang, option) {
const url = 'https://api.openai.com/v1/chat/completions';
const key = option.key;
const agent = option && option.proxy
? {
http: new proxy_agent_1.default(option.proxy),
https: new proxy_agent_1.default(option.proxy),
}
: undefined;
return new Promise((resolve, reject) => {
got
.post(url, {
headers: {
Authorization: `Bearer ${key || ''}`,
'Content-Type': 'application/json',
},
json: {
messages: [
{
role: 'system',
content: `请帮我翻译,如果是一个单词或者词组,每个首字母都是大写,如果是句子或者一段话,只有第一个单词大写,下面的文字改成${targetLang || '英文'}:
${word}
`,
},
],
model: 'gpt-3.5-turbo',
},
agent,
})
.then(({ body }) => {
const res = JSON.parse(body);
if (res && res.choices && res.choices.length) {
resolve(res.choices[0].message.content);
}
else {
reject(body);
}
})
.catch((e) => {
reject(e);
});
});
}
exports.chatGPTTranslate = chatGPTTranslate;