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
676 stars 21 forks source link

Some languages supported by custom translators are not displaying in the dropdown menu #374

Open Prasanta-Hembram opened 1 year ago

Prasanta-Hembram commented 1 year ago

Description

I have created a custom translator that uses the Mymemory API for translation. Thanks to @AlphaBack for making it work! However, when I add the translator to the custom translator list of the extension, languages such as Santali (code: sat), which Mymemory supports, do not display in the drop-down menu when I am translating using the extension.

Steps

  1. Copy the below custom translator code of Mymemory. (Added Santali for demo)

    
    class MyMemoryTranslator {
    
    apiPath = 'https://api.mymemory.translated.net/get';
    
    translate = (text, from, to) => {
        return fetch(`${this.apiPath}?q=${text}&langpair=${from}|${to}`, {
            credentials: 'omit',
            headers: {
                'User-Agent':
                    'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/114.0',
                Accept: '*/*',
                '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(({ responseData }) => responseData.translatedText);
    };
    
    translateBatch = (texts, from, to) =>
        Promise.all(texts.map((text) => this.translate(text, from, to)));
    
    getLengthLimit = () => 4000;
    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 = () => true;
    
    // prettier-ignore
    static getSupportedLanguages = () => [
        "en", "ar", "az", "zh", "cs",
        "nl", "eo", "fi", "fr", "de",
        "el", "hi", "hu", "id", "ga",
        "it", "ja", "ko", "fa", "pl",
        "pt", "ru", "sat", "sk", "es", "sv",
        "tr", "uk", "vi"
    ];
    }

MyMemoryTranslator;



2. Go to Translation preferences and add it to Custom translators by giving any name and save it.
3. Select that from Translator module.
4. Now try to translate some sentences from English to Santali.
5. Santali language is missing.

## Expected

It should show Santali language as an option.
vitonsky commented 1 year ago

Currently, Linguist supports only 2-letter ISO-639-1 language codes.

As i see, ISO 639-2 language codes have sat code. To fix the problem, we have to supports ISO 639-2 language codes. Also, we should to update custom translators docs.

We have to ensure ISO 639-1 language codes will works fine too, to implement it, we have to add mapper for ISO codes.

This problem serious, i will fix it as soon as possible and will release in next hot fix update. Thank you for report.

Prasanta-Hembram commented 3 months ago

return fetch(${this.apiPath}?q=${text}&langpair=${from}|${to}, {

to

return fetch(${this.apiPath}?q=${text}&langpair=${from}|sat, {

I have found a quick workaround, if we replace variable ${to} with simply a language code (three letter) then it is working fine. then select any language from the target language it will translate it properly in that desired language.