-
Notifications
You must be signed in to change notification settings - Fork 394
/
Gettext.min.js
1 lines (1 loc) · 10 KB
/
Gettext.min.js
1
Gettext=(function(a){this.domain="messages";this.locale_data=undefined;var c=["domain","locale_data"];if(this.isValidObject(a)){for(var b in a){for(var d=0;d<c.length;d++){if(b==c[d]){if(this.isValidObject(a[b])){this[b]=a[b]}}}}}this.try_load_lang();return this});Gettext.context_glue="\x04";Gettext._locale_data={};Gettext.prototype.try_load_lang=(function(){if(typeof(this.locale_data)!="undefined"){var d=this.locale_data;this.locale_data=undefined;this.parse_locale_data(d);if(typeof(Gettext._locale_data[this.domain])=="undefined"){throw new Error("Error: Gettext 'locale_data' does not contain the domain '"+this.domain+"'")}}var a=this.get_lang_refs();if(typeof(a)=="object"&&a.length>0){for(var c=0;c<a.length;c++){var b=a[c];if(b.type=="application/json"){if(!this.try_load_lang_json(b.href)){throw new Error("Error: Gettext 'try_load_lang_json' failed. Unable to exec xmlhttprequest for link ["+b.href+"]")}}else{if(b.type=="application/x-po"){if(!this.try_load_lang_po(b.href)){throw new Error("Error: Gettext 'try_load_lang_po' failed. Unable to exec xmlhttprequest for link ["+b.href+"]")}}else{throw new Error("TODO: link type ["+b.type+"] found, and support is planned, but not implemented at this time.")}}}}});Gettext.prototype.parse_locale_data=(function(e){if(typeof(Gettext._locale_data)=="undefined"){Gettext._locale_data={}}for(var d in e){if((!e.hasOwnProperty(d))||(!this.isValidObject(e[d]))){continue}var b=false;for(var m in e[d]){b=true;break}if(!b){continue}var f=e[d];if(d==""){d="messages"}if(!this.isValidObject(Gettext._locale_data[d])){Gettext._locale_data[d]={}}if(!this.isValidObject(Gettext._locale_data[d].head)){Gettext._locale_data[d].head={}}if(!this.isValidObject(Gettext._locale_data[d].msgs)){Gettext._locale_data[d].msgs={}}for(var i in f){if(i==""){var a=f[i];for(var c in a){var n=c.toLowerCase();Gettext._locale_data[d].head[n]=a[c]}}else{Gettext._locale_data[d].msgs[i]=f[i]}}}for(var d in Gettext._locale_data){if(this.isValidObject(Gettext._locale_data[d].head["plural-forms"])&&typeof(Gettext._locale_data[d].head.plural_func)=="undefined"){var l=Gettext._locale_data[d].head["plural-forms"];var k=new RegExp("^(\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;a-zA-Z0-9_()])+)","m");if(k.test(l)){var g=Gettext._locale_data[d].head["plural-forms"];if(!/;\s*$/.test(g)){g=g.concat(";")}var j="var plural; var nplurals; "+g+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) };';Gettext._locale_data[d].head.plural_func=new Function("n",j)}else{throw new Error("Syntax error in language file. Plural-Forms header is invalid ["+l+"]")}}else{if(typeof(Gettext._locale_data[d].head.plural_func)=="undefined"){Gettext._locale_data[d].head.plural_func=(function(h){var o=(h!=1)?1:0;return{nplural:2,plural:o}})}}}return});Gettext.prototype.try_load_lang_po=(function(c){var e=this.sjax(c);if(!e){return}var a=this.uri_basename(c);var d=this.parse_po(e);var b={};if(d){if(!d[""]){d[""]={}}if(!d[""].domain){d[""].domain=a}a=d[""].domain;b[a]=d;this.parse_locale_data(b)}return 1});Gettext.prototype.uri_basename=(function(c){var b;if(b=c.match(/^(.*\/)?(.*)/)){var a;if(a=b[2].match(/^(.*)\..+$/)){return a[1]}else{return b[2]}}else{return""}});Gettext.prototype.parse_po=(function(s){var c={};var n={};var a="";var m=[];var o=s.split("\n");for(var g=0;g<o.length;g++){o[g]=o[g].replace(/(\n|\r)+$/,"");var l;if(/^$/.test(o[g])){if(typeof(n.msgid)!="undefined"){var j=(typeof(n.msgctxt)!="undefined"&&n.msgctxt.length)?n.msgctxt+Gettext.context_glue+n.msgid:n.msgid;var d=(typeof(n.msgid_plural)!="undefined"&&n.msgid_plural.length)?n.msgid_plural:null;var r=[];for(var f in n){var l;if(l=f.match(/^msgstr_(\d+)/)){r[parseInt(l[1])]=n[f]}}r.unshift(d);if(r.length>1){c[j]=r}n={};a=""}}else{if(/^#/.test(o[g])){continue}else{if(l=o[g].match(/^msgctxt\s+(.*)/)){a="msgctxt";n[a]=this.parse_po_dequote(l[1])}else{if(l=o[g].match(/^msgid\s+(.*)/)){a="msgid";n[a]=this.parse_po_dequote(l[1])}else{if(l=o[g].match(/^msgid_plural\s+(.*)/)){a="msgid_plural";n[a]=this.parse_po_dequote(l[1])}else{if(l=o[g].match(/^msgstr\s+(.*)/)){a="msgstr_0";n[a]=this.parse_po_dequote(l[1])}else{if(l=o[g].match(/^msgstr\[0\]\s+(.*)/)){a="msgstr_0";n[a]=this.parse_po_dequote(l[1])}else{if(l=o[g].match(/^msgstr\[(\d+)\]\s+(.*)/)){a="msgstr_"+l[1];n[a]=this.parse_po_dequote(l[2])}else{if(/^"/.test(o[g])){n[a]+=this.parse_po_dequote(o[g])}else{m.push("Strange line ["+g+"] : "+o[g])}}}}}}}}}}if(typeof(n.msgid)!="undefined"){var j=(typeof(n.msgctxt)!="undefined"&&n.msgctxt.length)?n.msgctxt+Gettext.context_glue+n.msgid:n.msgid;var d=(typeof(n.msgid_plural)!="undefined"&&n.msgid_plural.length)?n.msgid_plural:null;var r=[];for(var f in n){var l;if(l=f.match(/^msgstr_(\d+)/)){r[parseInt(l[1])]=n[f]}}r.unshift(d);if(r.length>1){c[j]=r}n={};a=""}if(c[""]&&c[""][1]){var b={};var k=c[""][1].split(/\\n/);for(var g=0;g<k.length;g++){if(!k.length){continue}var q=k[g].indexOf(":",0);if(q!=-1){var p=k[g].substring(0,q);var h=k[g].substring(q+1);var e=p.toLowerCase();if(b[e]&&b[e].length){m.push("SKIPPING DUPLICATE HEADER LINE: "+k[g])}else{if(/#-#-#-#-#/.test(e)){m.push("SKIPPING ERROR MARKER IN HEADER: "+k[g])}else{h=h.replace(/^\s+/,"");b[e]=h}}}else{m.push("PROBLEM LINE IN HEADER: "+k[g]);b[k[g]]=""}}c[""]=b}else{c[""]={}}return c});Gettext.prototype.parse_po_dequote=(function(b){var a;if(a=b.match(/^"(.*)"/)){b=a[1]}b=b.replace(/\\"/,"");return b});Gettext.prototype.try_load_lang_json=(function(b){var c=this.sjax(b);if(!c){return}var a=this.JSON(c);this.parse_locale_data(a);return 1});Gettext.prototype.get_lang_refs=(function(){var a=new Array();var c=document.getElementsByTagName("link");for(var b=0;b<c.length;b++){if(c[b].rel=="gettext"&&c[b].href){if(typeof(c[b].type)=="undefined"||c[b].type==""){if(/\.json$/i.test(c[b].href)){c[b].type="application/json"}else{if(/\.js$/i.test(c[b].href)){c[b].type="application/json"}else{if(/\.po$/i.test(c[b].href)){c[b].type="application/x-po"}else{if(/\.mo$/i.test(c[b].href)){c[b].type="application/x-mo"}else{throw new Error("LINK tag with rel=gettext found, but the type and extension are unrecognized.")}}}}}c[b].type=c[b].type.toLowerCase();if(c[b].type=="application/json"){c[b].type="application/json"}else{if(c[b].type=="text/javascript"){c[b].type="application/json"}else{if(c[b].type=="application/x-po"){c[b].type="application/x-po"}else{if(c[b].type=="application/x-mo"){c[b].type="application/x-mo"}else{throw new Error("LINK tag with rel=gettext found, but the type attribute ["+c[b].type+"] is unrecognized.")}}}}a.push(c[b])}}return a});Gettext.prototype.textdomain=(function(a){if(a&&a.length){this.domain=a}return this.domain});Gettext.prototype.gettext=(function(e){var c;var a;var b;var d;return this.dcnpgettext(null,c,e,a,b,d)});Gettext.prototype.dgettext=(function(d,f){var c;var a;var b;var e;return this.dcnpgettext(d,c,f,a,b,e)});Gettext.prototype.dcgettext=(function(d,f,e){var c;var a;var b;return this.dcnpgettext(d,c,f,a,b,e)});Gettext.prototype.ngettext=(function(e,a,b){var c;var d;return this.dcnpgettext(null,c,e,a,b,d)});Gettext.prototype.dngettext=(function(d,f,a,b){var c;var e;return this.dcnpgettext(d,c,f,a,b,e)});Gettext.prototype.dcngettext=(function(d,f,a,b,e){var c;return this.dcnpgettext(d,c,f,a,b,e,e)});Gettext.prototype.pgettext=(function(c,e){var a;var b;var d;return this.dcnpgettext(null,c,e,a,b,d)});Gettext.prototype.dpgettext=(function(d,c,f){var a;var b;var e;return this.dcnpgettext(d,c,f,a,b,e)});Gettext.prototype.dcpgettext=(function(d,c,f,e){var a;var b;return this.dcnpgettext(d,c,f,a,b,e)});Gettext.prototype.npgettext=(function(c,e,a,b){var d;return this.dcnpgettext(null,c,e,a,b,d)});Gettext.prototype.dnpgettext=(function(d,c,f,a,b){var e;return this.dcnpgettext(d,c,f,a,b,e)});Gettext.prototype.dcnpgettext=(function(y,v,s,a,l,m){if(!this.isValidObject(s)){return""}var d=this.isValidObject(a);var k=this.isValidObject(v)?v+Gettext.context_glue+s:s;var t=this.isValidObject(y)?y:this.isValidObject(this.domain)?this.domain:"messages";var q="LC_MESSAGES";var m=5;var c=new Array();if(typeof(Gettext._locale_data)!="undefined"&&this.isValidObject(Gettext._locale_data[t])){c.push(Gettext._locale_data[t])}else{if(typeof(Gettext._locale_data)!="undefined"){for(var h in Gettext._locale_data){c.push(Gettext._locale_data[h])}}}var x=[];var e=false;var w;if(c.length){for(var f=0;f<c.length;f++){var u=c[f];if(this.isValidObject(u.msgs[k])){for(var g=0;g<u.msgs[k].length;g++){x[g]=u.msgs[k][g]}x.shift();w=u;e=true;if(x.length>0&&x[0].length!=0){break}}}}if(x.length==0||x[0].length==0){x=[s,a]}var o=x[0];if(d){var b;if(e&&this.isValidObject(w.head.plural_func)){var r=w.head.plural_func(l);if(!r.plural){r.plural=0}if(!r.nplural){r.nplural=0}if(r.nplural<=r.plural){r.plural=0}b=r.plural}else{b=(l!=1)?1:0}if(this.isValidObject(x[b])){o=x[b]}}return o});Gettext.strargs=(function(c,b){if(null==b||"undefined"==typeof(b)){b=[]}else{if(b.constructor!=Array){b=[b]}}var g="";while(true){var d=c.indexOf("%");var a;if(d==-1){g+=c;break}g+=c.substr(0,d);if(c.substr(d,2)=="%%"){g+="%";c=c.substr((d+2))}else{if(a=c.substr(d).match(/^%(\d+)/)){var e=parseInt(a[1]);var f=a[1].length;if(e>0&&b[e-1]!=null&&typeof(b[e-1])!="undefined"){g+=b[e-1]}c=c.substr((d+1+f))}else{g+="%";c=c.substr((d+1))}}}return g});Gettext.prototype.strargs=(function(b,a){return Gettext.strargs(b,a)});Gettext.prototype.isArray=(function(a){return this.isValidObject(a)&&a.constructor==Array});Gettext.prototype.isValidObject=(function(a){if(null==a){return false}else{if("undefined"==typeof(a)){return false}else{return true}}});Gettext.prototype.sjax=(function(d){var c;if(window.XMLHttpRequest){c=new XMLHttpRequest()}else{if(navigator.userAgent.toLowerCase().indexOf("msie 5")!=-1){c=new ActiveXObject("Microsoft.XMLHTTP")}else{c=new ActiveXObject("Msxml2.XMLHTTP")}}if(!c){throw new Error("Your browser doesn't do Ajax. Unable to support external language files.")}c.open("GET",d,false);try{c.send(null)}catch(b){return}var a=c.status;if(a==200||a==0){return c.responseText}else{var f=c.statusText+" (Error "+c.status+")";if(c.responseText.length){f+="\n"+c.responseText}alert(f);return}});Gettext.prototype.JSON=(function(data){return eval("("+data+")")});