UNPKG

ilib

Version:

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

2 lines 5.4 kB
// !data pseudomap var ilib=require("./ilib.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,void 0!==options.sync&&(this.sync=!!options.sync),void 0!==options.loadParams&&(this.loadParams=options.loadParams),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;var object="ResBundle-"+this.baseName;Utils.loadData({object:object,locale:lookupLocale,name:this.baseName+".json",sync:this.sync,loadParams:this.loadParams,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){var pseudoLocale=new Locale("zxx","XX",void 0,li.getDefaultScript());this._loadPseudo(pseudoLocale,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+)?[bBhHsScCdoxXeEfgGaAtT%n]"),_pseudo:function(str){if(str){var i,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)){var m=this.percentRE.exec(str.substring(i));m&&m.length&&(ret+=m[0],i+=m[0].length)}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(i=(ret.length<=20?Math.round(ret.length/2):20<ret.length&&ret.length<=40?Math.round(ret.length/3):Math.round(ret.length/5))-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}},_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){var key=source.replace(/\s+/gm," ");return"xml"===this.type||"html"===this.type?this._unescapeXml(key):key}},_getStringSingle:function(source,key,escapeMode){if(!source&&!key)return new IString("");var trans;if(this.locale.isPseudo()){var str=source||this.map[key];trans=this._pseudo(str||key)}else{var keyName=key||this._makeKey(source);trans=void 0!==this.map[keyName]?this.map[keyName]:"pseudo"===this.missing?this._pseudo(source||key):"empty"===this.missing?"":source}if(escapeMode&&"none"!==escapeMode&&("default"==escapeMode&&(escapeMode=this.type),"xml"===escapeMode||"html"===escapeMode?trans=this._escapeXml(trans):"js"!=escapeMode&&"attribute"!==escapeMode||(trans=trans.replace(/'/g,"\\'").replace(/"/g,'\\"'))),void 0!==trans){var ret=new IString(trans);return ret.setLocale(this.locale.getSpec(),!0,this.loadParams),ret}},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){if(ilib.isArray(source))return this.getString(source,key,escapeMode).map(function(str){return str&&str instanceof IString?str.toString():str});var s=this.getString(source,key,escapeMode);return s?s.toString():void 0}},containsKey:function(source,key){if(void 0===source&&void 0===key)return!1;var keyName=key||this._makeKey(source);return void 0!==this.map[keyName]},getResObj:function(){return this.map}},module.exports=ResBundle;