Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(services): add bing sentence translate service #1038

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addon/locale/en-US/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ service-chatgpt=ChatGPT🗝️
service-azuregpt=AzureGPT🗝️
service-gemini=Gemini🗝️
service-haici=Haici
service-bing=Bing
service-bingdict=Bing Dict(en↔zh)🔊
service-haicidict=Haici Dict(en↔zh)🔊
service-collinsdict=Collins Dict(en↔zh)🔊
Expand Down
2 changes: 2 additions & 0 deletions addon/locale/en-US/panel.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ service-gemini =
.label = Gemini🗝️
service-haici =
.label = Haici
service-bing =
.label = Bing
service-bingdict =
.label = Bing Dict(en↔zh)🔊
service-haicidict =
Expand Down
1 change: 1 addition & 0 deletions addon/locale/it-IT/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ service-xftrans=Xftrans🗝️
service-chatgpt=ChatGPT🗝️
service-azuregpt=AzureGPT🗝️
service-haici=Haici
service-bing=Bing
service-bingdict=Bing Dict(en↔zh)🔊
service-haicidict=Haici Dict(en↔zh)🔊
service-collinsdict=Collins Dict(en↔zh)🔊
Expand Down
2 changes: 2 additions & 0 deletions addon/locale/it-IT/panel.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ service-gemini =
.label = Gemini🗝️
service-haici =
.label = Haici
service-bing =
.label = Bing
service-bingdict =
.label = Bing Dict(en↔zh)🔊
service-haicidict =
Expand Down
1 change: 1 addition & 0 deletions addon/locale/zh-CN/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ service-xftrans=讯飞🗝️
service-chatgpt=ChatGPT🗝️
service-azuregpt=AzureGPT🗝️
service-haici=海词
service-bing=必应
service-bingdict=必应词典(en↔zh)🔊
service-haicidict=海词词典(en↔zh)🔊
service-collinsdict=科林斯词典(en↔zh)🔊
Expand Down
2 changes: 2 additions & 0 deletions addon/locale/zh-CN/panel.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ service-gemini =
.label = Gemini🗝️
service-haici =
.label = 海词
service-bing =
.label = 必应
service-bingdict =
.label = 必应词典(en↔zh)🔊
service-haicidict =
Expand Down
89 changes: 89 additions & 0 deletions src/modules/services/bing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { getPref, getPrefJSON, setPref } from "../../utils/prefs";
import { TranslateTaskProcessor } from "../../utils/task";

export default <TranslateTaskProcessor>async function (data) {
const xhr = await Zotero.HTTP.request(
"POST",
`https://api-edge.cognitive.microsofttranslator.com/translate?from=${data.langfrom}&to=${data.langto}&api-version=3.0&includeSentenceLength=true`,
{
headers: {
accept: "*/*",
"accept-language":
"zh-TW,zh;q=0.9,ja;q=0.8,zh-CN;q=0.7,en-US;q=0.6,en;q=0.5",
authorization: `Bearer ${await getToken()}`,
"cache-control": "no-cache",
"content-type": "application/json",
pragma: "no-cache",
"sec-ch-ua":
'"Microsoft Edge";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
Referer: "https://appsumo.com/",
"Referrer-Policy": "strict-origin-when-cross-origin",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42",
},
body: JSON.stringify([{ text: data.raw }]),
responseType: "json",
},
);
if (xhr?.status !== 200) {
throw `Request error: ${xhr?.status}`;
}

try {
data.result = xhr.response[0].translations[0].text;
} catch {
throw `Service error: ${xhr.response}`;
}
};

const bingTokenKey = "bingToken";
const tokenExpTime = 5 * 60 * 1000; // 5 minutes refresh token

async function getToken(forceRefresh: boolean = false) {
let token = "";
// Just in case the update fails
let doRefresh = true;
try {
const tokenObj = getPrefJSON(bingTokenKey);
if (
!forceRefresh &&
tokenObj &&
tokenObj.token &&
new Date().getTime() < tokenObj.exp
) {
token = tokenObj.token;
doRefresh = false;
}
} catch (e) {
ztoolkit.log(e);
}
if (doRefresh) {
const xhr = await Zotero.HTTP.request(
"GET",
"https://edge.microsoft.com/translate/auth",
{
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42",
},
responseType: "text",
},
);
if (xhr && xhr.response) {
token = xhr.response;
setPref(
bingTokenKey,
JSON.stringify({
exp: new Date().getTime() + tokenExpTime,
token: token,
}),
);
}
}
return token;
}
3 changes: 3 additions & 0 deletions src/modules/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export class TranslationServices {
import("./baidufield").then(
(e) => (this.baidufield = new TranslateTaskRunner(e.default)),
);
import("./bing").then(
(e) => (this.bing = new TranslateTaskRunner(e.default)),
);
import("./bingdict").then(
(e) => (this.bingdict = new TranslateTaskRunner(e.default)),
);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export const SERVICES: Readonly<Readonly<TranslateService>[]> = <const>[
type: "sentence",
id: "youdao",
},
{
type: "sentence",
id: "bing",
},
{
type: "sentence",
id: "huoshan",
Expand Down