ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
1 lines • 1.33 kB
JavaScript
var ilib=require("./ilib.js");var Utils=require("./Utils.js");var Locale=require("./Locale.js");var IString=require("./IString.js");var StringMapper=function(e){var a=true,t=undefined;this.locale=new Locale;this.mapData={};this.mapFunction=undefined;if(e){if(typeof e.locale!=="undefined"){this.locale=typeof e.locale==="string"?new Locale(e.locale):e.locale}if(typeof e.name!=="undefined"){this.name=e.name}if(typeof e.mapFunction==="function"){this.mapFunction=e.mapFunction}if(typeof e.sync!=="undefined"){a=e.sync==true}if(typeof e.loadParams!=="undefined"){t=e.loadParams}}if(!StringMapper.cache){StringMapper.cache={}}Utils.loadData({object:StringMapper,locale:this.locale,name:this.name+".json",sync:a,loadParams:t,callback:ilib.bind(this,function(a){if(!a){var t=this.locale.getSpec().replace(/-/g,"_");StringMapper.cache[t]={}}this.mapData=a||{};if(e&&typeof e.onLoad==="function"){e.onLoad(this)}})})};StringMapper.prototype={getLocale:function(){return this.locale},getName:function(){return this.name},map:function(e){var a;if(!e){return e}if(typeof e==="string"){a=new IString(e)}else{a=e.toString()}var t="";var i=a.charIterator();var n;while(i.hasNext()){n=i.next();if(this.mapData&&this.mapData[n]){t+=this.mapData[n]}else if(this.mapFunction){t+=this.mapFunction(n)}else{t+=n}}return t}};module.exports=StringMapper;