Sujay's Free Tools(code of Language Translator tool)
Certainly! Below is a basic example of a language translator tool using HTML, CSS, and JavaScript. Note that this example uses the MyMemory translation service, which doesn't require an API key.
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Language Translator Tool</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#translator-container {
max-width: 400px;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
select, textarea, button {
width: 100%;
margin-bottom: 10px;
padding: 10px;
font-size: 16px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div id="translator-container">
<h2>Language Translator</h2>
<label for="source-language">Source Language:</label>
<select id="source-language">
<option value="en">English</option>
<option value="es">Spanish</option>
<!-- Add more language options as needed -->
</select>
<label for="target-language">Target Language:</label>
<select id="target-language">
<option value="en">English</option>
<option value="es">Spanish</option>
<!-- Add more language options as needed -->
</select>
<label for="text-to-translate">Text to Translate:</label>
<textarea id="text-to-translate" rows="4"></textarea>
<button onclick="translate()">Translate</button>
<label for="translated-text">Translated Text:</label>
<textarea id="translated-text" rows="4" readonly></textarea>
</div>
<script>
function translate() {
const sourceLanguage = document.getElementById('source-language').value;
const targetLanguage = document.getElementById('target-language').value;
const textToTranslate = document.getElementById('text-to-translate').value;
// You can replace this URL with any other translation service that suits your needs.
const apiUrl = `https://api.mymemory.translated.net/get?q=${encodeURIComponent(textToTranslate)}&langpair=${sourceLanguage}|${targetLanguage}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.responseStatus === 200) {
document.getElementById('translated-text').value = data.matches[0].translation;
} else {
document.getElementById('translated-text').value = 'Error translating text.';
}
})
.catch(error => {
console.error('Error:', error);
document.getElementById('translated-text').value = 'Error translating text.';
});
}
</script>
</body>
</html>
```
This code creates a simple responsive language translator tool with styling. Remember to add more language options to the select elements if needed. Also, note that MyMemory translation service may have limitations, and for a production environment, you might want to consider using a more robust API with proper authentication.
Comments
Post a Comment