UNPKG

resolve-accept-language

Version:

Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.

1 lines 4.41 kB
var __read=this&&this.__read||function(e,r){var a="function"==typeof Symbol&&e[Symbol.iterator];if(!a)return e;var t,o,n=a.call(e),l=[];try{for(;(void 0===r||r-- >0)&&!(t=n.next()).done;)l.push(t.value)}catch(e){o={error:e}}finally{try{t&&!t.done&&(a=n.return)&&a.call(n)}finally{if(o)throw o.error}}return l},__spreadArray=this&&this.__spreadArray||function(e,r,a){if(a||2===arguments.length)for(var t,o=0,n=r.length;o<n;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.")};import{getDirectives}from"./directives.js";import{Locale,LocaleList,isLocale}from"./locales.js";export var MATCH_TYPES={locale:"locale",languageSpecificLocale:"languageSpecificLocale",language:"language",relatedLocale:"relatedLocale",languageCountry:"languageCountry",country:"country",defaultLocale:"defaultLocale"};export var resolveAcceptLanguage=function(e,r,a,t){if(r.forEach(function(e){if(!isLocale(e,!1))throw new Error("Invalid locale identifier '".concat(e,"'. A valid locale should follow the BCP 47 'language-country' format."))}),!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 Locale(a),n=new Set(__spreadArray([o.identifier],__read(r.map(function(e){return new Locale(e).identifier})),!1)),l=function(){var r,a,l,i,c,u,f,d,y,v,h,s,g=new LocaleList(n),_=getDirectives(e),m=_.filter(function(e){return g.languages.has(e.languageCode)}),p=function(e){var r,a,t=e.locale,o=e.languageCode;if(void 0!==t)return g.locales.has(t)?{value:{match:t,matchType:MATCH_TYPES.locale}}:"continue";var n=_.find(function(e){return e.languageCode===o&&void 0!==e.locale&&g.locales.has(e.locale)});if(n)return{value:{match:n.locale,matchType:MATCH_TYPES.languageSpecificLocale}};try{for(var l=(r=void 0,__values(g.objects)),i=l.next();!i.done;i=l.next()){var c=i.value;if(c.languageCode===o)return{value:{match:c.identifier,matchType:MATCH_TYPES.language}}}}catch(e){r={error:e}}finally{try{i&&!i.done&&(a=l.return)&&a.call(l)}finally{if(r)throw r.error}}};try{for(var C=__values(m),T=C.next();!T.done;T=C.next()){var w=p(S=T.value);if("object"==typeof w)return w.value}}catch(e){r={error:e}}finally{try{T&&!T.done&&(a=C.return)&&a.call(C)}finally{if(r)throw r.error}}try{for(var L=__values(m),x=L.next();!x.done;x=L.next()){var S=x.value;try{for(var b=(c=void 0,__values(g.objects)),A=b.next();!A.done;A=b.next()){if((D=A.value).languageCode===S.languageCode)return{match:D.identifier,matchType:MATCH_TYPES.relatedLocale}}}catch(e){c={error:e}}finally{try{A&&!A.done&&(u=b.return)&&u.call(b)}finally{if(c)throw c.error}}}}catch(e){l={error:e}}finally{try{x&&!x.done&&(i=L.return)&&i.call(L)}finally{if(l)throw l.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!==(S=M.value).locale&&void 0!==S.countryCode&&E.includes(S.countryCode))return{match:"".concat(o.languageCode,"-").concat(S.countryCode),matchType: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 j=__values(_),H=j.next();!H.done;H=j.next()){S=H.value;try{for(var Y=(h=void 0,__values(g.objects)),B=Y.next();!B.done;B=Y.next()){var D;if((D=B.value).countryCode===S.countryCode)return{match:D.identifier,matchType:MATCH_TYPES.country}}}catch(e){h={error:e}}finally{try{B&&!B.done&&(s=Y.return)&&s.call(Y)}finally{if(h)throw h.error}}}}catch(e){y={error:e}}finally{try{H&&!H.done&&(v=j.return)&&v.call(j)}finally{if(y)throw y.error}}return{match:o.identifier,matchType:MATCH_TYPES.defaultLocale}}();return(null==t?void 0:t.returnMatchType)?l:l.match};