UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

2 lines 6.21 kB
// !data pseudomap var ilib=require("../index.js"),Utils=require("./Utils.js"),JSUtils=require("./JSUtils.js"),Locale=require("./Locale.js"),LocaleInfo=require("./LocaleInfo.js"),IString=require("./IString.js"),ResBundle=function(options){var lookupLocale;this.locale=new Locale(),this.baseName="strings",this.type="text",this.loadParams={},this.missing="source",this.sync=!0,options?(options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),options.name&&(this.baseName=options.name),options.type&&(this.type=options.type),this.lengthen=options.lengthen||!1,this.path=options.basePath,void 0!==options.sync&&(this.sync=!!options.sync),void 0!==options.loadParams&&(this.loadParams=options.loadParams,this.path||(void 0!==options.loadParams.root?this.path=options.loadParams.root:void 0!==options.loadParams.base&&(this.path=options.loadParams.base))),void 0===options.missing||"pseudo"!==options.missing&&"empty"!==options.missing||(this.missing=options.missing)):options={sync:!0},this.map={},lookupLocale=this.locale.isPseudo()?new Locale("en-US"):this.locale,IString.loadPlurals(this.sync,lookupLocale,this.loadParams,ilib.bind(this,function(){Utils.loadData({locale:lookupLocale,name:this.baseName+".json",sync:this.sync,loadParams:this.loadParams,root:this.path,callback:ilib.bind(this,function(map){map=map||ilib.data[this.baseName]||{},this.map=map,this.locale.isPseudo()?this._loadPseudo(this.locale,options.onLoad):"pseudo"===this.missing?new LocaleInfo(this.locale,{sync:this.sync,loadParams:this.loadParams,onLoad:ilib.bind(this,function(li){li=new Locale("zxx","XX",void 0,li.getDefaultScript());this._loadPseudo(li,options.onLoad)})}):"function"==typeof options.onLoad&&options.onLoad(this)})})}))};ResBundle.defaultPseudo=ilib.data.pseudomap||{a:"à",e:"ë",i:"í",o:"õ",u:"ü",y:"ÿ",A:"Ã",E:"Ë",I:"Ï",O:"Ø",U:"Ú",Y:"Ŷ"},ResBundle.prototype={_loadPseudo:function(pseudoLocale,onLoad){Utils.loadData({object:"ResBundle",locale:pseudoLocale,name:"pseudomap.json",sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(map){this.pseudomap=!map||JSUtils.isEmpty(map)?ResBundle.defaultPseudo:map,"function"==typeof onLoad&&onLoad(this)})})},getLocale:function(){return this.locale},getName:function(){return this.baseName},getType:function(){return this.type},percentRE:new RegExp("%(\\d+\\$)?([\\-#\\+ 0,\\(])*(\\d+)?(\\.\\d+)?(h|hh|l|ll|j|z|t|L|q)?[diouxXfFeEgGaAcspnCS%@]"),_pseudo:function(str){if(str){var m,ret="";for(i=0;i<str.length;i++)if("raw"!==this.type){if("html"===this.type||"xml"===this.type)if("<"===str.charAt(i))for(ret+=str.charAt(i++);i<str.length&&">"!==str.charAt(i);)ret+=str.charAt(i++);else if("&"===str.charAt(i))for(ret+=str.charAt(i++);i<str.length&&";"!==str.charAt(i)&&" "!==str.charAt(i);)ret+=str.charAt(i++);else"\\"===str.charAt(i)&&"u"===str.charAt(i+1)&&(ret+=str.substring(i,i+6),i+=6);else if("c"===this.type)"%"===str.charAt(i)&&(m=this.percentRE.exec(str.substring(i)))&&m.length&&(ret+=m[0],i+=m[0].length);else if("ruby"===this.type){if("%"===str.charAt(i)&&i<str.length&&"{"!==str.charAt(i+1))for(ret+=str.charAt(i++);i<str.length&&"%"!==str.charAt(i);)ret+=str.charAt(i++)}else if("template"===this.type)if("<"===str.charAt(i)&&"%"===str.charAt(i+1))for(ret=(ret+=str.charAt(i++))+str.charAt(i++);i<str.length&&(">"!==str.charAt(i)||"%"!==str.charAt(i-1));)ret+=str.charAt(i++);else if("&"===str.charAt(i))for(ret+=str.charAt(i++);i<str.length&&";"!==str.charAt(i)&&" "!==str.charAt(i);)ret+=str.charAt(i++);else"\\"===str.charAt(i)&&"u"===str.charAt(i+1)&&(ret+=str.substring(i,i+6),i+=6);if(i<str.length)if("{"===str.charAt(i)){for(ret+=str.charAt(i++);i<str.length&&"}"!==str.charAt(i);)ret+=str.charAt(i++);i<str.length&&(ret+=str.charAt(i))}else ret+=this.pseudomap[str.charAt(i)]||str.charAt(i)}else ret+=this.pseudomap[str.charAt(i)]||str.charAt(i);if(this.lengthen)for(var add=ret.length<=20?Math.round(ret.length/2):20<ret.length&&ret.length<=40?Math.round(ret.length/3):Math.round(ret.length/5),i=add-1;0<=i;i--)ret+=i%10;return"Hans"!==this.locale.getScript()&&"Hant"!==this.locale.getScript()&&"Hani"!==this.locale.getScript()&&"Hrkt"!==this.locale.getScript()&&"Jpan"!==this.locale.getScript()&&"Hira"!==this.locale.getScript()&&"Kana"!==this.locale.getScript()||(ret=ret.replace(/ /g,"")),ret="["+(ret=void 0!==this.pseudomap.tall?this.pseudomap.tall+ret:ret)+"]"}},_escapeXml:function(str){return str=(str=(str=str.replace(/&/g,"&amp;")).replace(/</g,"&lt;")).replace(/>/g,"&gt;")},_unescapeXml:function(str){return str=(str=(str=str.replace(/&amp;/g,"&")).replace(/&lt;/g,"<")).replace(/&gt;/g,">")},_makeKey:function(source){if(source)return source=source.replace(/\s+/gm," "),"xml"===this.type||"html"===this.type?this._unescapeXml(source):source},_getStringSingle:function(source,key,escapeMode){var str;return source||key?(str=this.locale.isPseudo()?(str=source||this.map[key],this._pseudo(str||key)):(str=key||this._makeKey(source),void 0!==this.map[str]?this.map[str]:"pseudo"===this.missing?this._pseudo(source||key):"empty"===this.missing?"":source),escapeMode&&"none"!==escapeMode&&("xml"===(escapeMode="default"===escapeMode?this.type:escapeMode)||"html"===escapeMode?str=this._escapeXml(str):"js"!==escapeMode&&"attribute"!==escapeMode||(str=str.replace(/'/g,"\\'").replace(/"/g,'\\"'))),void 0!==str?((key=new IString(str)).setLocale(this.locale.getSpec(),!0,this.loadParams),key):void 0):new IString("")},getString:function(source,key,escapeMode){return source||key?ilib.isArray(source)?source.map(ilib.bind(this,function(str){return"string"==typeof str?this._getStringSingle(str,key,escapeMode):str})):this._getStringSingle(source,key,escapeMode):new IString("")},getStringJS:function(source,key,escapeMode){if(void 0!==source||void 0!==key)return ilib.isArray(source)?this.getString(source,key,escapeMode).map(function(str){return str&&str instanceof IString?str.toString():str}):(source=this.getString(source,key,escapeMode))?source.toString():void 0},containsKey:function(source,key){return(void 0!==source||void 0!==key)&&(key=key||this._makeKey(source),void 0!==this.map[key])},getResObj:function(){return this.map}},module.exports=ResBundle;