javascriptcookiescallbackgoogle-translatejquery-cookie

Can't return to default language with Google Translate in javascript


i have a code uses google-translate and jquery.cookie to translate all page to 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 is english return to spanish
            $.removeCookie('googtrans',{path:'', domain: 'domain.es'});
            $.removeCookie('googtrans',{path:'', domain: '.domain.es'});
            location.reload();
        }else{ //Convert to english
            $.cookie('googtrans','/es/en',{path:''});
            location.reload();
        }
    });
});

//Google provides this 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 as a session:

enter image description here

i cant delete this cookie and return to my original language


Solution

  • I use this code witouth jquery.cookie and it works:

    function setCookie(b, h, c, f, e) {
        var a;
        if (c === 0) {
            a = ""
        } else {
            var g = new Date();
            g.setTime(g.getTime() + (c * 24 * 60 * 60 * 1000));
            a = "expires=" + g.toGMTString() + "; "
        }
        var e = (typeof e === "undefined") ? "" : "; domain=" + e;
        document.cookie = b + "=" + h + "; " + a + "path=" + f + e
    }
    
    function getCookie(d) {
        var b = d + "=";
        var a = document.cookie.split(";");
        for (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 this 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'
        );
        for (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