Skip to content

Instantly share code, notes, and snippets.

@jwenjian
Created December 29, 2022 03:32
Show Gist options
  • Save jwenjian/8c7c70801018aa9699da76ad52681424 to your computer and use it in GitHub Desktop.
Save jwenjian/8c7c70801018aa9699da76ad52681424 to your computer and use it in GitHub Desktop.
lingva translator for linguist translation extention
class LingvaTranslator {
// URL of your instance of LingvaTranslate
apiPath = "https://lingva.ml";
translate = (text, from, to) => {
return fetch(`${this.apiPath}/api/v1/${from}/${to}/${text}`, {
credentials: "omit",
headers: {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.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(({ translation }) => translation);
};
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;
static getSupportedLanguages = () => [
"en", "ar", "az", "zh", "cs",
"nl", "eo", "fi", "fr", "de",
"el", "hi", "hu", "id", "ga",
"it", "ja", "ko", "fa", "pl",
"pt", "ru", "sk", "es", "sv",
"tr", "uk", "vi"
];
}
LingvaTranslator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment