ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
2 lines • 4.11 kB
JavaScript
// !data address addressres regionnames
var ilib=require("./ilib.js"),Utils=require("./Utils.js"),JSUtils=require("./JSUtils.js"),Locale=require("./Locale.js"),Address=require("./Address.js"),IString=require("./IString.js"),ResBundle=require("./ResBundle.js"),AddressFmt=function(options){this.sync=!0,this.styleName="default",this.loadParams={},this.locale=new Locale(),options&&(options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),void 0!==options.sync&&(this.sync=!!options.sync),options.style&&(this.styleName=options.style),options.loadParams&&(this.loadParams=options.loadParams)),Utils.loadData({name:"address.json",object:"AddressFmt",locale:this.locale,sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(info){!info||JSUtils.isEmpty(info)?Utils.loadData({name:"address.json",object:"AddressFmt",locale:new Locale("XX"),sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(info){this.info=info,this._init(),options&&"function"==typeof options.onLoad&&options.onLoad(this)})}):(this.info=info,this._init(),options&&"function"==typeof options.onLoad&&options.onLoad(this))})})};function isAsianLocale(locale){return"zh"===locale.language||"ja"===locale.language||"ko"===locale.language}function invertAndFilter(object){var ret=[],regions=["AQ","EU","EZ","UN","ZZ"];for(var p in object)p&&!object[p].match(/\d/)&&-1===regions.indexOf(object[p])&&ret.push({code:object[p],name:p});return ret}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&&this.info.formats.default||"{streetAddress}\n{locality} {region} {postalCode}\n{country}"},AddressFmt.prototype.format=function(address){var format;if(!address)return"";if(address.countryCode&&address.countryCode!==this.locale.region&&Locale._isRegionCode(this.locale.region)&&"XX"!==this.locale.region)return new AddressFmt({locale:new Locale(address.countryCode),style:this.styleName}).format(address);format="object"==typeof this.style?this.style[address.format||"latin"]:this.style;var params={country:address.country||"",region:address.region||"",locality:address.locality||"",streetAddress:address.streetAddress||"",postalCode:address.postalCode||"",postOffice:address.postOffice||""};return new IString(format).format(params).replace(/[ \t]+/g," ").replace("\n ","\n").replace(" \n","\n").replace(/\n+/g,"\n").trim()},AddressFmt.prototype.getFormatInfo=function(locale,sync,callback){var info,loc=new Locale(this.locale);return locale&&("string"==typeof locale&&(locale=new Locale(locale)),loc.language=locale.getLanguage(),loc.spec=void 0),Utils.loadData({name:"regionnames.json",object:"AddressFmt",locale:loc,sync:this.sync,loadParams:JSUtils.merge(this.loadParams,{returnOne:!0},!0),callback:ilib.bind(this,function(regions){this.regions=regions,new ResBundle({locale:loc,name:"addressres",sync:this.sync,loadParams:this.loadParams,onLoad:ilib.bind(this,function(rb){var type,format,fields=this.info.fields;this.info.multiformat&&(type=isAsianLocale(this.locale)?"asian":"latin",fields=this.info.fields[type]),format="object"==typeof this.style?this.style[type||"latin"]:this.style,new Address(" ",{locale:loc,sync:this.sync,loadParams:this.loadParams,onLoad:ilib.bind(this,function(localeAddress){var rows=format.split(/\n/g);info=rows.map(ilib.bind(this,function(row){return row.split("}").filter(function(component){return 0<component.length}).map(ilib.bind(this,function(component){var name=component.replace(/.*{/,""),obj={component:name,label:rb.getStringJS(this.info.fieldNames[name])},field=fields.filter(function(f){return f.name===name});return field&&field[0]&&field[0].pattern&&"string"==typeof field[0].pattern&&(obj.constraint=field[0].pattern),"country"===name?obj.constraint=invertAndFilter(localeAddress.ctrynames):"region"===name&&this.regions[loc.getRegion()]&&(obj.constraint=this.regions[loc.getRegion()]),obj}))})),callback&&"function"==typeof callback&&callback(info)})})})})})}),info},module.exports=AddressFmt;