简单的使用JS将google翻译实现简单的企业多语言外贸网站多语言翻译功能

作者: 2024-08-26 13:05:01
简单的使用JS将google翻译实现简单的企业多语言外贸网站多语言翻译功能。

将以下为代保存到一个 JS 文件中,并放在要使用的项目中并调用。

browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
condition = !(((browserName.indexOf("Explorer") >= 0) && (browserVer < 4)) || ((browserName.indexOf("Netscape") >= 0) && (browserVer < 2)));
if (condition == true) {
	CanAnimate = true;
} else {
	CanAnimate = false;
}

function translator(pattern) {
	var open_in_same_window = 0;
	var my_location = unescape(document.location.toString());
	var new_location = "";
	var new_pattern = "";
	if (my_location.indexOf("translate_c?") != -1) {
		var indexof_u = my_location.indexOf("u=");
		if (indexof_u == -1) {
			new_location = document.location;
		} else {
			var subs = my_location.substring(indexof_u, my_location.length);
			var ss = subs.split("&");
			new_location = ss[0].substring(2, ss[0].length);
		}
	} else {
		new_location = document.location;
	}
	indexof_p = pattern.indexOf("|");
	var isen = "";
	if (indexof_p == -1) {
		indexof_p1 = pattern.indexOf("><");
		if (indexof_p1 == -1) {
			new_pattern = pattern;
			if (pattern == "en") {
				isen = 1;
			}
		} else {
			var psplit = pattern.split("><");
			new_pattern = psplit[0] + "|" + psplit[1];
			if (psplit[1] == "en") {
				isen = 1;
			}
		}
	} else {
		var psplit = pattern.split("|");
		new_pattern = psplit[0] + "|" + psplit[1];
		if (psplit[1] == "en") {
			isen = 1;
		}
	}
	var thisurl = "";
	if (isen == 1) {
		thisurl = new_location;
	} else {
		thisurl = "http://translate.google.com/translate_c?langpair=" + new_pattern + "&u=" + new_location;
	}
	if (open_in_same_window == 1) {
		window.location.href = thisurl;
	} else {
		if (CanAnimate) {
			msgWindow = window.open("", "subwindow", "toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,menubar=yes,resizable=yes,left=0,top=0");
			msgWindow.focus();
			msgWindow.location.href = thisurl;
		} else {
			msgWindow = window.open(thisurl, "subwindow", "toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,menubar=yes,resizable=yes,left=0,top=0");
		}
	}
}

function translator1(pattern) {
	var thisurl = "http://translate.google.com/translate_c?langpair=" + pattern + "&u=" + document.location;
	if (CanAnimate) {
		msgWindow = window.open("", "subwindow", "toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,menubar=yes,resizable=yes,left=0,top=0");
		msgWindow.focus();
		msgWindow.location.href = thisurl;
	} else {
		msgWindow = window.open(thisurl, "subwindow", "toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,menubar=yes,resizable=yes,left=0,top=0");
	}
}

function showsubmenu(sid) {
	whichEl = eval("submenu" + sid);
	if (whichEl.style.display == "none") {
		eval("submenu" + sid + ".style.display=\"\";");
	} else {
		eval("submenu" + sid + ".style.display=\"none\";");
	}
}

调用方法,以下为三个例子:

<a href="javascript:translator('cn|en')">china</a> //中文翻译成英语
<a href="javascript:translator('cn|de')">German</a> //中文翻译成德语
<a href="javascript:translator('cn|es')">Spanish</a> //中文翻译成西班牙语

当然该方法为简单的翻译,如果达到高效翻译,还需使用其它方法。

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68984.html