This commit is contained in:
1 parent
23b21479b8
commit
2f4f8e99ea
2 files changed
+36
-12
No files matched your search
@@ -27,10 +27,20 @@ export async function translateWithGoogle(
|
|||||||
try {
|
try {
|
||||||
const { text, source_lang, target_lang } = params;
|
const { text, source_lang, target_lang } = params;
|
||||||
|
|
||||||
// Construct the request to Google Translate's internal API
|
const proxyUrls = String(config?.env?.PROXY_URLS || "")
|
||||||
const googleApiUrl = new URL(
|
.split(",")
|
||||||
"https://translate.googleapis.com/translate_a/single"
|
.filter(Boolean);
|
||||||
);
|
const proxyUrl = proxyUrls[Math.floor(Math.random() * proxyUrls.length)];
|
||||||
|
const attempts = [
|
||||||
|
[
|
||||||
|
proxyUrl
|
||||||
|
? new URL("/google/translate_a/single", proxyUrl).toString()
|
||||||
|
: "https://translate.googleapis.com/translate_a/single",
|
||||||
|
"gtx",
|
||||||
|
],
|
||||||
|
["https://translate.googleapis.com/translate_a/single", "gtx"],
|
||||||
|
["https://translate.google.com/translate_a/single", "dict-chrome-ex"],
|
||||||
|
];
|
||||||
const googleParams = new URLSearchParams();
|
const googleParams = new URLSearchParams();
|
||||||
googleParams.append("client", "gtx"); // Google Translate web client
|
googleParams.append("client", "gtx"); // Google Translate web client
|
||||||
googleParams.append(
|
googleParams.append(
|
||||||
@@ -55,16 +65,11 @@ export async function translateWithGoogle(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let googleResponse: Response | undefined;
|
let googleResponse: Response | undefined;
|
||||||
for (const [hostname, client] of [
|
for (const [endpoint, client] of attempts) {
|
||||||
["translate.googleapis.com", "gtx"],
|
|
||||||
["translate.google.com", "gtx"],
|
|
||||||
["translate.googleapis.com", "dict-chrome-ex"],
|
|
||||||
]) {
|
|
||||||
googleApiUrl.hostname = hostname;
|
|
||||||
googleParams.set("client", client);
|
googleParams.set("client", client);
|
||||||
requestInit.body = googleParams.toString();
|
requestInit.body = googleParams.toString();
|
||||||
try {
|
try {
|
||||||
googleResponse = await fetch(googleApiUrl.toString(), requestInit);
|
googleResponse = await fetch(endpoint, requestInit);
|
||||||
if (googleResponse.ok) break;
|
if (googleResponse.ok) break;
|
||||||
} catch {
|
} catch {
|
||||||
// Try the compatible fallback host.
|
// Try the compatible fallback host.
|
||||||
|
|||||||
@@ -76,6 +76,25 @@ describe("Google Translate Service", () => {
|
|||||||
expect(result.data).toBeNull();
|
expect(result.data).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should prefer an XDPL proxy when configured", async () => {
|
||||||
|
global.fetch = jest.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: () =>
|
||||||
|
Promise.resolve([[["Hola", "Hello", null, null, 10]], null, "en"]),
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await translateWithGoogle(
|
||||||
|
{ text: "Hello", source_lang: "en", target_lang: "es" },
|
||||||
|
{ env: { PROXY_URLS: "https://0.example.com/jsonrpc" } }
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.code).toBe(200);
|
||||||
|
expect(fetch).toHaveBeenCalledWith(
|
||||||
|
"https://0.example.com/google/translate_a/single",
|
||||||
|
expect.any(Object)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it("should use the compatible host when the primary is unavailable", async () => {
|
it("should use the compatible host when the primary is unavailable", async () => {
|
||||||
global.fetch = jest
|
global.fetch = jest
|
||||||
.fn()
|
.fn()
|
||||||
@@ -96,7 +115,7 @@ describe("Google Translate Service", () => {
|
|||||||
expect(result.code).toBe(200);
|
expect(result.code).toBe(200);
|
||||||
expect(fetch).toHaveBeenCalledTimes(3);
|
expect(fetch).toHaveBeenCalledTimes(3);
|
||||||
expect(fetch).toHaveBeenLastCalledWith(
|
expect(fetch).toHaveBeenLastCalledWith(
|
||||||
expect.stringContaining("translate.googleapis.com"),
|
expect.stringContaining("translate.google.com"),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
body: expect.stringContaining("client=dict-chrome-ex"),
|
body: expect.stringContaining("client=dict-chrome-ex"),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user