UNPKG

ilib

Version:

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

2 lines 4.4 kB
// !data localematch var ilib=require("./ilib.js"),Utils=require("./Utils.js"),Locale=require("./Locale.js"),componentWeights=[.5,.2,.25,.05],LocaleMatcher=function(options){var sync=!0,loadParams=void 0;this.locale=new Locale(),options&&(void 0!==options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),void 0!==options.sync&&(sync=!!options.sync),void 0!==options.loadParams&&(loadParams=options.loadParams)),void 0===ilib.data.localematch?Utils.loadData({object:"LocaleMatcher",locale:"-",name:"localematch.json",sync:sync,loadParams:loadParams,callback:ilib.bind(this,function(info){info||(info={}),this.info=info,options&&"function"==typeof options.onLoad&&options.onLoad(this)})}):(this.info=ilib.data.localematch,options&&"function"==typeof options.onLoad&&options.onLoad(this))};LocaleMatcher.prototype={getLocale:function(){return this.locale},_getLikelyLocale:function(locale){if(locale.language&&locale.script&&locale.region)return locale;if(void 0!==this.info.likelyLocales[locale.getSpec()])return new Locale(this.info.likelyLocales[locale.getSpec()]);var partial=this.info.likelyLocales[new Locale(locale.language,void 0,locale.region).getSpec()];return void 0!==partial?new Locale(partial):void 0!==(partial=this.info.likelyLocales[new Locale(locale.language,locale.script,void 0).getSpec()])?new Locale(partial):void 0!==(partial=this.info.likelyLocales[new Locale(locale.language,void 0,void 0).getSpec()])?new Locale(partial):void 0!==(partial=this.info.likelyLocales[new Locale(void 0,locale.script,locale.region).getSpec()])?new Locale(partial):void 0!==(partial=this.info.likelyLocales[new Locale(void 0,void 0,locale.region).getSpec()])?new Locale(partial):void 0!==(partial=this.info.likelyLocales[new Locale(void 0,locale.script,void 0).getSpec()])?new Locale(partial):locale},getLikelyLocale:function(){return this._getLikelyLocale(this.locale)},match:function(locale){var thisfull,otherfull,i,other=new Locale(locale),scores=[0,0,0,0];if(this.locale.language===other.language)scores[0]=100;else if(this.locale.language&&other.language)if((this.info.macroLanguagesReverse[this.locale.language]||this.locale.language)===(this.info.macroLanguagesReverse[other.language]||other.language))scores[0]=90;else{var pair=this.locale.language+"-"+other.language;scores[0]=this.info.mutualIntelligibility[pair]||0}else thisfull=this.getLikelyLocale(),otherfull=new Locale(this.info.likelyLocales[other.getSpec()]||other.getSpec()),thisfull.language===otherfull.language&&(scores[0]=100);if(this.locale.script===other.script?scores[1]=100:this.locale.script&&other.script||(thisfull=this.locale.script?this.locale:new Locale(this.info.likelyLocales[this.locale.language]),otherfull=other.script?other:new Locale(this.info.likelyLocales[other.language]),thisfull.script===otherfull.script&&(scores[1]=100)),this.locale.region===other.region)scores[2]=100;else if(this.locale.region&&other.region){var containers=this.info.territoryContainmentReverse[this.locale.region]||[];for(i=containers.length-1;0<i;i--){var container=this.info.territoryContainment[containers[i]];if(container&&-1<container.indexOf(other.region)){scores[2]=100*(i+1)/containers.length*.2;break}}}else thisfull=this.getLikelyLocale(),otherfull=new Locale(this.info.likelyLocales[other.getSpec()]||other.getSpec()),thisfull.region===otherfull.region&&(scores[2]=100);this.locale.variant===other.variant&&(scores[3]=100);var total=0;for(i=0;i<4;i++)total+=scores[i]*componentWeights[i];return Math.round(total)},getMacroLanguage:function(){return this.info.macroLanguagesReverse[this.locale.language]||this.locale.language},_getRegionContainment:function(region){return this.info.territoryContainmentReverse[region]||[]},getRegionContainment:function(){var region=this.locale.region||this.getLikelyLocale().region;return this._getRegionContainment(region)},smallestCommonRegion:function(otherLocale){if(void 0===otherLocale)return"001";for(var thisRegion=this.locale.region||this.getLikelyLocale().region,otherLoc="string"==typeof otherLocale?new Locale(otherLocale):otherLocale,otherRegion=this._getLikelyLocale(otherLoc).region,thisRegions=this._getRegionContainment(thisRegion),otherRegions=this._getRegionContainment(otherRegion),i=thisRegions.length-1;0<i;i--)if(-1<otherRegions.indexOf(thisRegions[i]))return thisRegions[i];return"001"}},module.exports=LocaleMatcher;