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

Image translate #38

Open
FrontEnd07 opened this issue Feb 23, 2024 · 6 comments
Open

Image translate #38

FrontEnd07 opened this issue Feb 23, 2024 · 6 comments

Comments

@FrontEnd07
Copy link

Thank you for the open source code, I have a question: can it be translated using an image?

@cjvnjde
Copy link
Owner

cjvnjde commented Feb 25, 2024

In theory, yes. However, I couldn't find a way to make it work.

Here is the code I tested. It consistently responds with the same image data without any translation.

import https from "node:https";
import fs from 'node:fs';
import path from 'node:path';
import zlib from 'node:zlib';

export function translate(filePath) {
    const imagePath = path.resolve(__dirname, filePath);
    const fileData = fs.readFileSync(imagePath);
    const base64 = Buffer.from(fileData).toString('base64');

    return new Promise((resolve, reject) => {
        let body = encodeURIComponent(`[[["WqWDPb","[[\\"${base64}\\",\\"image/png\\"],\\"en\\",\\"es\\"]",null,"generic"]]]`);
        body = `f.req=${body}&`;

        const searchParams = new URLSearchParams({
            rpcids: "WqWDPb",
            "source-path": "/",
            "bl": "boq_translate-webserver_20240220.05_p0",
            hl: "en-US",
            "soc-app": "1",
            "soc-platform": "1",
            "soc-device": "1",
            rt: "c"
        });

        const url = `https://translate.google.com/_/TranslateWebserverUi/data/batchexecute?${searchParams}`;

        const req = https
            .request(
                url,
                {
                    method: "POST",
                    headers: {
                        "Accept-Encoding": "gzip, deflate, br",
                        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
                        "Content-Length": Buffer.byteLength(body),
                    }
                },
                (resp) => {
                    const gunzip = zlib.createGunzip();
                    resp.pipe(gunzip);
                    let data = "";

                    gunzip.on("data", (chunk) => {
                        data += chunk;
                    });

                    gunzip.on("end", () => {
                        resolve(data);
                    });
                },
            )
            .on("error", reject);

        req.write(body);
        req.end();
    });
}

translate("image.png").then((data) => {
    console.log(data)
})

@FrontEnd07
Copy link
Author

Anyway, did you manage to read the image?

@cjvnjde
Copy link
Owner

cjvnjde commented Mar 3, 2024

It uses images in base64 encoding.

In this example, the selected text is an image:
image

@StarkGang
Copy link

@cjvnjde Did you manage to get translated image? I am getting same Image I sent.

@cjvnjde
Copy link
Owner

cjvnjde commented Apr 4, 2024

no, most likely we need to pass the X-Goog-BatchExecute-Bgr header to be able to translate images. But the algorithm by which it is generated is a mystery

@StarkGang
Copy link

no, most likely we need to pass the X-Goog-BatchExecute-Bgr header to be able to translate images. But the algorithm by which it is generated is a mystery

I tried passing it, but same response. Google is good at hiding stuffs, IG. Pls do let me know if you get some luck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants