translate-tools / linguist

Translate web pages, highlighted text, Netflix subtitles, private messages, speak the translated text, and save important translations to your personal dictionary to learn words even offline
https://linguister.io
BSD 3-Clause "New" or "Revised" License
704 stars 24 forks source link

API integration: Mozhi #399

Closed Terkyz closed 7 months ago

Terkyz commented 1 year ago

Mozhi as their Codeberg page explains "It was initially made as a maintained fork/rewrite of simplytranslate, but has grown to have a lot more features as well!"

What personally attracted my attention about this front-end is that it supports DeepL without using an API Key, I know that many front-ends already support this and that due to usage the API is blocked, but having more options is always good.

I have no knowledge in this area, but it seems to me that this link about the API documentation can be useful.

vitonsky commented 1 year ago

It looks good.

I tried to call public API with code below, it works for me

await fetch("https://translate.bus-hit.me/api/translate?engine=deepl&from=en&to=ru&text=Hello%20world", {
    "credentials": "omit",
    "headers": {
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
        "Accept": "application/json",
        "Accept-Language": "en-US,en;q=0.5",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin"
    },
    "method": "GET",
    "mode": "cors"
}).then(r=>r.json());

Thanks for links, i will research it and maybe will implement custom translator with this service

Terkyz commented 7 months ago

When I try to add Mozhi as a custom language to Linguistic by copying your code, I get this error:

"await is only valid in async functions, async generators and modules"

vitonsky commented 7 months ago

If you need code for translator, you need a class that implements a custom translator API.

I implemented it for you, but when i tried, it works fine for single text translation, but it does not work for pages translation, due to API limitations:

class MozhiTranslator {
    translate = (text, from, to) => {
        return fetch(`https://translate.bus-hit.me/api/translate?engine=deepl&from=${from}&to=${to}&text=${encodeURIComponent(text)}`, {
            "credentials": "omit",
            "headers": {
                "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
                "Accept": "application/json",
                "Accept-Language": "en-US,en;q=0.5",
                "Sec-Fetch-Dest": "empty",
                "Sec-Fetch-Mode": "cors",
                "Sec-Fetch-Site": "same-origin"
            },
            "method": "GET",
            "mode": "cors"
        })
            .then((r) => r.json())
            .then((r) => r['translated-text']);
    };

    translateBatch = (texts, from, to) =>
        Promise.all(texts.map((text) => this.translate(text, from, to)));

    getLengthLimit = () => 5000;
    getRequestsTimeout = () => 300;
    checkLimitExceeding = (text) => {
        const textLength = !Array.isArray(text)
            ? text.length
            : text.reduce((len, text) => len + text.length, 0);

        return textLength - this.getLengthLimit();
    };

    static isSupportedAutoFrom = () => false;

    // prettier-ignore
    static getSupportedLanguages = () => [
  "af",
  "sq",
  "am",
  "ar",
  "hy",
  "as",
  "ay",
  "az",
  "bm",
  "eu",
  "be",
  "bn",
  "bho",
  "bs",
  "bg",
  "ca",
  "ceb",
  "ny",
  "zh",
  "co",
  "hr",
  "cs",
  "da",
  "dv",
  "doi",
  "nl",
  "en",
  "eo",
  "et",
  "ee",
  "tl",
  "fi",
  "fr",
  "fy",
  "gl",
  "ka",
  "de",
  "el",
  "gn",
  "gu",
  "ht",
  "ha",
  "haw",
  "iw",
  "hi",
  "hmn",
  "hu",
  "is",
  "ig",
  "ilo",
  "id",
  "ga",
  "it",
  "ja",
  "jw",
  "kn",
  "kk",
  "km",
  "rw",
  "gom",
  "ko",
  "kri",
  "ku",
  "ckb",
  "ky",
  "lo",
  "la",
  "lv",
  "ln",
  "lt",
  "lg",
  "lb",
  "mk",
  "mai",
  "mg",
  "ms",
  "ml",
  "mt",
  "mi",
  "mr",
  "lus",
  "mn",
  "my",
  "ne",
  "no",
  "or",
  "om",
  "ps",
  "fa",
  "pl",
  "pt",
  "pa",
  "qu",
  "ro",
  "ru",
  "sm",
  "sa",
  "gd",
  "nso",
  "sr",
  "st",
  "sn",
  "sd",
  "si",
  "sk",
  "sl",
  "so",
  "es",
  "su",
  "sw",
  "sv",
  "tg",
  "ta",
  "tt",
  "te",
  "th",
  "ti",
  "ts",
  "tr",
  "tk",
  "ak",
  "uk",
  "ur",
  "ug",
  "uz",
  "vi",
  "cy",
  "xh",
  "yi",
  "yo",
  "zu"
];
}

MozhiTranslator;
Terkyz commented 7 months ago

Hmm, I tried it and it doesn't even translate a word... Thank you for your work and sorry if I wasted your time.

vitonsky commented 7 months ago

It's ok. Anyway it's good to know about translation API in the world