无法 return 使用 Google 翻译成 javascript 的默认语言
Can't return to default language with Google Translate in javascript
我有一个代码使用 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 将我设置为会话:
我无法删除此 cookie 和 return 到我的原始语言
我在 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);
}
}
仅加载 Google 如果已设置 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 将我设置为会话:
我无法删除此 cookie 和 return 到我的原始语言
我在 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);
}
}
仅加载 Google 如果已设置 cookie 则翻译