41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
const { DeepLkey } = require("../config.js")
|
|
const { statusIsValid } = require("./utils.js")
|
|
|
|
/**
|
|
*
|
|
* @param {string} text
|
|
* @returns {string}
|
|
*/
|
|
function translateText(text) {
|
|
const response = context.http.fetch(`https://api.deepl.com/v2/translate`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Accept: "application/json",
|
|
Authorization: `DeepL-Auth-Key ${DeepLkey}`,
|
|
"Content-Length": String(text.length),
|
|
},
|
|
body: JSON.stringify({
|
|
text: [text],
|
|
target_lang: "DE",
|
|
formality: "less",
|
|
context:
|
|
"Es handelt sich um eine Produktbeschreibung - insbesondere um die Größenbeschreibungen. Dinge wie A Länge, B Breite, C Ärmellänge etc. sollten präzise übersetzt werden, da die buchstaben hier auf ein Bild verweisen, welches du nicht kennst!",
|
|
}),
|
|
})
|
|
|
|
if (!statusIsValid(response.status)) {
|
|
console.log(JSON.stringify(response), JSON.stringify(response.body.json()))
|
|
throw {
|
|
status: response.status,
|
|
error: response.statusText,
|
|
}
|
|
}
|
|
const res = response.body.json()
|
|
const textMerged = res.translations.map((t) => t.text).join(" ")
|
|
return textMerged
|
|
}
|
|
module.exports = {
|
|
translateText,
|
|
}
|