0

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 );
        })();
Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86

1 Answers1

2

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');
}
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • Thanks a lot. Yes this works great. But I want to get the language from the cookie, so when the user chooses a new language and that is set in the cookie, then that language is used the next time the user visit the page. That way the user can select any language that he likes. I have the read the existing cookie and use that, I have to check how to do that :-) – Claes Gustavsson Mar 08 '17 at 07:35
  • Ok Kul-Tigin, thanks a lot for your help. Now it works! – Claes Gustavsson Mar 08 '17 at 07:47
  • My bad, I was a little bit to quick :-), off course it does´t work quite yet, when the browser closes then the cookie is deleted, so I need to set the expiration so it never expires. I guess after all this code, I have to test. – Claes Gustavsson Mar 08 '17 at 09:00
  • Glad I can help. – Kul-Tigin Mar 08 '17 at 14:27