0

Similar to this post, I am wondering how to set the cookie data on the GTranslate WordPress plugin so that it never expires. Currently, I have a website with two language options: English and Spanish. If a user logs in and selects Spanish, I want them to be able to log out, close the browser, open the browser, log back in, and see the website in Spanish. Now, it appears that their language selection expires at the end of each Session, according to the cookie data in dev tools. Here is the Widget code for the GTranslate plugin - is there a way to modify this code so that the cookie never expires, and the user can see their selected language each time they log in? Thanks for any leads.

<!-- GTranslate: https://gtranslate.io/ -->
<a href="#" onclick="doGTranslate('en|en');return false;" title="English" class="glink nturl notranslate">English</a> <a href="#" onclick="doGTranslate('en|es');return false;" title="Español" class="glink nturl notranslate">Español</a> <style type="text/css">
#goog-gt-tt {display:none !important;}
.goog-te-banner-frame {display:none !important;}
.goog-te-menu-value:hover {text-decoration:none !important;}
.goog-text-highlight {background-color:transparent !important;box-shadow:none !important;}
body {top:0 !important;}
#google_translate_element2 {display:none!important;}
</style>

<div id="google_translate_element2"></div>
<script type="text/javascript">
function googleTranslateElementInit2() {new google.translate.TranslateElement({pageLanguage: 'en',autoDisplay: false}, 'google_translate_element2');}
</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit2"></script>


<script type="text/javascript">
function GTranslateGetCurrentLang() {var keyValue = document['cookie'].match('(^|;) ?googtrans=([^;]*)(;|$)');return keyValue ? keyValue[2].split('/')[2] : null;}
function GTranslateFireEvent(element,event){try{if(document.createEventObject){var evt=document.createEventObject();element.fireEvent('on'+event,evt)}else{var evt=document.createEvent('HTMLEvents');evt.initEvent(event,true,true);element.dispatchEvent(evt)}}catch(e){}}
function doGTranslate(lang_pair){if(lang_pair.value)lang_pair=lang_pair.value;if(lang_pair=='')return;var lang=lang_pair.split('|')[1];if(GTranslateGetCurrentLang() == null && lang == lang_pair.split('|')[0])return;if(typeof ga!='undefined'){ga('send', 'event', 'GTranslate', lang, location.hostname+location.pathname+location.search);}else{if(typeof _gaq!='undefined')_gaq.push(['_trackEvent', 'GTranslate', lang, location.hostname+location.pathname+location.search]);}var teCombo;var sel=document.getElementsByTagName('select');for(var i=0;i<sel.length;i++)if(/goog-te-combo/.test(sel[i].className)){teCombo=sel[i];break;}if(document.getElementById('google_translate_element2')==null||document.getElementById('google_translate_element2').innerHTML.length==0||teCombo.length==0||teCombo.innerHTML.length==0){setTimeout(function(){doGTranslate(lang_pair)},500)}else{teCombo.value=lang;GTranslateFireEvent(teCombo,'change');GTranslateFireEvent(teCombo,'change')}}
</script>

Jon
  • 622
  • 8
  • 29

1 Answers1

0

Set another custom cookie in server-side so even if the googtrans cookie is expired after the session, it will remake the session after reading your custom-made cookie.

For example, I have this PHP code here to set a custom cookie when a user wants to translate from English to Japanese:

function set_jap_lang()
{
    if(!isset($_COOKIE['your_custom_cookie'])) 
    {

        // set a cookie for 1 week
        setcookie('your_custom_cookie', 'ja', time() + (86400 * 7), COOKIEPATH, COOKIE_DOMAIN);
 
    }
    else
    {
        //unset and set again in case if lang is in other language
        setcookie('your_custom_cookie', '', time() - (86400 * 7), COOKIEPATH, COOKIE_DOMAIN);

        setcookie('tia_lang', 'ja', time() + (86400 * 7), COOKIEPATH, COOKIE_DOMAIN);
    }
}

Now since you have this ajax function, even if googtrans cookie expires after the session, your custom cookie will still be there. Moreover, you can set it to expire anytime you want.

The second thing you would do now is to detect in JQuery when a user has your custom cookie for translation. Use google's div google_translate_element2 to be a detection div in jquery (Since that div exists in your translation on any page).

if($('#google_translate_element2').length) //if translation div exists anywhere on page
{
    let lang = getCookie('your_custom_cookie');
    if(lang == null) return;

    doGTranslate("en|"+lang); //this is the function GTranslate uses to translate the page
}
function getCookie(name)
{
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}

Now if ever a user wants to translate my English page to Japanese, my custom cookie will then have the value of ja that will then be concatenated to the GTranslate function doGTranslate("en|"+lang) = doGTranslate("en|ja")

rochiey
  • 29
  • 7