/*!
Pure Javascript implementation of Uniforum message translation.
Copyright (C) 2008 Joshua I. Miller <unrtst@cpan.org>, all rights reserved
*/
Gettext=function(b){this.domain="messages";this.locale_data=undefined;var d=["domain","locale_data"];if(this.isValidObject(b)){for(var c in b){for(var a=0;a<d.length;a++){if(c==d[a]){if(this.isValidObject(b[c])){this[c]=b[c]}}}}}this.try_load_lang();return this};Gettext.context_glue="\004";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 b=this.get_lang_refs();if(typeof(b)=="object"&&b.length>0){for(var a=0;a<b.length;a++){var c=b[a];if(c.type=="application/json"){if(!this.try_load_lang_json(c.href)){throw new Error("Error: Gettext 'try_load_lang_json' failed. Unable to exec xmlhttprequest for link ["+c.href+"]")}}else{if(c.type=="application/x-po"){if(!this.try_load_lang_po(c.href)){throw new Error("Error: Gettext 'try_load_lang_po' failed. Unable to exec xmlhttprequest for link ["+c.href+"]")}}else{throw new Error("TODO: link type ["+c.type+"] found, and support is planned, but not implemented at this time.")}}}}};Gettext.prototype.parse_locale_data=function(g){if(typeof(Gettext._locale_data)=="undefined"){Gettext._locale_data={}}for(var f in g){if((!g.hasOwnProperty(f))||(!this.isValidObject(g[f]))){continue}var b=false;for(var a in g[f]){b=true;break}if(!b){continue}var i=g[f];if(f==""){f="messages"}if(!this.isValidObject(Gettext._locale_data[f])){Gettext._locale_data[f]={}}if(!this.isValidObject(Gettext._locale_data[f].head)){Gettext._locale_data[f].head={}}if(!this.isValidObject(Gettext._locale_data[f].msgs)){Gettext._locale_data[f].msgs={}}for(var n in i){if(n==""){var e=i[n];for(var m in e){var j=m.toLowerCase();Gettext._locale_data[f].head[j]=e[m]}}else{Gettext._locale_data[f].msgs[n]=i[n]}}}for(var f in Gettext._locale_data){if(this.isValidObject(Gettext._locale_data[f].head["plural-forms"])&&typeof(Gettext._locale_data[f].head.plural_func)=="undefined"){var l=Gettext._locale_data[f].head["plural-forms"];var d=new RegExp("^(\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;a-zA-Z0-9_()])+)","m");if(d.test(l)){var k=Gettext._locale_data[f].head["plural-forms"];if(!/;\s*$/.test(k)){k=k.concat(";")}var c="var plural; var nplurals; "+k+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) };';Gettext._locale_data[f].head.plural_func=new Function("n",c)}else{throw new Error("Syntax error in language file. Plural-Forms header is invalid ["+l+"]")}}else{if(typeof(Gettext._locale_data[f].head.plural_func)=="undefined"){Gettext._locale_data[f].head.plural_func=function(o){var h=(o!=1)?1:0;return{nplural:2,plural:h}}}}}return};Gettext.prototype.try_load_lang_po=function(c){var d=this.sjax(c);if(!d){return}var a=this.uri_basename(c);var b=this.parse_po(d);var e={};if(b){if(!b[""]){b[""]={}}if(!b[""]["domain"]){b[""]["domain"]=a}a=b[""]["domain"];e[a]=b;this.parse_locale_data(e)}return 1};Gettext.prototype.uri_basename=function(b){var c;if(c=b.match(/^(.*\/)?(.*)/)){var a;if(a=c[2].match(/^(.*)\..+$/)){return a[1]}else{return c[2]}}else{return""}};Gettext.prototype.parse_po=function(q){var g={};var p={};var a="";var d=[];var b=q.split("\n");for(var m=0;m<b.length;m++){b[m]=b[m].replace(/(\n|\r)+$/,"");var j;if(/^$/.test(b[m])){if(typeof(p.msgid)!="undefined"){var l=(typeof(p.msgctxt)!="undefined"&&p.msgctxt.length)?p.msgctxt+Gettext.context_glue+p.msgid:p.msgid;var n=(typeof(p.msgid_plural)!="undefined"&&p.msgid_plural.length)?p.msgid_plural:null;var f=[];for(var k in p){var j;if(j=k.match(/^msgstr_(\d+)/)){f[parseInt(j[1])]=p[k]}}f.unshift(n);if(f.length>1){g[l]=f}p={};a=""}}else{if(/^#/.test(b[m])){continue}else{if(j=b[m].match(/^msgctxt\s+(.*)/)){a="msgctxt";p[a]=this.parse_po_dequote(j[1])}else{if(j=b[m].match(/^msgid\s+(.*)/)){a="msgid";p[a]=this.parse_po_dequote(j[1])}else{if(j=b[m].match(/^msgid_plural\s+(.*)/)){a="msgid_plural";p[a]=this.parse_po_dequote(j[1])}else{if(j=b[m].match(/^msgstr\s+(.*)/)){a="msgstr_0";p[a]=this.parse_po_dequote(j[1])}else{if(j=b[m].match(/^msgstr\[0\]\s+(.*)/)){a="msgstr_0";p[a]=this.parse_po_dequote(j[1])}else{if(j=b[m].match(/^msgstr\[(\d+)\]\s+(.*)/)){a="msgstr_"+j[1];p[a]=this.parse_po_dequote(j[2])}else{if(/^"/.test(b[m])){p[a]+=this.parse_po_dequote(b[m])}else{d.push("Strange line ["+m+"] : "+b[m])}}}}}}}}}}if(typeof(p.msgid)!="undefined"){var l=(typeof(p.msgctxt)!="undefined"&&p.msgctxt.length)?p.msgctxt+Gettext.context_glue+p.msgid:p.msgid;var n=(typeof(p.msgid_plural)!="undefined"&&p.msgid_plural.length)?p.msgid_plural:null;var f=[];for(var k in p){var j;if(j=k.match(/^msgstr_(\d+)/)){f[parseInt(j[1])]=p[k]}}f.unshift(n);if(f.length>1){g[l]=f}p={};a=""}if(g[""]&&g[""][1]){var e={};var o=g[""][1].split(/\\n/);for(var m=0;m<o.length;m++){if(!o.length){continue}var h=o[m].indexOf(":",0);if(h!=-1){var s=o[m].substring(0,h);var r=o[m].substring(h+1);var c=s.toLowerCase();if(e[c]&&e[c].length){d.push("SKIPPING DUPLICATE HEADER LINE: "+o[m])}else{if(/#-#-#-#-#/.test(c)){d.push("SKIPPING ERROR MARKER IN HEADER: "+o[m])}else{r=r.replace(/^\s+/,"");e[c]=r}}}else{d.push("PROBLEM LINE IN HEADER: "+o[m]);e[o[m]]=""}}g[""]=e}else{g[""]={}}return g};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(a){var b=this.sjax(a);if(!b){return}var c=this.JSON(b);this.parse_locale_data(c);return 1};Gettext.prototype.get_lang_refs=function(){var c=new Array();var a=document.getElementsByTagName("link");for(var b=0;b<a.length;b++){if(a[b].rel=="gettext"&&a[b].href){if(typeof(a[b].type)=="undefined"||a[b].type==""){if(/\.json$/i.test(a[b].href)){a[b].type="application/json"}else{if(/\.js$/i.test(a[b].href)){a[b].type="application/json"}else{if(/\.po$/i.test(a[b].href)){a[b].type="application/x-po"}else{if(/\.mo$/i.test(a[b].href)){a[b].type="application/x-mo"}else{throw new Error("LINK tag with rel=gettext found, but the type and extension are unrecognized.")}}}}}a[b].type=a[b].type.toLowerCase();if(a[b].type=="application/json"){a[b].type="application/json"}else{if(a[b].type=="text/javascript"){a[b].type="application/json"}else{if(a[b].type=="application/x-po"){a[b].type="application/x-po"}else{if(a[b].type=="application/x-mo"){a[b].type="application/x-mo"}else{throw new Error("LINK tag with rel=gettext found, but the type attribute ["+a[b].type+"] is unrecognized.")}}}}c.push(a[b])}}return c};Gettext.prototype.textdomain=function(a){if(a&&a.length){this.domain=a}return this.domain};Gettext.prototype.gettext=function(c){var e;var a;var d;var b;return this.dcnpgettext(null,e,c,a,d,b)};Gettext.prototype.dgettext=function(b,d){var f;var a;var e;var c;return this.dcnpgettext(b,f,d,a,e,c)};Gettext.prototype.dcgettext=function(b,d,c){var f;var a;var e;return this.dcnpgettext(b,f,d,a,e,c)};Gettext.prototype.ngettext=function(c,a,e){var d;var b;return this.dcnpgettext(null,d,c,a,e,b)};Gettext.prototype.dngettext=function(b,d,a,f){var e;var c;return this.dcnpgettext(b,e,d,a,f,c)};Gettext.prototype.dcngettext=function(b,d,a,f,c){var e;return this.dcnpgettext(b,e,d,a,f,c,c)};Gettext.prototype.pgettext=function(e,c){var a;var d;var b;return this.dcnpgettext(null,e,c,a,d,b)};Gettext.prototype.dpgettext=function(b,f,d){var a;var e;var c;return this.dcnpgettext(b,f,d,a,e,c)};Gettext.prototype.dcpgettext=function(b,f,d,c){var a;var e;return this.dcnpgettext(b,f,d,a,e,c)};Gettext.prototype.npgettext=function(e,c,a,d){var b;return this.dcnpgettext(null,e,c,a,d,b)};Gettext.prototype.dnpgettext=function(b,f,d,a,e){var c;return this.dcnpgettext(b,f,d,a,e,c)};Gettext.prototype.dcnpgettext=function(g,e,y,t,k,m){if(!this.isValidObject(y)){return""}var x=this.isValidObject(t);var r=this.isValidObject(e)?e+Gettext.context_glue+y:y;var d=this.isValidObject(g)?g:this.isValidObject(this.domain)?this.domain:"messages";var w="LC_MESSAGES";var m=5;var o=new Array();if(typeof(Gettext._locale_data)!="undefined"&&this.isValidObject(Gettext._locale_data[d])){o.push(Gettext._locale_data[d])}else{if(typeof(Gettext._locale_data)!="undefined"){for(var u in Gettext._locale_data){o.push(Gettext._locale_data[u])}}}var b=[];var f=false;var l;if(o.length){for(var s=0;s<o.length;s++){var v=o[s];if(this.isValidObject(v.msgs[r])){for(var q=0;q<v.msgs[r].length;q++){b[q]=v.msgs[r][q]}b.shift();l=v;f=true;if(b.length>0&&b[0].length!=0){break}}}}if(b.length==0||b[0].length==0){b=[y,t]}var a=b[0];if(x){var h;if(f&&this.isValidObject(l.head.plural_func)){var c=l.head.plural_func(k);if(!c.plural){c.plural=0}if(!c.nplural){c.nplural=0}if(c.nplural<=c.plural){c.plural=0}h=c.plural}else{h=(k!=1)?1:0}if(this.isValidObject(b[h])){a=b[h]}}return a};Gettext.strargs=function(g,d){if(null==d||"undefined"==typeof(d)){d=[]}else{if(d.constructor!=Array){d=[d]}}var b="";while(true){var e=g.indexOf("%");var a;if(e==-1){b+=g;break}b+=g.substr(0,e);if(g.substr(e,2)=="%%"){b+="%";g=g.substr((e+2))}else{if(a=g.substr(e).match(/^%(\d+)/)){var f=parseInt(a[1]);var c=a[1].length;if(f>0&&d[f-1]!=null&&typeof(d[f-1])!="undefined"){b+=d[f-1]}g=g.substr((e+1+c))}else{b+="%";g=g.substr((e+1))}}}return b};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(f){return}var a=c.status;if(a==200||a==0){return c.responseText}else{var b=c.statusText+" (Error "+c.status+")";if(c.responseText.length){b+="\n"+c.responseText}alert(b);return}};Gettext.prototype.JSON=function(data){return eval("("+data+")")};
