How to add Google Translate to your website

Sometimes you would like to add to your website a quick widget to allow your users to translate into their language. Google has a simple widget that you can add with some lines of code:

<script>
    function googleTranslateElementInit() {
        new google.translate.TranslateElement(
            //here you add the supported languages like en, es, zh, nl, de, etc.
            {pageLanguage: 'en'},
            'google_translate_element'
        );
    }
</script>
<script
src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit">
</script>

<div id="google_translate_element"></div>

And that's all.

Comments