diff --git a/src/lib/constant.ts b/src/lib/constant.ts index 00b0411..7d6e6fc 100644 --- a/src/lib/constant.ts +++ b/src/lib/constant.ts @@ -16,5 +16,6 @@ export const languages: Language[] = [ { key: "de", value: "Deutsch" }, { key: "it", value: "Italian" }, { key: "es", value: "Spanish" }, + { key: "ko", value: "Korean" }, { key: "pt", value: "Portuguese" }, ]; diff --git a/src/pages/api/lib/baidu.ts b/src/pages/api/lib/baidu.ts index a55a35d..48d591d 100644 --- a/src/pages/api/lib/baidu.ts +++ b/src/pages/api/lib/baidu.ts @@ -8,6 +8,7 @@ const languageMap: Record = { "ja": "jp", "fr": "fra", "es": "spa", + "ko": "kor", }; export class Baidu { @@ -24,10 +25,10 @@ export class Baidu { const salt = String(Math.random()).slice(2) const sign = MD5(this.APPID+text+salt+this.APPKEY) try { - const apiUrl =`http://api.fanyi.baidu.com/api/trans/vip/translate?q=${text}&from=${source}&to=${target}&appid=${this.APPID}&salt=${salt}&sign=${sign}`; + const apiUrl =`http://api.fanyi.baidu.com/api/trans/vip/translate?q=${encodeURIComponent(text)}&from=${source}&to=${target}&appid=${this.APPID}&salt=${salt}&sign=${sign}`; const response = await axios.get(apiUrl); if (response.data) { - return response.data.trans_result[0].dst; + return response.data.trans_result.map((item: { dst: any; }) => item.dst).join("\n\n"); } else { throw new Error("Invalid response from Baidu"); } diff --git a/src/pages/api/lib/chatgpt.ts b/src/pages/api/lib/chatgpt.ts index 11dad6c..5043fe8 100644 --- a/src/pages/api/lib/chatgpt.ts +++ b/src/pages/api/lib/chatgpt.ts @@ -38,9 +38,17 @@ export class ChatGPT { const data = JSON.stringify({ model: this.model, messages: [ + { + role: "system", + content: `You are a professional, authentic translation engine, only returns translations.`, + }, + { + role: "user", + content: `Please translate the text from ${source} to ${target} language, without explaining my original text.`, + }, { role: "user", - content: `请将以下文本从${source}翻译为${target}:“${text}”(要求:输出的结果只能出现翻译结果,需要准确且清晰,不得出现翻译结果以外的内容,输出结果不可以带任何特殊的文本样式,'',""不得出现)`, + content: text, }, ], temperature: 0.7, diff --git a/src/pages/api/lib/gemini.ts b/src/pages/api/lib/gemini.ts index 369b287..ae98853 100644 --- a/src/pages/api/lib/gemini.ts +++ b/src/pages/api/lib/gemini.ts @@ -37,7 +37,13 @@ export class Gemini { { parts: [ { - text: `请将以下文本从${source}翻译为${target}:“${text}”(要求:输出的结果只能出现翻译结果,需要准确且清晰,不得出现翻译结果以外的内容,输出结果不可以带任何特殊的文本样式,'',""不得出现)`, + text: `You are a professional, authentic translation engine, only returns translations.`, + }, + { + text: `Please translate the text from ${source} to ${target} language, without explaining my original text.`, + }, + { + text: text, }, ], }, diff --git a/src/pages/index.tsx b/src/pages/index.tsx index d784e7a..fe23537 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -152,7 +152,15 @@ export default function Home() { onChange={setFrom} placeholder={`Source Language`} /> - + { + const temp = from; + if (temp != 'auto') { + setFrom(to); + setTo(temp); + } + }} + />