change deepl.ts to new mode
This commit is contained in:
parent
564192f678
commit
c0456370cd
25
.history/src/modules/services/deepl_20230726092452.ts
Normal file
25
.history/src/modules/services/deepl_20230726092452.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { TranslateTask, TranslateTaskProcessor } from "../../utils/translate";
|
||||||
|
|
||||||
|
export const deeplfree = <TranslateTaskProcessor>async function (data) {
|
||||||
|
return await deepl("https://api-free.deepl.com/v2/translate", data);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
||||||
|
return await deepl("https://api.deepl.com/v2/translate", data);
|
||||||
|
};
|
||||||
|
|
||||||
|
async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
|
const reqBody = `auth_key=${data.secret}&text=${encodeURIComponent(
|
||||||
|
data.raw
|
||||||
|
)}&source_lang=${data.langfrom
|
||||||
|
.split("-")[0]
|
||||||
|
.toUpperCase()}&target_lang=${data.langto.split("-")[0].toUpperCase()}`;
|
||||||
|
const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
responseType: "json",
|
||||||
|
body: reqBody,
|
||||||
|
});
|
||||||
|
if (xhr?.status !== 200) {
|
||||||
|
throw `Request error: ${xhr?.status}`;
|
||||||
|
}
|
||||||
|
data.result = xhr.response.translations[0].text;
|
||||||
|
}
|
65
.history/src/modules/services/deepl_20250610163253.ts
Normal file
65
.history/src/modules/services/deepl_20250610163253.ts
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import { TranslateTask, TranslateTaskProcessor } from "../../utils/translate";
|
||||||
|
|
||||||
|
export const deeplfree = <TranslateTaskProcessor>async function (data) {
|
||||||
|
return await deepl("https://api-free.deepl.com/v2/translate", data);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
||||||
|
return await deepl("https://api.deepl.com/v2/translate", data);
|
||||||
|
};
|
||||||
|
|
||||||
|
// async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
|
// const reqBody = `auth_key=${data.secret}&text=${encodeURIComponent(
|
||||||
|
// data.raw
|
||||||
|
// )}&source_lang=${data.langfrom
|
||||||
|
// .split("-")[0]
|
||||||
|
// .toUpperCase()}&target_lang=${data.langto.split("-")[0].toUpperCase()}`;
|
||||||
|
// const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
// responseType: "json",
|
||||||
|
// body: reqBody,
|
||||||
|
// });
|
||||||
|
// if (xhr?.status !== 200) {
|
||||||
|
// throw `Request error: ${xhr?.status}`;
|
||||||
|
// }
|
||||||
|
// data.result = xhr.response.translations[0].text;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
|
const [key, glossary_id]: string[] = data.secret.split("#");
|
||||||
|
const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `DeepL-Auth-Key ${key}`,
|
||||||
|
"User-Agent": `Translate for Zotero/${Zotero.version}-${Zotero.platform}-${version}`,
|
||||||
|
},
|
||||||
|
responseType: "json",
|
||||||
|
body: JSON.stringify({
|
||||||
|
text: [data.raw],
|
||||||
|
source_lang: mapLang(data.langfrom),
|
||||||
|
target_lang: mapLang(data.langto),
|
||||||
|
glossary_id: glossary_id,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
if (xhr?.status !== 200) {
|
||||||
|
throw `Request error: ${xhr?.status}`;
|
||||||
|
}
|
||||||
|
data.result = xhr.response.translations[0].text;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapLang(lang: string) {
|
||||||
|
if (lang in LANG_MAP) {
|
||||||
|
return LANG_MAP[lang];
|
||||||
|
}
|
||||||
|
return lang.split("-")[0].toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
const LANG_MAP = {
|
||||||
|
"pt-BR": "PT-BR",
|
||||||
|
"pt-PT": "PT-PT",
|
||||||
|
"zh-CN": "ZH-HANS",
|
||||||
|
"zh-HK": "ZH-HANT",
|
||||||
|
"zh-MO": "ZH-HANT",
|
||||||
|
"zh-SG": "ZH-HANS",
|
||||||
|
"zh-TW": "ZH-HANT",
|
||||||
|
} as Record<string, string | undefined>;
|
74
.history/src/modules/services/deepl_20250610163408.ts
Normal file
74
.history/src/modules/services/deepl_20250610163408.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import { TranslateTask, TranslateTaskProcessor } from "../../utils/translate";
|
||||||
|
|
||||||
|
export const deeplfree = <TranslateTaskProcessor>async function (data) {
|
||||||
|
return await deepl("https://api-free.deepl.com/v2/translate", data);
|
||||||
|
};
|
||||||
|
|
||||||
|
// export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
||||||
|
// return await deepl("https://api.deepl.com/v2/translate", data);
|
||||||
|
// };
|
||||||
|
|
||||||
|
// async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
|
// const reqBody = `auth_key=${data.secret}&text=${encodeURIComponent(
|
||||||
|
// data.raw
|
||||||
|
// )}&source_lang=${data.langfrom
|
||||||
|
// .split("-")[0]
|
||||||
|
// .toUpperCase()}&target_lang=${data.langto.split("-")[0].toUpperCase()}`;
|
||||||
|
// const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
// responseType: "json",
|
||||||
|
// body: reqBody,
|
||||||
|
// });
|
||||||
|
// if (xhr?.status !== 200) {
|
||||||
|
// throw `Request error: ${xhr?.status}`;
|
||||||
|
// }
|
||||||
|
// data.result = xhr.response.translations[0].text;
|
||||||
|
// }
|
||||||
|
|
||||||
|
export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
||||||
|
// See https://github.com/windingwind/zotero-pdf-translate/issues/579
|
||||||
|
return await deepl(
|
||||||
|
data.secret.endsWith("dp")
|
||||||
|
? "https://api.deepl-pro.com/v2/translate"
|
||||||
|
: "https://api.deepl.com/v2/translate",
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
|
const [key, glossary_id]: string[] = data.secret.split("#");
|
||||||
|
const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `DeepL-Auth-Key ${key}`,
|
||||||
|
"User-Agent": `Translate for Zotero/${Zotero.version}-${Zotero.platform}-${version}`,
|
||||||
|
},
|
||||||
|
responseType: "json",
|
||||||
|
body: JSON.stringify({
|
||||||
|
text: [data.raw],
|
||||||
|
source_lang: mapLang(data.langfrom),
|
||||||
|
target_lang: mapLang(data.langto),
|
||||||
|
glossary_id: glossary_id,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
if (xhr?.status !== 200) {
|
||||||
|
throw `Request error: ${xhr?.status}`;
|
||||||
|
}
|
||||||
|
data.result = xhr.response.translations[0].text;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapLang(lang: string) {
|
||||||
|
if (lang in LANG_MAP) {
|
||||||
|
return LANG_MAP[lang];
|
||||||
|
}
|
||||||
|
return lang.split("-")[0].toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
const LANG_MAP = {
|
||||||
|
"pt-BR": "PT-BR",
|
||||||
|
"pt-PT": "PT-PT",
|
||||||
|
"zh-CN": "ZH-HANS",
|
||||||
|
"zh-HK": "ZH-HANT",
|
||||||
|
"zh-MO": "ZH-HANT",
|
||||||
|
"zh-SG": "ZH-HANS",
|
||||||
|
"zh-TW": "ZH-HANT",
|
||||||
|
} as Record<string, string | undefined>;
|
74
.history/src/modules/services/deepl_20250610163541.ts
Normal file
74
.history/src/modules/services/deepl_20250610163541.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import { TranslateTask, TranslateTaskProcessor } from "../../utils/translate";
|
||||||
|
|
||||||
|
export const deeplfree = <TranslateTaskProcessor>async function (data) {
|
||||||
|
return await deepl("https://api-free.deepl.com/v2/translate", data);
|
||||||
|
};
|
||||||
|
|
||||||
|
// export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
||||||
|
// return await deepl("https://api.deepl.com/v2/translate", data);
|
||||||
|
// };
|
||||||
|
|
||||||
|
// async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
|
// const reqBody = `auth_key=${data.secret}&text=${encodeURIComponent(
|
||||||
|
// data.raw
|
||||||
|
// )}&source_lang=${data.langfrom
|
||||||
|
// .split("-")[0]
|
||||||
|
// .toUpperCase()}&target_lang=${data.langto.split("-")[0].toUpperCase()}`;
|
||||||
|
// const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
// responseType: "json",
|
||||||
|
// body: reqBody,
|
||||||
|
// });
|
||||||
|
// if (xhr?.status !== 200) {
|
||||||
|
// throw `Request error: ${xhr?.status}`;
|
||||||
|
// }
|
||||||
|
// data.result = xhr.response.translations[0].text;
|
||||||
|
// }
|
||||||
|
|
||||||
|
export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
||||||
|
// See https://github.com/windingwind/zotero-pdf-translate/issues/579
|
||||||
|
return await deepl(
|
||||||
|
data.secret.endsWith("dp")
|
||||||
|
? "https://api.deepl-pro.com/v2/translate"
|
||||||
|
: "https://api.deepl.com/v2/translate",
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
|
const [key, glossary_id]: string[] = data.secret.split("#");
|
||||||
|
const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `DeepL-Auth-Key ${key}`,
|
||||||
|
"User-Agent": `Translate for Zotero/${Zotero.version}-${Zotero.platform}-${version}`,
|
||||||
|
},
|
||||||
|
responseType: "json",
|
||||||
|
body: JSON.stringify({
|
||||||
|
text: [data.raw],
|
||||||
|
source_lang: mapLang(data.langfrom),
|
||||||
|
target_lang: mapLang(data.langto),
|
||||||
|
glossary_id: glossary_id,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
if (xhr?.status !== 200) {
|
||||||
|
throw `Request error: ${xhr?.status}`;
|
||||||
|
}
|
||||||
|
data.result = xhr.response.translations[0].text;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapLang(lang: string) {
|
||||||
|
if (lang in LANG_MAP) {
|
||||||
|
return LANG_MAP[lang];
|
||||||
|
}
|
||||||
|
return lang.split("-")[0].toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
const LANG_MAP = {
|
||||||
|
"pt-BR": "PT-BR",
|
||||||
|
"pt-PT": "PT-PT",
|
||||||
|
"zh-CN": "ZH-HANS",
|
||||||
|
"zh-HK": "ZH-HANT",
|
||||||
|
"zh-MO": "ZH-HANT",
|
||||||
|
"zh-SG": "ZH-HANS",
|
||||||
|
"zh-TW": "ZH-HANT",
|
||||||
|
} as Record<string, string | undefined>;
|
@ -4,22 +4,71 @@ export const deeplfree = <TranslateTaskProcessor>async function (data) {
|
|||||||
return await deepl("https://api-free.deepl.com/v2/translate", data);
|
return await deepl("https://api-free.deepl.com/v2/translate", data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
||||||
|
// return await deepl("https://api.deepl.com/v2/translate", data);
|
||||||
|
// };
|
||||||
|
|
||||||
|
// async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
|
// const reqBody = `auth_key=${data.secret}&text=${encodeURIComponent(
|
||||||
|
// data.raw
|
||||||
|
// )}&source_lang=${data.langfrom
|
||||||
|
// .split("-")[0]
|
||||||
|
// .toUpperCase()}&target_lang=${data.langto.split("-")[0].toUpperCase()}`;
|
||||||
|
// const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
// responseType: "json",
|
||||||
|
// body: reqBody,
|
||||||
|
// });
|
||||||
|
// if (xhr?.status !== 200) {
|
||||||
|
// throw `Request error: ${xhr?.status}`;
|
||||||
|
// }
|
||||||
|
// data.result = xhr.response.translations[0].text;
|
||||||
|
// }
|
||||||
|
|
||||||
export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
export const deeplpro = <TranslateTaskProcessor>async function (data) {
|
||||||
return await deepl("https://api.deepl.com/v2/translate", data);
|
// See https://github.com/windingwind/zotero-pdf-translate/issues/579
|
||||||
|
return await deepl(
|
||||||
|
data.secret.endsWith("dp")
|
||||||
|
? "https://api.deepl-pro.com/v2/translate"
|
||||||
|
: "https://api.deepl.com/v2/translate",
|
||||||
|
data,
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function deepl(url: string, data: Required<TranslateTask>) {
|
async function deepl(url: string, data: Required<TranslateTask>) {
|
||||||
const reqBody = `auth_key=${data.secret}&text=${encodeURIComponent(
|
const [key, glossary_id]: string[] = data.secret.split("#");
|
||||||
data.raw
|
|
||||||
)}&source_lang=${data.langfrom
|
|
||||||
.split("-")[0]
|
|
||||||
.toUpperCase()}&target_lang=${data.langto.split("-")[0].toUpperCase()}`;
|
|
||||||
const xhr = await Zotero.HTTP.request("POST", url, {
|
const xhr = await Zotero.HTTP.request("POST", url, {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `DeepL-Auth-Key ${key}`,
|
||||||
|
"User-Agent": `Translate for Zotero/${Zotero.version}-${Zotero.platform}-${version}`,
|
||||||
|
},
|
||||||
responseType: "json",
|
responseType: "json",
|
||||||
body: reqBody,
|
body: JSON.stringify({
|
||||||
|
text: [data.raw],
|
||||||
|
source_lang: mapLang(data.langfrom),
|
||||||
|
target_lang: mapLang(data.langto),
|
||||||
|
glossary_id: glossary_id,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
if (xhr?.status !== 200) {
|
if (xhr?.status !== 200) {
|
||||||
throw `Request error: ${xhr?.status}`;
|
throw `Request error: ${xhr?.status}`;
|
||||||
}
|
}
|
||||||
data.result = xhr.response.translations[0].text;
|
data.result = xhr.response.translations[0].text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mapLang(lang: string) {
|
||||||
|
if (lang in LANG_MAP) {
|
||||||
|
return LANG_MAP[lang];
|
||||||
|
}
|
||||||
|
return lang.split("-")[0].toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
const LANG_MAP = {
|
||||||
|
"pt-BR": "PT-BR",
|
||||||
|
"pt-PT": "PT-PT",
|
||||||
|
"zh-CN": "ZH-HANS",
|
||||||
|
"zh-HK": "ZH-HANT",
|
||||||
|
"zh-MO": "ZH-HANT",
|
||||||
|
"zh-SG": "ZH-HANS",
|
||||||
|
"zh-TW": "ZH-HANT",
|
||||||
|
} as Record<string, string | undefined>;
|
Loading…
x
Reference in New Issue
Block a user