Created
August 5, 2023 10:35
-
-
Save janvarev/314b13e4d44d5a0cc349385ee85a4f58 to your computer and use it in GitHub Desktop.
OneRingTranslator support JS for Linguist extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Homepage: https://github.com/janvarev/OneRingTranslator | |
*/ | |
class OneRingTranslator { | |
// URL of your instance of OneRingTranslator | |
// for local instance use URL "http://localhost/translate" | |
apiPath = 'http://127.0.0.1:4990/translate'; | |
// Insert API key if you have | |
apiKey = ''; | |
translate = (text, from, to) => { | |
const params = { | |
text: text, | |
from_lang: from, | |
to_lang: to, | |
api_key: this.apiKey, | |
} | |
return fetch(this.apiPath + '?' + (new URLSearchParams(params)).toString(), { | |
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', | |
'Content-Type': 'application/json', | |
}, | |
method: 'GET', | |
mode: 'cors', | |
}) | |
.then((r) => r.json()) | |
.then((r) => r.result); | |
}; | |
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 = () => [ // got from Google Translate, may need adjustment for your plugins | |
'af', 'ak', 'am', 'ar', 'as', 'ay', 'az', 'be', 'bg', 'bho', | |
'bm', 'bn', 'bs', 'ca', 'ceb', 'ckb', 'co', 'cs', 'cy', 'da', | |
'de', 'doi', 'dv', 'ee', 'el', 'en', 'eo', 'es', 'et', 'eu', | |
'fa', 'fi', 'fr', 'fy', 'ga', 'gd', 'gl', 'gn', 'gom', 'gu', | |
'ha', 'haw', 'hi', 'hmn', 'hr', 'ht', 'hu', 'hy', 'id', 'ig', | |
'ilo', 'is', 'it', 'iw', 'ja', 'jw', 'ka', 'kk', 'km', 'kn', | |
'ko', 'kri', 'ku', 'ky', 'la', 'lb', 'lg', 'ln', 'lo', 'lt', | |
'lus', 'lv', 'mai', 'mg', 'mi', 'mk', 'ml', 'mn', 'mni-Mtei', 'mr', | |
'ms', 'mt', 'my', 'ne', 'nl', 'no', 'nso', 'ny', 'om', 'or', | |
'pa', 'pl', 'ps', 'pt', 'qu', 'ro', 'ru', 'rw', 'sa', 'sd', | |
'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'st', 'su', | |
'sv', 'sw', 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tr', | |
'ts', 'tt', 'ug', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'yo', | |
'zh-CN', 'zh-TW', 'zu' | |
]; | |
} | |
OneRingTranslator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment