cookies - Can't return to default language with Google Translate in javascript -
i have code uses google-translate , jquery.cookie translate page other language:
$(document).ready(function () { var googtrans = $.cookie('googtrans'); if (googtrans === '/es/en') { //paint specific flag var src = $('.lang-change img').attr('src').replace('flag_en.png', 'flag_es.gif'); $('.lang-change img').attr('src', src); $('#lang-change-en').attr('id', 'lang-change-es'); } $(".lang-change").on("click",function(){ if (googtrans == '/es/en') { //if language english return spanish $.removecookie('googtrans',{path:'', domain: 'domain.es'}); $.removecookie('googtrans',{path:'', domain: '.domain.es'}); location.reload(); }else{ //convert english $.cookie('googtrans','/es/en',{path:''}); location.reload(); } }); }); //google provides function function googletranslateelementinit() { new google.translate.translateelement({ pagelanguage: 'es', includedlanguages: 'en', layout: google.translate.translateelement.inlinelayout.simple, autodisplay: false }, 'google_translate_element'); }
googtrans cookie set me session:
i cant delete cookie , return original language
i use code witouth jquery.cookie , works:
function setcookie(b, h, c, f, e) { var a; if (c === 0) { = "" } else { var g = new date(); g.settime(g.gettime() + (c * 24 * 60 * 60 * 1000)); = "expires=" + g.togmtstring() + "; " } var e = (typeof e === "undefined") ? "" : "; domain=" + e; document.cookie = b + "=" + h + "; " + + "path=" + f + e } function getcookie(d) { var b = d + "="; var = document.cookie.split(";"); (var e = 0; e < a.length; e++) { var f = a[e].trim(); if (f.indexof(b) == 0) { return f.substring(b.length, f.length) } } return "" } //google provides function function googletranslateelementinit() { new google.translate.translateelement({ pagelanguage: "es", includedlanguages: "en", layout: google.translate.translateelement.inlinelayout.simple, autodisplay: false }, "google_translate_element") } //using jquery $(document).ready(function() { $(".post-owl").owlcarousel({ navigation : false, slidespeed : 300, paginationspeed : 400, singleitem:true, autoplay : 3000, }); $(".lang-change").on("click",function(){ if (googtrans == '/es/en') { setcookie("googtrans", "", 0, "/", ".domain.es"); setcookie("googtrans", "", 0, "/"); location.reload(); }else{ setcookie("googtrans", "/es/en", 0, "/", ".domain.es"); setcookie("googtrans", "/es/en", 0, "/"); location.reload() } }); var googtrans = getcookie('googtrans'); if (googtrans === '/es/en') { downloadjsatonload(); var src = $('#lang-change-en > img').attr('src').replace('flag_en.png', 'flag_es.gif'); $('#lang-change-en > img').attr('src', src); $('#lang-change-en').attr('id', 'lang-change-es'); } }); function downloadjsatonload() { var i; var paths = new array( '//translate.google.com/translate_a/element.js?cb=googletranslateelementinit' ); (i in paths) { if (typeof paths[i] !== 'string') { console.log(typeof paths[i]); continue; } var element = document.createelement("script"); element.src = paths[i]; document.body.appendchild(element); } }
only loads google translate if cookie have been set
Comments
Post a Comment