The google translate API is used for translation:
function translate() {
const inputText = inputTextElem.value;
const inputLanguage =
inputLanguageDropdown.querySelector(".selected").dataset.value;
const outputLanguage =
outputLanguageDropdown.querySelector(".selected").dataset.value;
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${inputLanguage}&tl=${outputLanguage}&dt=t&q=${encodeURI(inputText
)}`;
fetch(url)
.then((response) => response.json())//fetches response
.then((json) => {
console.log(json);//prints output
outputTextElem.value = json[0].map((item) => item[0]).join("");//maps the item where to connect and print
})
.catch((error) => {
console.log(error);//Exception Handling
});
}
Changed usage to systran translate API according to instructions: https://docs.systran.net/translateAPI/translation/
function translate() {
const inputText = inputTextElem.value;
const inputLanguage = inputLanguageDropdown.querySelector(".selected").dataset.value;
const outputLanguage = outputLanguageDropdown.querySelector(".selected").dataset.value;
const apiKey = "YOUR_API_KEY";
const url = `https://api.example.com/translation?source=${inputLanguage}&target=${outputLanguage}&input=${encodeURIComponent(inputText)}`;
const headers = {
"Authorization": `Key ${apiKey}`
};
fetch(url, {
method: "GET",
headers: headers
})
.then(response => response.json())
.then(json => {
console.log(json);
outputTextElem.value = json.translation;
})
.catch(error => {
console.error("Translation error:", error);
});
}
Does not work. What is the problem? Help please.