Placeholder translation
Demonstration of translating placeholders with Google Translate.
by sgamon
HTML
<div id="google_translate_element" style="float:left; padding-left:15px"></div>
<form><label id ="aaa" style ="display:none ">Enter youre</label>
<input type="text" placeholder="Enter text here" />
<input type="submit" value="Submit" />
</form>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'ca,da,de,el,en,es,fr,it,ja,ko,nl,pl,pt,ru,sv,tl', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<style>
div#google_translate_element div.goog-te-gadget-simple{background-color:green;}
div#google_translate_element div.goog-te-gadget-simple a.goog-te-menu-value span{color:yellow}
</style>
JavaScript
// Find all placeholders
var placeholders = document.querySelectorAll('input[placeholder]');
if (placeholders.length) {
// convert to array
placeholders = Array.prototype.slice.call(placeholders);
// copy placeholder text to a hidden div
var div = $('<div id="placeholders" style="display:none;"></div>');
placeholders.forEach(function(input){
var text = input.placeholder;
div.append('<div>' + text + '</div>');
});
$('body').append(div);
// save the first placeholder in a closure
var originalPH = placeholders[0].placeholder;
// check for changes and update as needed
setInterval(function(){
if (isTranslated()) {
updatePlaceholders();
originalPH = placeholders[0].placeholder;
}
}, 500);
// hoisted ---------------------------
function isTranslated() { // true if the text has been translated
var currentPH = $($('#placeholders > div')[0]).text();
return !(originalPH == currentPH);
}
function updatePlaceholders() {
$('#placeholders > div').each(function(i, div){
placeholders[i].placeholder = $(div).text();
});
}
}