UNPKG

ilib

Version:

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

2 lines 4.64 kB
// !data localematch var ilib=require("../index.js"),Utils=require("./Utils.js"),Locale=require("./Locale.js"),componentWeights=[.5,.2,.25,.05],multiScriptLanguages={az:!0,kk:!0,ku:!0,ky:!0,pa:!0,sr:!0,tg:!0,uz:!0,zh:!0},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){this.info=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){var partial;return locale.language&&locale.script&&locale.region?locale:void 0===this.info.likelyLocales[locale.getSpec()]?void 0!==(partial=this.info.likelyLocales[new Locale(locale.language,void 0,locale.region).getSpec()])||void 0!==(partial=this.info.likelyLocales[new Locale(locale.language,locale.script,void 0).getSpec()])||void 0!==(partial=this.info.likelyLocales[new Locale(locale.language,void 0,void 0).getSpec()])||void 0!==(partial=this.info.likelyLocales[new Locale(void 0,locale.script,locale.region).getSpec()])||void 0!==(partial=this.info.likelyLocales[new Locale(void 0,void 0,locale.region).getSpec()])||void 0!==(partial=this.info.likelyLocales[new Locale(void 0,locale.script,void 0).getSpec()])?new Locale(partial):locale:new Locale(this.info.likelyLocales[locale.getSpec()])},getLikelyLocale:function(){return this._getLikelyLocale(this.locale)},getLikelyLocaleMinimal:function(){var fullLocale=this._getLikelyLocale(this.locale),langLocale=this._getLikelyLocale(new Locale(fullLocale.language));return fullLocale.script!==langLocale.script||multiScriptLanguages[fullLocale.language]?fullLocale:new Locale(fullLocale.language,void 0,fullLocale.region)},match:function(locale){var thisfull,otherfull,other=new Locale(locale),scores=[0,0,0,0];if(this.locale.language===other.language?scores[0]=100:this.locale.language&&other.language?(this.info.macroLanguagesReverse[this.locale.language]||this.locale.language)===(this.info.macroLanguagesReverse[other.language]||other.language)?scores[0]=90:(locale=this.locale.language+"-"+other.language,scores[0]=this.info.mutualIntelligibility[locale]||0):(thisfull=this.getLikelyLocale(),otherfull=new Locale(this.info.likelyLocales[other.getSpec()]||other.getSpec()),thisfull.language===otherfull.language&&(scores[0]=100)),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)for(var containers=this.info.territoryContainmentReverse[this.locale.region]||[],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)for(var thisRegion=this.locale.region||this.getLikelyLocale().region,otherLocale="string"==typeof otherLocale?new Locale(otherLocale):otherLocale,otherLocale=this._getLikelyLocale(otherLocale).region,thisRegions=this._getRegionContainment(thisRegion),otherRegions=this._getRegionContainment(otherLocale),i=thisRegions.length-1;0<i;i--)if(-1<otherRegions.indexOf(thisRegions[i]))return thisRegions[i];return"001"}},module.exports=LocaleMatcher;