From f0b4392e1d59245c745ce93349c4c4680cae4093 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Thu, 24 Mar 2016 18:16:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8E=8B=E7=BC=A9=E5=92=8C?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E7=89=88...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/translater.js | 114 ++++++++++++++++++++++++++++++++++++++++ dist/translater.min.js | 115 +---------------------------------------- gulpfile.js | 3 +- 3 files changed, 117 insertions(+), 115 deletions(-) create mode 100644 dist/translater.js diff --git a/dist/translater.js b/dist/translater.js new file mode 100644 index 0000000..699764a --- /dev/null +++ b/dist/translater.js @@ -0,0 +1,114 @@ +;(function(root, factory) { + if (typeof define === 'function' && define.amd) { + define([], factory); + } else if (typeof exports === 'object') { + module.exports = factory(); + } else { + root.Translater = factory(); + } +}(this, function() { +var Translater = function(option,callback){ + // 默认给URL参数 ?lang=en + this.lang_name = (option && option.lang) || 'default'; + // 回调函数 + this.callback = callback || function(){}; + this.langs = getElems() || [] +} + +Translater.prototype = { + setLang:function(name){ + var langs = this.langs; + this.lang_name = name; + for (var i = 0; i < langs.length; i++) { + if(langs[i]['lang-'+name]) + langs[i].element.nodeValue = langs[i]['lang-'+name]; + } + } +} + +// 获取所有节点里面的注释信息 +// 返回一个数组 +function getElems(){ + // var str = document.getElementById("box").innerHTML; + // var str1 = str.replace(/<.*>(.*)<.*>/i,"$1"); + // var str2 = str.replace(/^.*.*$/,"$1"); + var elems = getTextNodes(document); + var emptyArray = []; + var translateData = new Object() + // console.log("elems::",elems); + 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 ); + }; + } + return emptyArray; +} + +// 序列化翻译数据 +function translater(elm,langData){ + langData = langData||{}; + + var name = 'lang-default',value=elm.nodeValue, + fragmentRE = /^\{\w+\}/; + + if(elm.nodeType === 8 && fragmentRE.test(value)){ + // 获取花括号内容 + name = value.match(fragmentRE)[0]; + // 去掉花括号 + name = 'lang-' + (name?name.replace(/\{([^\ ]*)\}/g, "$1"):''); + // 获取好括号后面的内容 + value = value.replace(fragmentRE,"") + if(trim(value) !== '') langData[name] = value; + } + + if(trim(value) !== '' && !langData['lang-default']){ + langData[name] = value; + langData.element = elm; + } + + var nextElm = elm.nextSibling; + if(nextElm&&nextElm.nodeType !== 1){ + translater(nextElm,langData) + }; + return langData; +} + +//过滤左右的空格以及换行符 +function trim(text) { + return "" + (null == text ? "" : (text + "").replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "").replace(/[\r\n]+/g,"")); +} + +//兼容的获取文本节点的简单方案 +var getTextNodes = window.NodeFilter?function(e){ + //支持TreeWalker的浏览器 + var r=[],o,s; + s=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null,null); + while(o=s.nextNode()){ + if(o.parentElement.tagName !== 'SCRIPT' + && o.parentElement.tagName !== 'STYLE') { + r.push(o); //遍历迭代器 + } + } + return r; +}:function(e){ + //不支持的需要遍历 + switch(e.nodeType){ + //注释节点直接返回 + case 3:return [e]; + case 1:;case 9: + //文档或元素需要遍历子节点 + var i,s=e.childNodes,result=[]; + if(e.tagName!== 'SCRIPT' && e.tagName!== 'STYLE'){ + for(i=0;i(.*)<.*>/i,"$1"); - // var str2 = str.replace(/^.*.*$/,"$1"); - var elems = getTextNodes(document); - var emptyArray = []; - var translateData = new Object() - // console.log("elems::",elems); - 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 ); - }; - } - return emptyArray; -} - -// 序列化翻译数据 -function translater(elm,langData){ - langData = langData||{}; - - var name = 'lang-default',value=elm.nodeValue, - fragmentRE = /^\{\w+\}/; - - if(elm.nodeType === 8 && fragmentRE.test(value)){ - // 获取花括号内容 - name = value.match(fragmentRE)[0]; - // 去掉花括号 - name = 'lang-' + (name?name.replace(/\{([^\ ]*)\}/g, "$1"):''); - // 获取好括号后面的内容 - value = value.replace(fragmentRE,"") - if(trim(value) !== '') langData[name] = value; - } - - if(trim(value) !== '' && !langData['lang-default']){ - langData[name] = value; - langData.element = elm; - } - - var nextElm = elm.nextSibling; - if(nextElm&&nextElm.nodeType !== 1){ - translater(nextElm,langData) - }; - return langData; -} - -//过滤左右的空格以及换行符 -function trim(text) { - return "" + (null == text ? "" : (text + "").replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "").replace(/[\r\n]+/g,"")); -} - -//兼容的获取文本节点的简单方案 -var getTextNodes = window.NodeFilter?function(e){ - //支持TreeWalker的浏览器 - var r=[],o,s; - s=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null,null); - while(o=s.nextNode()){ - if(o.parentElement.tagName !== 'SCRIPT' - && o.parentElement.tagName !== 'STYLE') { - r.push(o); //遍历迭代器 - } - } - return r; -}:function(e){ - //不支持的需要遍历 - switch(e.nodeType){ - //注释节点直接返回 - case 3:return [e]; - case 1:;case 9: - //文档或元素需要遍历子节点 - var i,s=e.childNodes,result=[]; - if(e.tagName!== 'SCRIPT' && e.tagName!== 'STYLE'){ - for(i=0;i2&&a.push(l));return a}function n(e,a){a=a||{};var r="lang-default",l=e.nodeValue,o=/^\{\w+\}/;8===e.nodeType&&o.test(l)&&(r=l.match(o)[0],r="lang-"+(r?r.replace(/\{([^\ ]*)\}/g,"$1"):""),l=l.replace(o,""),""!==t(l)&&(a[r]=l)),""===t(l)||a["lang-default"]||(a[r]=l,a.element=e);var u=e.nextSibling;return u&&1!==u.nodeType&&n(u,a),a}function t(e){return""+(null==e?"":(e+"").replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"").replace(/[\r\n]+/g,""))}var a=function(n,t){this.lang_name=n&&n.lang||"default",this.callback=t||function(){},this.langs=e()||[]};a.prototype={setLang:function(e){var n=this.langs;this.lang_name=e;for(var t=0;t