-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.min.js
9 lines (9 loc) · 1.7 KB
/
lang.min.js
1
2
3
4
5
6
7
8
9
/*
* =====================================================
* lang.js v1.0.2
* 简易版国际化方案
* 自动适配语言
* 推荐与vue.js一起使用
* =====================================================
*/
(function(a){a.init=function(){if(localStorage.lang&&localStorage.langfix){a.langName=localStorage.lang}else{a.langName=(navigator.language||navigator.browserLanguage).toLowerCase();localStorage.lang=a.langName}if(b.hasOwnProperty(a.langName)){localStorage.langNum=b[a.langName]}else{localStorage.langNum=2;localStorage.lang="en"}a.langNum=Number(localStorage.langNum)};a.setLang=function(c){localStorage.lang=c;if(b.hasOwnProperty(c)){localStorage.langNum=b[c]}else{localStorage.langNum=2}localStorage.langfix=true};a.fitOption=function(d){d||(d="lang-option");var c=document.getElementsByName("lang-option");if(!c){return}console.log(c[1].checked="true");c.length>0&&(c[a.langNum].selected=true)};a.show=function(){if(Number(localStorage.langNum)!=0){var c=document.querySelectorAll("[txt]");for(var g=0;g<c.length;g++){var f=c[g].getAttribute("txt").split("||");var e=f[Number(localStorage.langNum)-1];if(c[g].childNodes.length<=1){c[g].innerText=e}else{for(var h=0;h<c[g].childNodes.length;h++){if(c[g].childNodes[h].nodeType==3&&/[^\s]+/.test(c[g].childNodes[h].nodeValue)){c[g].childNodes[h].nodeValue=e;break}}}}var d=document.querySelectorAll("[plh]");for(var g=0;g<d.length;g++){var f=d[g].getAttribute("plh").split("||");var e=f[Number(localStorage.langNum)-1];d[g].placeholder=e}a.fitOption()}};a.fit=function(){return arguments[Number(localStorage.langNum)]};var b={zh:0,"zh-cn":0,"zh-hk":1,"zh-tw":1};a.langNum=0;if(localStorage.langNum){a.langNum=Number(localStorage.langNum)}else{a.init()}})(window.lang={});