Google Translate set default language

Viewed 41551

Maybe this has an obvious solution that I'm overlooking, but I can't seem to find the correct parameter to put in to make this happen. Using the Google Translate widget on a site, I need to set the default language that the user sees when entering the site, even though the site is english.

function googleTranslateElementInit() {
    new google.translate.TranslateElement({
       pageLanguage: 'en'
    }, 'google_translate_element');
}

I've tried adding: defaultLanguage: 'fr' and tried: targetLanguage: 'fr'

I did find some nice jQuery solutions, but didn't want to bypass this if it was an easy fix.

10 Answers

You can set cookie in JS like this way

function setCookie(key, value, expiry) {
  var expires = new Date();
  expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
  document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}

and call in the following way.

function googleTranslateElementInit() {
    setCookie('googtrans', '/en/pt',1);
    new google.translate.TranslateElement({
       pageLanguage: 'en'
    }, 'google_translate_element');
}
Related