import Pipeline from "https://esm.town/v/iamseeley/pipeline";
export default async function handler(req) {
if (req.method === "GET") {
return new Response(`
<!DOCTYPE html>
<html>
<body>
<h2>Translate English to German</h2>
<form id="translationForm">
<label for="text">English Text:</label><br>
<input type="text" id="text" name="text"><br><br>
<input type="submit" value="Translate">
</form>
<div id="result"></div>
<script>
document.getElementById('translationForm').addEventListener('submit', async function(event) {
event.preventDefault();
const formData = new FormData(this);
const text = formData.get('text');
const response = await fetch(window.location.href, {
method: 'POST',
body: formData
});
const result = await response.json();
let translatedText = 'Translation error';
if (result && result[0] && result[0].translation_text) {
translatedText = result[0].translation_text;
}
document.getElementById('result').innerHTML = '<p>Original: ' + text + '</p><p>Translated: ' + translatedText + '</p>';
});
</script>
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
iamseeley-exampletranslation.web.val.run
Updated: June 19, 2024