Here’s an HTML code for a language translator widget using Google Translate’s built-in functionality. This widget allows users to translate your website content into multiple languages:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Language Translator Widget</title>
<style>
/* Widget container styling */
#google_translate_element {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 9999;
background: white;
padding: 10px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
/* Dropdown styling */
.goog-te-combo {
padding: 8px 12px;
border-radius: 4px;
border: 1px solid #ddd;
background-color: #f8f9fa;
color: #333;
cursor: pointer;
}
/* Hide Google branding */
.goog-te-gadget {
color: transparent !important;
}
.goog-te-banner-frame {
display: none !important;
}
/* Language list styling */
.goog-te-menu-value {
color: #333 !important;
}
/* Hover effects */
.goog-te-combo:hover {
background-color: #e9ecef;
}
/* Mobile responsiveness */
@media (max-width: 480px) {
#google_translate_element {
bottom: 10px;
right: 10px;
}
}
</style>
</head>
<body>
<!-- Your website content -->
<h1>Welcome to Our Multilingual Website</h1>
<p>This content will be automatically translated to your preferred language.</p>
<!-- Google Translate Widget -->
<div id="google_translate_element"></div>
<script>
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en',
includedLanguages: 'en,es,fr,de,it,pt,ru,zh-CN,ja,ko,ar,hi',
layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL
}, 'google_translate_element');
}
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
</body>
</html>
Key Features:
- Fixed position widget in the bottom-right corner
- Custom styling for the dropdown menu
- Support for 12 major languages (modifiable)
- Mobile-responsive design
- Hidden Google branding
- Shadow effect and hover states
- Automatic translation of page content
Customization Options:
- Change position by modifying the
position
,bottom
, andright
properties - Add more languages to the
includedLanguages
list (comma-separated language codes) - Modify colors by changing the CSS variables
- Adjust the layout (horizontal/vertical) using the
layout
property - Change default language by modifying
pageLanguage
Limitations:
- Requires internet connection
- Uses Google Translate’s free version
- Translations might not be 100% accurate
- Dynamic content added via JavaScript might not auto-translate
To Use:
- Simply copy this code into your HTML file
- Place it before the closing
</body>
tag - Customize the included languages and styling as needed
The widget will automatically translate all text content on your page while maintaining the original layout and structure. Users can select their preferred language from the dropdown menu.