gpt4 book ai didi

javascript - 无法使用 javascript 中的 Google 翻译返回默认语言

转载 作者:行者123 更新时间:2023-11-30 07:56:18 25 4
gpt4 key购买 nike

我有一个代码使用 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 将我设置为一个 session :

enter image description here

我无法删除此 cookie 并返回到我原来的语言

最佳答案

我在不使用 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 才加载谷歌翻译

关于javascript - 无法使用 javascript 中的 Google 翻译返回默认语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38825957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com