t9n.js 1.4 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import e from"../request.js";import t from"../core/Error.js";import{isSome as n}from"../core/maybe.js";import{normalizeMessageBundleLocale as r}from"./messages.js";async function o(e,n,o,i){const a=n.exec(o);if(!a)throw new t("esri-intl:invalid-bundle",`Bundle id "${o}" is not compatible with the pattern "${n}"`);const c=a[1]?`${a[1]}/`:"",l=a[2],w=r(i),h=`${c}${l}.json`,u=w?`${c}${l}_${w}.json`:h;let f;try{f=await s(e(u))}catch(d){if(u===h)throw new t("intl:unknown-bundle",`Bundle "${o}" cannot be loaded`,{error:d});try{f=await s(e(h))}catch(d){throw new t("intl:unknown-bundle",`Bundle "${o}" cannot be loaded`,{error:d})}}return f}async function s(t){if(n(c.fetchBundleAsset))return c.fetchBundleAsset(t);const r=await e(t,{responseType:"text"});return JSON.parse(r.data)}class i{constructor({base:e="",pattern:t,location:n=new URL(window.location.href)}){let r;r="string"==typeof n?e=>new URL(e,new URL(n,window.location.href)).href:n instanceof URL?e=>new URL(e,n).href:n,this.pattern="string"==typeof t?new RegExp(`^${t}`):t,this.getAssetUrl=r,e=e?e.endsWith("/")?e:e+"/":"",this.matcher=new RegExp(`^${e}(?:(.*)/)?(.*)$`)}fetchMessageBundle(e,t){return o(this.getAssetUrl,this.matcher,e,t)}}function a(e){return new i(e)}const c={};export{i as JSONLoader,a as createJSONLoader,c as test};