Search code examples
cookiesgoogle-translate

How to set that google translate cookie never expires?


Im using the google translate code on my page and it works good, but if I look at the cookie it says that it only have expiration during the session!? So I want to set it so it does´t expire, so that it is the same language when the user comes back as he choosed the first time.

Im using this now.

SOLVED! OK so with this the user can select a language and the next time he visit the page it is translated to the language he picked before!

var ckDomain;
function googleTranslateElementInit() {

    function getCookie(name)
  {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
  }



 var kakan;
 var googkakan;


 kakan=getCookie("googtrans22");


$$(document).on('change', '#google_translate_element', function (e) {
    setTimeout(function(){
function getCookie(name)
  {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
  }

 googkakan=getCookie("googtrans");

 document.cookie = "googtrans22="+googkakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
 document.cookie = "googtrans22="+googkakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";


    },1000);
}); 

  for (var ckDomain = window.location.hostname.split("."); 2 < ckDomain.length;){
    ckDomain.shift();
  }
  ckDomain = ";domain=" + ckDomain.join(".");
  // domain cookie
  document.cookie = "googtrans="+kakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
  // host-only cookie (with no domain name definition)
  document.cookie = "googtrans="+kakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";



  new google.translate.TranslateElement({
    pageLanguage: 'sv',
    autoDisplay: false,
    layout: google.translate.TranslateElement
  }, 'google_translate_element');

}



    (function() {
          var googleTranslateScript = document.createElement('script');
          googleTranslateScript.type = 'text/javascript';
          googleTranslateScript.async = true;
          googleTranslateScript.src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
          ( document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0] ).appendChild( googleTranslateScript );
        })();

Solution

  • Apparently the library is forced to write over the cookie to make it expire at end of the session.

    The good news is that before doing that it reads the existing cookie, so you can feed it before each initialization call.

    To force the library to translate a Swedish page into English:

    function googleTranslateElementInit() {
      var ckDomain;
      for (var ckDomain = window.location.hostname.split("."); 2 < ckDomain.length;){
        ckDomain.shift();
      }
      ckDomain = ";domain=" + ckDomain.join(".");
      // domain cookie
      document.cookie = "googtrans=/sv/en; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
      // host-only cookie (with no domain name definition)
      document.cookie = "googtrans=/sv/en; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";
      new google.translate.TranslateElement({
        pageLanguage: 'sv',
        autoDisplay: false,
        layout: google.translate.TranslateElement
      }, 'google_translate_element');
    }