Language Translator Tool
/* styles.css */
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 600px;
text-align: center;
}
h1 {
color: #333333;
}
.translator {
margin: 20px 0;
}
textarea {
width: 100%;
height: 100px;
padding: 10px;
border: 1px solid #dddddd;
border-radius: 5px;
font-size: 16px;
}
select {
padding: 10px;
margin: 10px 0;
border: 1px solid #dddddd;
border-radius: 5px;
font-size: 16px;
}
button {
background-color: #007bff;
color: #ffffff;
padding: 10px 20px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.output {
margin-top: 20px;
}
h2 {
color: #333333;
}
#output-text {
background-color: #f9f9f9;
padding: 10px;
border: 1px solid #dddddd;
border-radius: 5px;
min-height: 50px;
text-align: left;
white-space: pre-wrap;
}
// script.js
document.getElementById('translate-button').addEventListener('click', translateText);
function translateText() {
const text = document.getElementById('input-text').value;
const targetLang = document.getElementById('language-select').value;
if (text.trim() === "") {
alert("Please enter some text to translate.");
return;
}
const url = `https://api.mymemory.translated.net/get?q=${encodeURIComponent(text)}&langpair=en|${targetLang}`;
fetch(url)
.then(response => response.json())
.then(data => {
if (data.responseData) {
document.getElementById('output-text').innerText = data.responseData.translatedText;
} else {
document.getElementById('output-text').innerText = "Translation failed.";
}
})
.catch(error => {
console.error('Error:', error);
document.getElementById('output-text').innerText = "An error occurred.";
});
}
No comments:
Post a Comment