Skip to content

Instantly share code, notes, and snippets.

@tedw
Created March 11, 2021 20:50
Show Gist options
  • Save tedw/fac000d3561ac2bb1f52a74fb2af5ead to your computer and use it in GitHub Desktop.
Save tedw/fac000d3561ac2bb1f52a74fb2af5ead to your computer and use it in GitHub Desktop.
Google Translate widget
<div id="google_translate_element"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
var translateEl= new google.translate.TranslateElement({
includedLanguages: 'en,es',
pageLanguage: 'en'
// autoDisplay: false,// doesn’t seem to have any effect
// multilanguagePage: true// will always include the page language as an option
// NOTE: Setting the layout option will replace the <select> with inaccessible markup
// layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
// layout: google.translate.TranslateElement.FloatPosition.TOP_RIGHT// seems to be the same as SIMPLE
}, 'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
@tedw
Copy link
Author

tedw commented Mar 11, 2021

Please note that usage will be restricted to government, non-profit, and/or non-commercial websites (e.g. academic institutions) that focus on COVID-19 response.

See https://developers.google.com/search/blog/2020/05/google-translates-website-translator

@dandeancook
Copy link

the pageLanguage is 'en', but the translate button itself is following the language of country by IP; how to fix this?

@tedw
Copy link
Author

tedw commented Sep 11, 2023

@dandeancook pageLanguage defines the original language of the page, not the user’s preferred language.

@dandeancook
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment