无法使用Google翻译在javascript中恢复默认语言

时间:2016-08-08 09:45:55

标签: javascript cookies callback google-translate jquery-cookie

我有一个代码使用google-translate和jquery.cookie将所有页面翻译成其他语言:

$(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将我设为会话:

enter image description here

我无法删除此Cookie并返回原始语言

1 个答案:

答案 0 :(得分:3)

我在jquery.cookie中使用这个代码并且它有效:

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);
    }
}

如果已设置Cookie,则仅加载Google翻译