Skip to content

Commit

Permalink
released translate.js v1.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 11, 2017
1 parent b5cb9c7 commit 64aab9c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 35 deletions.
91 changes: 60 additions & 31 deletions dist/translater.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*!
* translater.js v1.0.7
* translater.js v1.0.9
* Simple translation tools.
*
* Copyright (c) 2016 kenny wang
* Copyright (c) 2017 kenny wang
* https://github.com/jaywcjlove/translater.js
*
* Licensed under the MIT license.
Expand Down Expand Up @@ -47,19 +47,23 @@
lang && lang !== "default" && this.setLang(lang);
};
Translater.prototype = {
setLang: function(name) {
var langs = this.langs, method = "";
setLang: function(name, elms) {
var langs = elms || this.langs, method = "";
this.lang_name = name;
for (var i = 0; i < langs.length; i++) {
if (langs[i]["lang-" + name]) {
if (langs[i]["lang-" + name] || langs[i][name]) {
if (langs[i].element.tagName === "TITLE") {
method = "innerHTML";
} else if (langs[i].element.tagName === "IMG") {
method = "src";
method = langs[i]["type"];
} else if (langs[i].element.tagName === "INPUT") {
method = langs[i]["type"];
} else {
method = "nodeValue";
}
langs[i].element[method] = langs[i]["lang-" + name];
langs[i].element[method] = langs[i]["lang-" + name] || langs[i][name];
} else {
this.setLang(name, langs[i]);
}
}
setCookie("t-lang", name, 24);
Expand Down Expand Up @@ -99,14 +103,14 @@
// var str = document.getElementById("box").innerHTML;
// var str1 = str.replace(/<.*>(.*)<.*>/i,"$1");
// var str2 = str.replace(/^.*<!--(.*)-->.*$/,"$1");
var elems = Array.prototype.concat.apply(getTextNodes(document), getImgNodes(document));
var elems = Array.prototype.concat(getTextNodes(document), getNodes(document, "IMG"), getNodes(document, "INPUT"));
var emptyArray = [];
var translateData = new Object();
for (var i = 0; i < elems.length; i++) {
elems[i].nodeValue = trim(elems[i].nodeValue);
if (elems[i].nodeValue !== "") {
translateData = translater(elems[i]);
if (Object.getOwnPropertyNames(translateData).length > 2) emptyArray.push(translateData);
translateData = translater(elems[i]);
var mTran = Object.getOwnPropertyNames(translateData);
if (mTran.length >= 2 && mTran[0] == "0" || mTran.length > 2) {
emptyArray.push(translateData);
}
}
return emptyArray;
Expand All @@ -129,18 +133,45 @@
}
// 处理 IMG
function serializeIMG(elm) {
var i = 0, data = {}, htmlstr = elm.outerHTML, imgurl = htmlstr.match(/src=\"(.*?)\"/);
data.element = elm;
data["lang-default"] = imgurl.length === 2 ? imgurl[1] : "";
imgurl = htmlstr.match(/data-lang-.(\w+).\".*?\"/g);
if (imgurl && imgurl.length > 0) {
for (;i < imgurl.length; i++) {
var name = imgurl[i].match(/data-lang-(.*?)=/, "$1")[1];
var value = imgurl[i].match(/data-lang-(.*?)=\"(.*?)\"/, "$1")[2];
data["lang-" + name] = value;
var i = 0, trans = [];
var htmlstr = elm.outerHTML;
var imgurl = htmlstr.match(/src=\"(.*?)\"/);
var alt = htmlstr.match(/alt=\"(.*?)\"/);
var title = htmlstr.match(/title=\"(.*?)\"/);
var placeholder = htmlstr.match(/placeholder=\"(.*?)\"/);
var value = htmlstr.match(/value=\"(.*?)\"/);
var processing = function(proce, _type, _mark) {
var data = {};
var regm = new RegExp(_mark + '.(\\w+).\\".*?\\"', "g");
var regname = new RegExp(_mark + "(.*?)=");
var regval = new RegExp(_mark + '(.*?)=\\"(.*?)\\"');
data.element = elm;
data["default"] = proce.length === 2 ? proce[1] : "";
proce = htmlstr.match(regm);
if (proce && proce.length > 0) {
for (i = 0; i < proce.length; i++) {
data[proce[i].match(regname, "$1")[1]] = proce[i].match(regval, "$1")[2];
data["type"] = _type;
}
}
return data;
};
if (imgurl) {
trans.push(processing(imgurl, "src", "data-lang-"));
}
return data;
if (alt) {
trans.push(processing(alt, "alt", "alt-"));
}
if (title) {
trans.push(processing(title, "title", "title-"));
}
if (placeholder) {
trans.push(processing(placeholder, "placeholder", "placeholder-"));
}
if (value) {
trans.push(processing(value, "value", "value-"));
}
return trans;
}
// 序列化翻译数据
function translater(elm, langData) {
Expand All @@ -151,6 +182,9 @@
} else if (elm.tagName === "IMG" && elm.nodeType === 1) {
// 处理 IMG
return serializeIMG(elm);
} else if (elm.tagName === "INPUT" && elm.nodeType === 1) {
// 处理 INPUT
return serializeIMG(elm);
}
var name = "lang-default", value = elm.nodeValue, fragmentRE = /^\{\w+\}/;
if (elm.nodeType === 8 && fragmentRE.test(value)) {
Expand Down Expand Up @@ -189,16 +223,11 @@
}
return name ? params[name] : params;
}
var getImgNodes = function(e) {
var i = 0, result = [], imgs = e.getElementsByTagName("IMG");
for (;i < imgs.length; i++) {
var img_match = imgs[i].outerHTML.match(/data-lang-.(\w+).\".*?\"/g);
if (imgs[i] && imgs[i].nodeType === 1 && img_match && img_match.length > 0) {
result.push(imgs[i]);
}
}
function getNodes(e, _tagName) {
var i = 0, result = [], doms = e.getElementsByTagName(_tagName);
for (;i < doms.length; i++) result.push(doms[i]);
return result;
};
}
//兼容的获取文本节点的简单方案
var getTextNodes = window.NodeFilter ? function(e) {
//支持TreeWalker的浏览器
Expand Down
4 changes: 2 additions & 2 deletions dist/translater.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "translater.js",
"version": "1.0.8",
"version": "1.0.9",
"description": "Simple translation tools.",
"main": "dist/translater.min.js",
"directories": {
Expand All @@ -11,7 +11,7 @@
"build:min": "umd Translater src/translater.js | uglifyjs -mc | bannerjs -o > dist/translater.min.js",
"build:dist": "umd Translater src/translater.js | bannerjs -m | uglifyjs -b beautify=true --comments 'all' > dist/translater.js ",
"build": "npm run build:min && npm run build:dist",
"watch": "onchange 'src/store.js' -v -- npm run build"
"watch": "onchange 'src/translater.js' -v -- npm run build"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 64aab9c

Please sign in to comment.