UNPKG

ilib

Version:

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

1 lines 1.98 kB
var ilib=require("./ilib.js");var Utils=require("./Utils.js");var JSUtils=require("./JSUtils.js");var Locale=require("./Locale.js");var Address=require("./Address.js");var IString=require("./IString.js");var AddressFmt=function(t){this.sync=true;this.styleName="default";this.loadParams={};this.locale=new Locale;if(t){if(t.locale){this.locale=typeof t.locale==="string"?new Locale(t.locale):t.locale}if(typeof t.sync!=="undefined"){this.sync=t.sync==true}if(t.style){this.styleName=t.style}if(t.loadParams){this.loadParams=t.loadParams}}Utils.loadData({name:"address.json",object:AddressFmt,locale:this.locale,sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(e){if(!e||JSUtils.isEmpty(e)){Utils.loadData({name:"address.json",object:AddressFmt,locale:new Locale("XX"),sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(e){this.info=e;this._init();if(t&&typeof t.onLoad==="function"){t.onLoad(this)}})})}else{this.info=e;this._init();if(t&&typeof t.onLoad==="function"){t.onLoad(this)}}})})};AddressFmt.prototype._init=function(){this.style=this.info&&this.info.formats&&this.info.formats[this.styleName];this.style=this.style||this.info&&this.info.formats["default"]||"{streetAddress}\n{locality} {region} {postalCode}\n{country}"};AddressFmt.prototype.format=function(t){var e,s,i,o;if(!t){return""}if(t.countryCode&&t.countryCode!==this.locale.region&&Locale._isRegionCode(this.locale.region)&&this.locale.region!=="XX"){i=new AddressFmt({locale:new Locale(t.countryCode),style:this.styleName});return i.format(t)}if(typeof this.style==="object"){o=this.style[t.format||"latin"]}else{o=this.style}var a={country:t.country||"",region:t.region||"",locality:t.locality||"",streetAddress:t.streetAddress||"",postalCode:t.postalCode||"",postOffice:t.postOffice||""};s=new IString(o);e=s.format(a);e=e.replace(/[ \t]+/g," ");e=e.replace("\n ","\n");e=e.replace(" \n","\n");return e.replace(/\n+/g,"\n").trim()};module.exports=AddressFmt;