Language Translator Tool
Language Translator
body {
font-family: Arial, sans-serif;
background-color: #f3f3f3;
color: #333;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
background-color: #fff;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 80%;
max-width: 500px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
font-weight: bold;
}
textarea,
select {
width: 100%;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #007BFF;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
width: 100%;
}
.translated-text {
margin-top: 20px;
}
h2 {
margin-top: 0;
}
document.addEventListener("DOMContentLoaded", function () {
const translateBtn = document.getElementById("translateBtn");
const sourceText = document.getElementById("sourceText");
const targetLang = document.getElementById("targetLang");
const output = document.getElementById("output");
translateBtn.addEventListener("click", function () {
const textToTranslate = sourceText.value;
const langCode = targetLang.value;
if (textToTranslate.trim() === "") {
alert("Please enter some text to translate.");
return;
}
translateText(textToTranslate, langCode).then((translatedText) => {
output.textContent = translatedText;
});
});
async function translateText(text, targetLang) {
const response = await fetch(
`https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20230729T000834Z.xxxxxxxx&text=${encodeURIComponent(
text
)}&lang=${targetLang}`
);
if (!response.ok) {
throw new Error("Failed to fetch translation.");
}
const data = await response.json();
return data.text[0];
}
});
0 Comments