Created
April 4, 2016 09:09
-
-
Save artturik/15bed885bcec6faa95eb73acb2e2ae54 to your computer and use it in GitHub Desktop.
Forced Google Translate (also can be used as April Fools Day joke)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
This code will translate page contents automatically (without user input) | |
Settings located at line 9, current script will translate english to estonian | |
--> | |
<style>#google_translate_element,.skiptranslate{display:none;}body{top:0!important;}</style> | |
<div id="google_translate_element"></div> | |
<script> | |
function googleTranslateElementInit() { | |
new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'et', autoDisplay: false}, 'google_translate_element'); | |
var a = document.querySelector("#google_translate_element select"); | |
a.selectedIndex=1; | |
a.dispatchEvent(new Event('change')); | |
} | |
</script> | |
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script> |
This will only translate the page if the browser is NOT english
<div id="google_translate_element"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
/*
Get the current browser language.
*/
var langBrowser = navigator.language || navigator.userLanguage;
/* If the language contains a region code, get only the 2-character lang code. For example en-US -> en. */
var currentBLang = langBrowser.split('-')[0];
new google.translate.TranslateElement({
pageLanguage: 'en',
}, 'google_translate_element');
if (currentBLang != "en"){
setTimeout(function() {
// Set the default language to Spanish
var selectElement = document.querySelector('#google_translate_element select');
selectElement.value = currentBLang;
selectElement.dispatchEvent(new Event('change'));
}, 1000);
}
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
Change your chrome browser to a different default language and test it for yourself
https://www.indeed.com/career-advice/career-development/change-language-in-chrome
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Load the above code at the beginning of the body.
Then place
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
at the end of the body.