-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
translate: refactor, add noswhere provider
Also includes tests. Signed-off-by: Semisol <[email protected]> Co-authored-by: William Casarin <[email protected]> Signed-off-by: William Casarin <[email protected]>
- Loading branch information
Showing
8 changed files
with
218 additions
and
96 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const translate_sources = new Set([ | ||
'bg', 'cs', 'da', 'de', 'el', | ||
'en', 'es', 'et', 'fi', 'fr', | ||
'hu', 'id', 'it', 'ja', 'ko', | ||
'lt', 'lv', 'nb', 'nl', 'pl', | ||
'pt', 'ro', 'ru', 'sk', 'sl', | ||
'sv', 'tr', 'uk', 'zh' | ||
]) | ||
const translate_targets = new Set([ | ||
'bg', 'cs', 'da', 'de', | ||
'el', 'en', 'en-gb', 'en-us', | ||
'es', 'et', 'fi', 'fr', | ||
'hu', 'id', 'it', 'ja', | ||
'ko', 'lt', 'lv', 'nb', | ||
'nl', 'pl', 'pt', 'pt-br', | ||
'pt-pt', 'ro', 'ru', 'sk', | ||
'sl', 'sv', 'tr', 'uk', | ||
'zh' | ||
]) | ||
|
||
module.exports = class DeepLTranslator { | ||
#deeplURL = process.env.DEEPL_URL || 'https://api.deepl.com/v2/translate' | ||
#deeplKey = process.env.DEEPL_KEY | ||
constructor() { | ||
if (!this.#deeplKey) | ||
throw new Error("expected DEEPL_KEY env var") | ||
} | ||
canTranslate(from_lang, to_lang) { | ||
return translate_sources.has(from_lang) && translate_targets.has(to_lang) | ||
} | ||
async translate(from_lang, to_lang, text) { | ||
let resp = await fetch(this.#deeplURL, { | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `DeepL-Auth-Key ${this.#deeplKey}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
text: [text], | ||
source_lang: from_lang.toUpperCase(), | ||
target_lang: to_lang.toUpperCase(), | ||
}) | ||
}) | ||
|
||
if (!resp.ok) throw new Error("error translating: API failed with " + resp.status + " " + resp.statusText) | ||
|
||
let data = await resp.json() | ||
|
||
if (data.translations && data.translations.length > 0) { | ||
return { | ||
text: data.translations[0].text | ||
} | ||
} | ||
|
||
throw new Error("error translating: no response") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = class MockTranslator { | ||
constructor() { | ||
|
||
} | ||
canTranslate(from_lang, to_lang) { | ||
return true | ||
} | ||
async translate(from_lang, to_lang, text) { | ||
return { | ||
text: "Mock translation" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module.exports = class NoswhereTranslator { | ||
#noswhereURL = process.env.NOSWHERE_URL || 'https://translate.api.noswhere.com/api' | ||
#noswhereKey = process.env.NOSWHERE_KEY | ||
#type = "default" | ||
#fromLangs = new Set() | ||
#toLangs = new Set() | ||
constructor() { | ||
if (!this.#noswhereKey) | ||
throw new Error("expected NOSWHERE_KEY env var") | ||
this.#loadTranslationLangs() | ||
} | ||
async #loadTranslationLangs() { | ||
let resp = await fetch(this.#noswhereURL + "/langs", { | ||
method: 'GET', | ||
headers: { | ||
'X-Noswhere-Key': this.#noswhereKey, | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
let data = await resp.json() | ||
if (!resp.ok) { | ||
throw new Error(`error getting translation langs: API failed with ${resp.status} ${data.error} (request: ${resp.headers.get("x-noswhere-request")})`) | ||
} | ||
if (!data[this.#type]) { | ||
throw new Error(`type ${this.#type} not supported for translation`) | ||
} | ||
this.#fromLangs = new Set(data[this.#type].from) | ||
this.#toLangs = new Set(data[this.#type].to) | ||
} | ||
canTranslate(from_lang, to_lang) { | ||
if (this.#fromLangs.size === 0) return true // assume true until we get the list of languages | ||
return this.#fromLangs.has(from_lang) && this.#toLangs.has(to_lang) | ||
} | ||
async translate(from_lang, to_lang, text) { | ||
let resp = await fetch(this.#noswhereURL + "/translate", { | ||
method: 'POST', | ||
headers: { | ||
'X-Noswhere-Key': this.#noswhereKey, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
text: text, | ||
src_lang: from_lang, | ||
dst_lang: to_lang, | ||
}) | ||
}) | ||
|
||
let data = await resp.json() | ||
if (!resp.ok) { | ||
throw new Error(`error translating: API failed with ${resp.status} ${data.error} (request: ${resp.headers.get("x-noswhere-request")})`) | ||
} | ||
|
||
if (data.result) { | ||
return { | ||
text: data.result | ||
} | ||
} | ||
|
||
throw new Error("error translating: no response") | ||
} | ||
} |
Oops, something went wrong.