0
I am using the following script to translate a website.
<div id="google_translate_element"></div>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<select class="select-language" onchange="ChangeLang(this.value)">
<option value="pt">Português</option>
<option value="en">Inglês</option>
<option value="es">Espanhol</option>
<option value="it">Italiano</option>
<option value="de">Alemão</option>
<option value="fr">Francês</option>
</select>
And
function ChangeLang(a) {
var b, elemento = "";
if (document.createEvent) {
var c = document.createEvent("HTMLEvents");
c.initEvent("click", true, true)
}
if (a == 'pt') {
elemento = $(".goog-te-banner-frame:eq(0)").contents().find("button[id*='restore']")
} else {
switch (a) {
case 'de':
b = "alem";
break;
case 'es':
b = "espanhol";
break;
case 'fr':
b = "fran";
break;
case 'en':
b = "ing";
break;
case 'it':
b = "italiano";
break
}
elemento = $(".goog-te-menu-frame:eq(0)").contents().find("span:contains('" + b + "')");
}
if (elemento.length > 0) {
if (document.createEvent) {
elemento[0].dispatchEvent(c)
} else {
elemento[0].click()
}
}
}
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'pt',
autoDisplay: false,
includedLanguages: 'de,es,fr,en,it',
layout: google.translate.TranslateElement.InlineLayout.SIMPLE
},
'google_translate_element');
}
The files are being called, no error is generated and also no translation occurs.
@Edit Testing with onclick instead of onchange works as expected.
Just out of curiosity why
onchange
the value of the function?ChangeLang('pt')
– Maicon Carraro
My error, @Maiconcarraro. Correct is Changelang(this.value)
– Marcelo de Andrade
Is working again?
– Maicon Carraro
It worked as expected using onclick instead of onchange.
– Marcelo de Andrade