resolve-accept-language
Version:
Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.
1 lines • 4.68 kB
JavaScript
;var __read=this&&this.__read||function(e,r){var a="function"==typeof Symbol&&e[Symbol.iterator];if(!a)return e;var t,o,l=a.call(e),n=[];try{for(;(void 0===r||r-- >0)&&!(t=l.next()).done;)n.push(t.value)}catch(e){o={error:e}}finally{try{t&&!t.done&&(a=l.return)&&a.call(l)}finally{if(o)throw o.error}}return n},__spreadArray=this&&this.__spreadArray||function(e,r,a){if(a||2===arguments.length)for(var t,o=0,l=r.length;o<l;o++)!t&&o in r||(t||(t=Array.prototype.slice.call(r,0,o)),t[o]=r[o]);return e.concat(t||Array.prototype.slice.call(r))},__values=this&&this.__values||function(e){var r="function"==typeof Symbol&&Symbol.iterator,a=r&&e[r],t=0;if(a)return a.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&t>=e.length&&(e=void 0),{value:e&&e[t++],done:!e}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")};Object.defineProperty(exports,"__esModule",{value:!0}),exports.resolveAcceptLanguage=exports.MATCH_TYPES=void 0;var directives_1=require("./directives"),locales_1=require("./locales");exports.MATCH_TYPES={locale:"locale",languageSpecificLocale:"languageSpecificLocale",language:"language",relatedLocale:"relatedLocale",languageCountry:"languageCountry",country:"country",defaultLocale:"defaultLocale"};var resolveAcceptLanguage=function(e,r,a,t){if(r.forEach(function(e){if(!(0,locales_1.isLocale)(e,!1))throw new Error("Invalid locale identifier '".concat(e,"'. A valid locale should follow the BCP 47 'language-country' format."))}),!(0,locales_1.isLocale)(a,!1))throw new Error("Invalid default locale identifier '".concat(a,"'. A valid locale should follow the BCP 47 'language-country' format."));if(!r.some(function(e){return e.toLowerCase()===a.toLowerCase()}))throw new Error("The default locale '".concat(a,"' must be included in the locales array because it is used as a fallback when no match is found."));var o=new locales_1.Locale(a),l=new Set(__spreadArray([o.identifier],__read(r.map(function(e){return new locales_1.Locale(e).identifier})),!1)),n=function(){var r,a,n,c,i,u,f,d,s,y,v,h,g=new locales_1.LocaleList(l),_=(0,directives_1.getDirectives)(e),p=_.filter(function(e){return g.languages.has(e.languageCode)}),C=function(e){var r,a,t=e.locale,o=e.languageCode;if(void 0!==t)return g.locales.has(t)?{value:{match:t,matchType:exports.MATCH_TYPES.locale}}:"continue";var l=_.find(function(e){return e.languageCode===o&&void 0!==e.locale&&g.locales.has(e.locale)});if(l)return{value:{match:l.locale,matchType:exports.MATCH_TYPES.languageSpecificLocale}};try{for(var n=(r=void 0,__values(g.objects)),c=n.next();!c.done;c=n.next()){var i=c.value;if(i.languageCode===o)return{value:{match:i.identifier,matchType:exports.MATCH_TYPES.language}}}}catch(e){r={error:e}}finally{try{c&&!c.done&&(a=n.return)&&a.call(n)}finally{if(r)throw r.error}}};try{for(var m=__values(p),x=m.next();!x.done;x=m.next()){var T=C(A=x.value);if("object"==typeof T)return T.value}}catch(e){r={error:e}}finally{try{x&&!x.done&&(a=m.return)&&a.call(m)}finally{if(r)throw r.error}}try{for(var w=__values(p),L=w.next();!L.done;L=w.next()){var A=L.value;try{for(var S=(i=void 0,__values(g.objects)),b=S.next();!b.done;b=S.next()){if((B=b.value).languageCode===A.languageCode)return{match:B.identifier,matchType:exports.MATCH_TYPES.relatedLocale}}}catch(e){i={error:e}}finally{try{b&&!b.done&&(u=S.return)&&u.call(S)}finally{if(i)throw i.error}}}}catch(e){n={error:e}}finally{try{L&&!L.done&&(c=w.return)&&c.call(w)}finally{if(n)throw n.error}}var E=g.objects.filter(function(e){return e.languageCode===o.languageCode&&e.identifier!==o.identifier}).map(function(e){return e.countryCode});if(E.length>0)try{for(var P=__values(_),M=P.next();!M.done;M=P.next()){if(void 0!==(A=M.value).locale&&void 0!==A.countryCode&&E.includes(A.countryCode))return{match:"".concat(o.languageCode,"-").concat(A.countryCode),matchType:exports.MATCH_TYPES.languageCountry}}}catch(e){f={error:e}}finally{try{M&&!M.done&&(d=P.return)&&d.call(P)}finally{if(f)throw f.error}}if(null==t?void 0:t.matchCountry)try{for(var H=__values(_),Y=H.next();!Y.done;Y=H.next()){A=Y.value;try{for(var j=(v=void 0,__values(g.objects)),q=j.next();!q.done;q=j.next()){var B;if((B=q.value).countryCode===A.countryCode)return{match:B.identifier,matchType:exports.MATCH_TYPES.country}}}catch(e){v={error:e}}finally{try{q&&!q.done&&(h=j.return)&&h.call(j)}finally{if(v)throw v.error}}}}catch(e){s={error:e}}finally{try{Y&&!Y.done&&(y=H.return)&&y.call(H)}finally{if(s)throw s.error}}return{match:o.identifier,matchType:exports.MATCH_TYPES.defaultLocale}}();return(null==t?void 0:t.returnMatchType)?n:n.match};exports.resolveAcceptLanguage=resolveAcceptLanguage;