-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate POST request for Valyrian translation using FunTransl…
…ations API - Updated API integration to use POST request with URLSearchParams. - Ensured proper error handling and user feedback during translation process.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
async function translateToValyrian(text) { | ||
const apiUrl = `https://api.funtranslations.com/translate/valyrian.json`; | ||
|
||
try { | ||
const response = await fetch(apiUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: new URLSearchParams({ | ||
'text': text | ||
}) | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
if (response.ok) { | ||
return data.contents.translated; | ||
} else { | ||
throw new Error(data.error.message); | ||
} | ||
} catch (error) { | ||
console.error("Translation error:", error); | ||
return "Translation failed. Please try again later."; | ||
} | ||
} | ||
|
||
document.getElementById("translateButton").addEventListener("click", async function() { | ||
let inputText = document.getElementById("inputText").value; | ||
|
||
if (inputText.trim() === "") { | ||
document.getElementById("outputText").innerText = "Please enter some text."; | ||
return; | ||
} | ||
|
||
document.getElementById("outputText").innerText = "Translating..."; | ||
|
||
let translatedText = await translateToValyrian(inputText); | ||
|
||
document.getElementById("outputText").innerText = translatedText; | ||
}); |