Reputation: 892
I am using google translate on my website. (https://translate.google.com/manager/website/suggestions)
Everything works as I want, but how do I manually (with javascript) restore the original language button ?
The idea is this: I got my pages in my languages, so if a user changes this manually (without google translate) I want to disable the actual translation of google translate.
is this possible ?
I checked the iframe that translates generates, but I didn't find an answer there.
with kind regards
Upvotes: 3
Views: 4717
Reputation: 136
For anybody looking for this in 2024.
First bind the translator object to something:
window.googleTranslator = new google.translate.TranslateElement({
pageLanguage: 'en',
includedLanguages: 'es,de,fr'
}, 'google_translate_element');
Then find the restore
method inside it and execute it:
var restored = false;
Object.keys(window.googleTranslator).forEach((k) => {
if (restored) {
return;
}
if (typeof window.googleTranslator[k]?.restore === 'function') {
window.googleTranslator[k].restore();
restored = true;
}
});
// Also, flush the cookie that preserves the lang change across reloads
document.cookie="googtrans="
Upvotes: 3
Reputation: 1241
Try using this to restore original state:
function() {
var iframe = document.getElementsByClassName('goog-te-banner-frame')[0];
if(!iframe) return;
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var restore_el = innerDoc.getElementsByTagName("button");
for(var i = 0; i < restore_el.length; i++){
if(restore_el[i].id.indexOf("restore") >= 0) {
restore_el[i].click();
var close_el = innerDoc.getElementsByClassName("goog-close-link");
close_el[0].click();
return;
}
}
}
Upvotes: 9