Skip to content

Commit

Permalink
fix: add region to microsoft translator api (#1009)
Browse files Browse the repository at this point in the history
fixed #1004
update preference prompt for the Microsoft translator.
  • Loading branch information
l0o0 authored Nov 18, 2024
1 parent f5af799 commit cc7f4af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/modules/services/microsoft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import { TranslateTaskProcessor } from "../../utils/task";

export default <TranslateTaskProcessor>async function (data) {
const reqBody = JSON.stringify([{ Text: data.raw }]);

const params = data.secret.split("#");
const secretKey = params[0];
const headers: Record<string, string> = {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": secretKey,
};
// Sometimes the MicroSoft translator service need a region parameter.
// The region string is in lower case without space inside. East Asia -> eastasia
if (params[1]) {
const region = params[1].replace(" ", "").toLowerCase();
if (params[1] != "global") headers["Ocp-Apim-Subscription-Region"] = region;
}
const xhr = await Zotero.HTTP.request(
"POST",
`https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=${data.langto}`,
{
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": data.secret,
},
headers: headers,
responseType: "json",
body: reqBody,
},
Expand Down
4 changes: 2 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ export const SERVICES: Readonly<Readonly<TranslateService>[]> = <const>[
id: "microsoft",
defaultSecret: "",
secretValidator(secret: string) {
const flag = secret?.length === 32;
const flag = secret?.length === 32 || secret?.length === 84;
return {
secret,
status: flag,
info: flag
? ""
: `The secret is your Azure translate service KEY. The secret length must be 32, but got ${secret?.length}.`,
: `The secret is your Azure translate serviceKEY#region(required if the region is not global). The secret length must be 32 or 84, but got ${secret?.length}.`,
};
},
},
Expand Down

0 comments on commit cc7f4af

Please sign in to comment.