jquery-lang.min.js 6.5 KB

1
  1. (function(a){if(typeof define==="function"&&define.amd){define(["jquery","Cookies"],a)}else{if(typeof exports==="object"){module.exports=a()}else{var c=window.Lang;var b=window.Lang=a(jQuery,typeof Cookies!=="undefined"?Cookies:undefined);b.noConflict=function(){window.Lang=c;return b}}}}(function(c,a){var b=function(){this._fireEvents=true;this._dynamic={}};b.prototype.init=function(f){var d=this,h,e,i,g;f=f||{};f.cookie=f.cookie||{};e=f.defaultLang;i=f.currentLang;g=f.allowCookieOverride;this.cookieName=f.cookie.name||"langCookie";this.cookieExpiry=f.cookie.expiry||365;this.cookiePath=f.cookie.path||"/";this._mutationCopies={append:c.fn.append,appendTo:c.fn.appendTo,prepend:c.fn.prepend,before:c.fn.before,after:c.fn.after,html:c.fn.html};c.fn.append=function(){return d._mutation(this,"append",arguments)};c.fn.appendTo=function(){return d._mutation(this,"appendTo",arguments)};c.fn.prepend=function(){return d._mutation(this,"prepend",arguments)};c.fn.before=function(){return d._mutation(this,"before",arguments)};c.fn.after=function(){return d._mutation(this,"after",arguments)};c.fn.html=function(){return d._mutation(this,"html",arguments)};this.defaultLang=e||"en";this.currentLang=e||"en";if((g||!i)&&typeof a!=="undefined"){h=a.get(this.cookieName);if(h){i=h}}c(function(){d._start();if(i&&i!==d.defaultLang){d.change(i)}})};b.prototype.pack={};b.prototype.attrList=["title","alt","placeholder","href"];b.prototype.dynamic=function(e,d){if(e!==undefined&&d!==undefined){this._dynamic[e]=d}};b.prototype.loadPack=function(e,f){var d=this;if(e&&d._dynamic[e]){c.ajax({dataType:"json",url:d._dynamic[e],success:function(k){d.pack[e]=k;if(d.pack[e].regex){var j=d.pack[e].regex,h,g;for(g=0;g<j.length;g++){h=j[g];if(h.length===2){h[0]=new RegExp(h[0])}else{if(h.length===3){h[0]=new RegExp(h[0],h[1]);h.splice(1,1)}}}}if(f){f(false,e,d._dynamic[e])}},error:function(){if(f){f(true,e,d._dynamic[e])}throw ("Organizr Translation Function: Error loading language pack "+d._dynamic[e])}})}else{throw ("Cannot load language pack, no file path specified!")}};b.prototype._start=function(e){var d=e!==undefined?c(e).find("[lang]"):c(":not(html)[lang]"),g=d.length,f;while(g--){f=c(d[g]);this._processElement(f)}};b.prototype._processElement=function(d){if(d.attr("lang")===this.defaultLang){this._storeAttribs(d);this._storeContent(d)}};b.prototype._storeAttribs=function(g){var f,d,e;for(f=0;f<this.attrList.length;f++){d=this.attrList[f];if(g.attr(d)){e=g.data("lang-attr")||{};e[d]=g.attr(d);g.data("lang-attr",e)}}};b.prototype._storeContent=function(e){if(e.is("input")){switch(e.attr("type")){case"button":case"submit":case"hidden":case"reset":e.data("lang-val",e.val());break}}else{if(e.is("img")){e.data("lang-src",e.attr("src"))}else{var d=this._getTextNodes(e);if(d){e.data("lang-text",d)}}}};b.prototype._getTextNodes=function(g){var d=g.contents(),e=[],h={},j,f=this,i=Array.prototype.map;c.each(d,function(k,l){if(l.nodeType!==3){return}h={node:l,langDefaultText:l.data};e.push(h)});if(d.length==1){e[0].langToken=g.data("langToken")}return e};b.prototype._setTextNodes=function(h,f,g){var j,i,l,d,m,n=g!==this.defaultLang;for(j=0;j<f.length;j++){i=f[j];if(n){l=i.langToken||c.trim(i.langDefaultText);if(l){d=this.translate(l,g);m=/<[^>]+>/g;if(m.test(d)){h.context.innerHTML=d}else{if(d){try{i.node.data=i.node.data.split(c.trim(i.node.data)).join(d)}catch(k){}}else{if(console&&console.log){console.warn('Organizr Function: Translation for "'+l+'" not found!')}}}}}else{try{i.node.data=i.langDefaultText}catch(k){}}}};b.prototype._translateAttribs=function(f,g){var d,e=f.data("lang-attr")||{},h;for(d in e){if(e.hasOwnProperty(d)){if(f.attr(d)){if(g!==this.defaultLang){h=this.translate(e[d],g);if(h){f.attr(d,h)}}else{f.attr(d,e[d])}}}}};b.prototype._translateContent=function(f,g){var d=g!==this.defaultLang,h,e;if(f.is("input")){switch(f.attr("type")){case"button":case"submit":case"hidden":case"reset":if(d){h=this.translate(f.data("lang-val"),g);if(h){f.val(h)}}else{f.val(f.data("lang-val"))}break}}else{if(f.is("img")){if(d){h=this.translate(f.data("lang-src"),g);if(h){f.attr("src",h)}}else{f.attr("src",f.data("lang-src"))}}else{e=f.data("lang-text");if(e){this._setTextNodes(f,e,g)}}}};b.prototype.change=function(d,f,k){var l=this;if(d===this.defaultLang||this.pack[d]||this._dynamic[d]){if(d!==this.defaultLang){if(!this.pack[d]&&this._dynamic[d]){this.loadPack(d,function(n,m,o){if(!n){l.change.call(l,d,f,k)}else{if(k){k("Language pack could not load from: "+o,d,f)}}});return}else{if(!this.pack[d]&&!this._dynamic[d]){if(k){k("Language pack not defined for: "+d,d,f)}throw ("Could not change language to "+d+" because no language pack for this language exists!")}}}var h=false,g=this.currentLang;if(this.currentLang!=d){this.beforeUpdate(g,d);h=true}this.currentLang=d;var i=f!==undefined?c(f).find("[lang]"):c(":not(html)[lang]"),j=i.length,e;while(j--){e=c(i[j]);if(e.attr("lang")!==d){this._translateElement(e,d)}}if(h){this.afterUpdate(g,d)}if(typeof a!=="undefined"){a.set(l.cookieName,d,{expires:l.cookieExpiry,path:l.cookiePath})}if(k){k(false,d,f)}}else{if(k){k("No language pack defined for: "+d,d,f)}throw ('Attempt to change language to "'+d+'" but no language pack for that language is loaded!')}};b.prototype._translateElement=function(d,e){this._translateAttribs(d,e);if(d.attr("data-lang-content")!="false"){this._translateContent(d,e)}d.attr("lang",e)};b.prototype.translate=function(e,d){d=d||this.currentLang;if(this.pack[d]){var f="";if(d!=this.defaultLang){f=this.pack[d].token[e];if(!f){f=this._regexMatch(e,d)}if(!f){if(console&&console.log){langStrings.token[e]=e}}return f||e}else{return e}}else{return e}};b.prototype._regexMatch=function(j,i){var e,k,h,g,f,d;e=this.pack[i].regex;if(e){k=e.length;for(h=0;h<k;h++){g=e[h];f=g[0];d=f.exec(j);if(d&&d[0]){return j.split(d[0]).join(g[1])}}}return""};b.prototype.beforeUpdate=function(e,d){if(this._fireEvents){c(this).triggerHandler("beforeUpdate",[e,d,this.pack[e],this.pack[d]])}};b.prototype.afterUpdate=function(e,d){if(this._fireEvents){c(this).triggerHandler("afterUpdate",[e,d,this.pack[e],this.pack[d]])}};b.prototype.refresh=function(){this._fireEvents=false;this.change(this.currentLang);this._fireEvents=true};b.prototype._mutation=function(h,i,g){var d=this._mutationCopies[i].apply(h,g),f=this.currentLang,e=c(h);if(e.attr("lang")){this._fireEvents=false;this._translateElement(e,this.defaultLang);this.change(this.defaultLang,e);this.currentLang=f;this._processElement(e);this._translateElement(e,this.currentLang)}this._start(e);this.change(this.currentLang,e);this._fireEvents=true;return d};return b}));