@tolgee/core
Advanced tools
Comparing version 5.0.0-rc.4aa3be8.0 to 5.0.0-rc.9be0f0e.0
@@ -275,9 +275,10 @@ 'use strict'; | ||
function invalidate() { | ||
asyncRequests.clear(); | ||
version += 1; | ||
} | ||
function addRecordInternal(descriptor, data, version) { | ||
function addRecordInternal(descriptor, data, recordVersion) { | ||
const cacheKey = encodeCacheKey(descriptor); | ||
cache.set(cacheKey, { | ||
data: flattenTranslations(data), | ||
version: version, | ||
version: recordVersion, | ||
}); | ||
@@ -423,3 +424,6 @@ onCacheChange.emit(descriptor); | ||
withPromises.forEach((value, i) => { | ||
if (value.new) { | ||
const promiseChanged = asyncRequests.get(value.cacheKey) !== value.promise; | ||
// if promise has changed in between, it means cache been invalidated or | ||
// new data are being fetched | ||
if (value.new && !promiseChanged) { | ||
asyncRequests.delete(value.cacheKey); | ||
@@ -966,5 +970,2 @@ const data = results[i]; | ||
cache.addStaticData(state.getInitialOptions().staticData); | ||
if (isDev()) { | ||
cache.invalidate(); | ||
} | ||
} | ||
@@ -1096,2 +1097,5 @@ function isLoading(ns) { | ||
if (!state.isRunning()) { | ||
if (isDev()) { | ||
cache.invalidate(); | ||
} | ||
state.setRunning(true); | ||
@@ -1098,0 +1102,0 @@ pluginService.run(); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=()=>{let e=[];return Object.freeze({listen:n=>{const t=e=>{n(e)};return e.push(t),{unsubscribe:()=>{e=e.filter((e=>t!==e))}}},emit:n=>{e.forEach((e=>e({value:n})))}})};function n(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function t(e){return n(e)||[]}function a(e,n){return"object"!=typeof(a=n)||Array.isArray(a)||null===a?t(n):t(null==n?void 0:n[e]);var a}function i(e){return Array.from(new Set(e))}function o(e,n){const t=e.get(n)||0;e.set(n,t+1)}function r(e,n){let t=e.get(n)||1;t-=1,t<=0?e.delete(n):e.set(n,t)}const s=()=>{const e=new Set,n=new Set;let a=[];const i=()=>{if(0===a.length)return;e.forEach((e=>{e({value:void 0})}));let t=[],i=[];a.forEach((e=>{void 0===(null==e?void 0:e.ns)?t=void 0:void 0!==t&&(t=[...t,...e.ns]),void 0===(null==e?void 0:e.key)?i=void 0:void 0!==i&&(i=[...i,e.key])})),(i||[void 0]).forEach((e=>{((e,t)=>{n.forEach((n=>{const a=void 0!==t,i=n.namespaces.has(void 0)||-1!==(null==t?void 0:t.findIndex((e=>n.namespaces.has(e)))),o=void 0!==e,r=void 0===e||n.keys.has(e)||0===n.keys.size;a&&!i||o&&!r||n.fn({value:void 0})}))})(e,t)})),a=[]};return Object.freeze({listenSome:e=>{const a={fn:n=>{e(n)},keys:new Map,namespaces:new Map};n.add(a);const i={unsubscribe:()=>{n.delete(a)},subscribeNs:e=>(t(e).forEach((e=>o(a.namespaces,e))),i),unsubscribeNs:e=>(t(e).forEach((e=>r(a.namespaces,e))),i),subscribeKey:e=>{const{key:n,ns:r}=e;return o(a.keys,n),t(r).forEach((e=>o(a.namespaces,e))),void 0===r&&o(a.namespaces,void 0),i},unsubscribeKey:e=>{const{key:n,ns:o}=e;return r(a.keys,n),t(o).forEach((e=>r(a.namespaces,e))),void 0===o&&r(a.namespaces,void 0),i}};return i},listen:n=>{e.add(n);return{unsubscribe:()=>{e.delete(n)}}},emit:(e,n)=>{a.push(e),n?Promise.resolve().then((()=>{i()})):i()}})},g=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):Object.entries(g(t)).forEach((([t,a])=>{n.set(e+"."+t,a)})))})),n},c=e=>{const[n,t]=e.split(":");return{language:n,namespace:t||""}},u=({language:e,namespace:n})=>n?`${e}:${n}`:e,l=(e,n,a,i,o,r,s)=>{const l=new Map,d=new Map;let f={},v=0;function p(n,t,a){const i=u(n);d.set(i,{data:g(t),version:a}),e.emit(n)}function m(e,n){p(e,n,v)}function b(e,n=!1){const t=d.get(u(e));return t&&n?t.version===v:Boolean(t)}function h(e){var n;return null===(n=d.get(u(e)))||void 0===n?void 0:n.data}function y(e){let t;if(!t){const n=f[u(e)];"function"==typeof n?t=n():n&&(t=Promise.resolve(n))}return t||(t=n(e)),t||(t=Promise.resolve({})),t}return Object.freeze({addStaticData:function(e){e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=c(e),a=d.get(e);a&&0!==a.version||p(t,n,0)}})))},invalidate:function(){v+=1},addRecord:m,exists:b,getRecord:h,getTranslation:function(e,n){var t;return null===(t=d.get(u(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs:function(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=d.get(u({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return i}return Array.from(new Set(e))},getTranslationFallback:function(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=d.get(u({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation:function(n,t,a){var i;const o=null===(i=d.get(u(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching:function(e){if(o())return!0;if(void 0===e)return l.size>0;const n=t(e);return Boolean(Array.from(l.keys()).find((e=>n.includes(c(e).namespace))))},isLoading:function(e,n){const a=t(n);return Boolean(o()||Array.from(l.keys()).find((n=>{const t=c(n);return(!a.length||a.includes(t.namespace))&&!b({namespace:t.namespace,language:e})})))},loadRecords:async function(e,n){const t=e.map((e=>{const t=i(e),o=u(t),r=l.get(o);if(r)return{new:!1,promise:r,keyObject:t,cacheKey:o};const s=function(e,n){var t;let i;return n&&(i=null===(t=a(e))||void 0===t?void 0:t.catch((()=>(console.warn("Tolgee: Failed to fetch data from dev backend"),y(e))))),i||(i=y(e)),i}(t,n);return l.set(o,s),{new:!0,promise:s,keyObject:t,cacheKey:o}}));r.notify(),s.notify();const o=await Promise.all(t.map((e=>e.promise)));return t.forEach(((e,n)=>{if(e.new){l.delete(e.cacheKey);const t=o[n];t&&m(e.keyObject,t)}})),r.notify(),s.notify(),t.map((e=>h(e.keyObject)))},clear:function(){d.clear()},getAllRecords:function(){return Array.from(d.entries()).map((([e,n])=>Object.assign(Object.assign({},c(e)),{data:n.data})))}})};function d(e){return Boolean(e&&"function"==typeof e.then)}const f=(e,n)=>d(e)?Promise.resolve(e).then(n):n(e),v=e=>`Tolgee: You need to specify '${e}' option`,p=(e,n,a,i,o,r)=>{const s={ui:void 0},g={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},c=async(e,{keysAndDefaults:n})=>{var a;const r=n.map((({key:e,ns:n,defaultValue:a})=>({key:e,defaultValue:a,ns:t(i({key:e,ns:n,defaultValue:a})),translation:o({key:e,ns:n})})));null===(a=g.ui)||void 0===a||a.handleElementClick(e,r)},u=()=>{if(g.languageDetector){if(!a())throw new Error(v("availableLanguages"))}},l=(e,n)=>{var t,a;return(null===(a=null===(t=g.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},p=e=>{const n=o(e);return m(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))},m=({key:n,translation:t,defaultValue:a,noWrap:i,params:o,orEmpty:r,ns:s,formatEnabled:c})=>{var u;const l=t||a;let d=l||(r?"":n);g.observer&&!i&&(d=g.observer.wrap({key:n,translation:d,defaultValue:a,params:o,ns:s}));const f=e(),v=c||!(null===(u=g.observer)||void 0===u?void 0:u.outputNotFormattable);if(l&&f&&v)for(const e of g.formatters)d=e.format({translation:d,language:f,params:o});return g.finalFormatter&&l&&f&&v&&(d=g.finalFormatter.format({translation:d,language:f,params:o})),d};return Object.freeze({setFinalFormatter:e=>{g.finalFormatter=e},addFormatter:e=>{e&&g.formatters.push(e)},formatTranslation:m,setObserver:e=>{g.observer=null==e?void 0:e({translate:p,onClick:c})},getObserver:()=>g.observer,setUi:e=>{s.ui=(null==e?void 0:e.UI)||e},getUi:()=>s.ui,addBackend:e=>{e&&g.backends.push(e)},setDevBackend:e=>{g.devBackend=e},getDevBackend:()=>g.devBackend,getBackendRecord:({language:e,namespace:n})=>{for(const t of g.backends){const a=t.getRecord({language:e,namespace:n});if(d(a))return null==a?void 0:a.catch((e=>(console.error(e),{})));if(void 0!==a)return a}},getBackendDevRecord:({language:e,namespace:t})=>{var a;return null===(a=g.devBackend)||void 0===a?void 0:a.getRecord({apiKey:n().apiKey,apiUrl:n().apiUrl,language:e,namespace:t})},setLanguageDetector:e=>{g.languageDetector=e},setLanguageStorage:e=>{g.languageStorage=e},getInitialLanguage:()=>{var e;const n=a(),t=null===(e=g.languageStorage)||void 0===e?void 0:e.getLanguage();return f(t,(e=>n&&!n.includes(e)||!e?(()=>{if(!g.languageDetector)return;const e=a();return g.languageDetector.getLanguage({availableLanguages:e})})():e))},setStoredLanguage:e=>{var n;null===(n=g.languageStorage)||void 0===n||n.setLanguage(e)},run:()=>{var e;g.ui=s.ui&&new s.ui({apiKey:n().apiKey,apiUrl:n().apiUrl,highlight:l,changeTranslation:r}),null===(e=g.observer)||void 0===e||e.run({mouseHighlight:Boolean(g.ui)}),u()},stop:()=>{var e;g.ui=void 0,null===(e=g.observer)||void 0===e||e.stop()},retranslate:()=>{var e;null===(e=g.observer)||void 0===e||e.retranslate()},highlight:l,wrap:e=>{var n;return g.observer?null===(n=g.observer)||void 0===n?void 0:n.wrap(e):e.translation},unwrap:e=>{var n;return g.observer?null===(n=g.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}}})},m=(e,n,t)=>{let a=e;return Object.freeze({init:function(e){a=e},notify:function(){const e=n();a!==e&&t(e),a=e}})},b={enableLanguageStore:!0,defaultNs:"",filesUrlPrefix:"i18n/"},h=(e,n)=>{const t=Object.assign(Object.assign(Object.assign({},b),null==n?void 0:n.initialOptions),e),a=t.apiUrl;return t.apiUrl=a?a.replace(/\/+$/,""):a,{initialOptions:t,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}};function y(e){var{ns:n,noWrap:t,orEmpty:a,params:i}=e,o=function(e,n){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&n.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(a=Object.getOwnPropertySymbols(e);i<a.length;i++)n.indexOf(a[i])<0&&Object.prototype.propertyIsEnumerable.call(e,a[i])&&(t[a[i]]=e[a[i]])}return t}(e,["ns","noWrap","orEmpty","params"]);return{ns:n,noWrap:t,orEmpty:a,params:Object.assign(Object.assign({},o),i)}}const L=(e,...n)=>{let t,a={};return"object"==typeof e?a=e:(a.key=e,"string"==typeof n[0]?(a.defaultValue=n[0],t=n[1]):"object"==typeof n[0]&&(t=n[0])),t&&(a=Object.assign(Object.assign({},y(t)),a)),a},O=({events:e,options:n})=>{const o=m(!1,(()=>u.isFetching()),e.onFetchingChange.emit),r=m(!1,(()=>y()),e.onLoadingChange.emit),s=((e,n,o)=>{let r=h();function s(){return r.language||r.initialOptions.language}function g(){return r.initialOptions}return Object.freeze({init:function(e){r=h(e,r)},isRunning:function(){return r.isRunning},setRunning:function(e){r.isRunning!==e&&(r.isRunning=e,o.emit(e))},isInitialLoading:function(){return r.isInitialLoading},setInitialLoading:function(e){r.isInitialLoading=e},getLanguage:s,getLanguageOrFail:function(){const e=r.language||r.initialOptions.language;if(!e)throw new Error("No language set");return e},setLanguage:function(n){r.language!==n&&(r.language=n,e.emit(n))},getPendingLanguage:function(){return r.pendingLanguage||s()},setPendingLanguage:function(e){r.pendingLanguage!==e&&(r.pendingLanguage=e,n.emit(e))},getInitialOptions:g,addActiveNs:function(e){t(e).forEach((e=>{const n=r.activeNamespaces.get(e);void 0!==n?r.activeNamespaces.set(e,n+1):r.activeNamespaces.set(e,1)}))},removeActiveNs:function(e){t(e).forEach((e=>{const n=r.activeNamespaces.get(e);void 0!==n&&n>1?r.activeNamespaces.set(e,n-1):r.activeNamespaces.delete(e)}))},getRequiredNamespaces:function(){return i([...r.initialOptions.ns||[r.initialOptions.defaultNs],...r.activeNamespaces.keys()])},getFallbackLangs:function(e){const n=e||s();return n?i([n,...a(n,r.initialOptions.fallbackLanguage)]):[]},getFallbackNamespaces:function(){const e=r.initialOptions.defaultNs;return i([..."string"==typeof e?[e]:[],...t(r.initialOptions.fallbackNs)])},getAvailableLanguages:function(){if(r.initialOptions.availableLanguages)return r.initialOptions.availableLanguages;if(r.initialOptions.staticData){const e=Object.keys(r.initialOptions.staticData).map((e=>c(e).language));return Array.from(new Set(e))}},withDefaultNs:function(e){return{namespace:void 0===e.namespace?g().defaultNs:e.namespace,language:e.language}}})})(e.onLanguageChange,e.onPendingLanguageChange,e.onRunningChange),g=p(s.getLanguage,s.getInitialOptions,s.getAvailableLanguages,(function({key:e,ns:n}){const a=n?t(n):s.getFallbackNamespaces(),i=s.getFallbackLangs();return u.getTranslationNs(a,i,e)}),R,b),u=l(e.onCacheChange,g.getBackendRecord,g.getBackendDevRecord,s.withDefaultNs,s.isInitialLoading,o,r);s.init(n),u.addStaticData(s.getInitialOptions().staticData),O()&&u.invalidate(),e.onKeyUpdate.listen((()=>{s.isRunning()&&g.retranslate()}));function b(e,n,t){const a=s.withDefaultNs(e),i=u.getTranslation(a,n);return u.changeTranslation(a,n,t),{revert:()=>{u.changeTranslation(a,n,i)}}}function y(e){return u.isLoading(s.getLanguage(),e)}function O(){return Boolean(s.getInitialOptions().apiKey&&g.getDevBackend())}function k(e,n){const a=function(e,n){const a=s.getFallbackLangs(e),i=void 0!==n?t(n):s.getRequiredNamespaces(),o=[];return a.forEach((e=>{i.forEach((n=>{u.exists({language:e,namespace:n},!0)||o.push({language:e,namespace:n})}))})),o}(e,n);if(a.length)return f(E(a),(()=>{}))}function R({key:e,ns:n}){const a=n?t(n):s.getFallbackNamespaces(),i=s.getFallbackLangs();return u.getTranslationFallback(a,i,e)}function j(){const n=f(function(){if(s.getLanguage())return;if(!s.getInitialOptions().defaultLanguage)throw new Error(v("defaultLanguage"));const e=g.getInitialLanguage();return f(e,(e=>{const n=e||s.getInitialOptions().defaultLanguage;n&&s.setLanguage(n)}))}(),(()=>(s.getLanguageOrFail(),k())));if(d(n))return s.setInitialLoading(!0),o.notify(),r.notify(),Promise.resolve(n).then((()=>{s.setInitialLoading(!1),o.notify(),r.notify(),e.onInitialLoaded.emit()}));e.onInitialLoaded.emit()}function E(e){return u.loadRecords(e,O())}return Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign({},s),g),u),{init:function(e){s.init(e),u.addStaticData(s.getInitialOptions().staticData),O()&&u.invalidate()},changeLanguage:async function(e){s.getPendingLanguage()===e&&s.getLanguage()===e||(s.setPendingLanguage(e),s.isRunning()&&await k(e),e===s.getPendingLanguage()&&(s.setLanguage(e),g.setStoredLanguage(e)))},getTranslation:R,changeTranslation:b,addActiveNs:async function(e,n){n||s.addActiveNs(e),s.isRunning()&&await k(void 0,e)},loadRequiredRecords:k,loadRecords:E,loadRecord:async function(e){return(await E([e]))[0]},isLoading:y,isLoaded:function(e){const n=s.getLanguage();if(!n)return!1;const a=s.getFallbackLangs(n),i=void 0!==e?t(e):s.getRequiredNamespaces(),o=[];return a.forEach((e=>{i.forEach((n=>{u.exists({language:e,namespace:n})||o.push({language:e,namespace:n})}))})),0===o.length},t:(...e)=>{const n=L(...e),t=R(n);return g.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:O,run:function(){let e;return s.isRunning()||(s.setRunning(!0),g.run(),e=j()),Promise.resolve(e)},stop:function(){s.isRunning()&&(g.stop(),s.setRunning(!1))}}))};exports.DEVTOOLS_ID="__tolgee_dev_tools",exports.RESTRICTED_ASCENDANT_ATTRIBUTE="data-tolgee-restricted",exports.TOLGEE_ATTRIBUTE_NAME="_tolgee",exports.TOLGEE_HIGHLIGHTER_CLASS="_tolgee-highlighter",exports.TOLGEE_WRAPPED_ONLY_DATA_ATTRIBUTE="data-tolgee-key-only",exports.Tolgee=n=>{const t=(()=>{const n=e(),t=e(),a=e(),i=e(),o=e(),r=e(),g=s(),c=e(),u=e();return r.listen((()=>g.emit())),t.listen((()=>g.emit())),c.listen((({value:e})=>{g.emit({ns:[e.namespace],key:e.key},!0)})),Object.freeze({onPendingLanguageChange:n,onLanguageChange:t,onKeyChange:a,onKeyUpdate:g,onLoadingChange:i,onFetchingChange:o,onInitialLoaded:r,onRunningChange:u,onCacheChange:c,on:(e,a)=>{switch(e){case"pendingLanguage":return n.listen(a);case"language":return t.listen(a);case"loading":return i.listen(a);case"fetching":return o.listen(a);case"initialLoad":return r.listen(a);case"running":return u.listen(a);case"cache":return c.listen(a);case"keyUpdate":return g.listen(a)}}})})(),a=O({events:t,options:n}),i=Object.freeze({setFinalFormatter:a.setFinalFormatter,addFormatter:a.addFormatter,setObserver:a.setObserver,getObserver:a.getObserver,setUi:a.setUi,getUi:a.getUi,setDevBackend:a.setDevBackend,addBackend:a.addBackend,setLanguageDetector:a.setLanguageDetector,setLanguageStorage:a.setLanguageStorage}),o=e=>{const n=a.isRunning();n&&a.stop(),e(),n&&a.run()},r=Object.freeze({on:t.on,onKeyUpdate:t.onKeyUpdate.listenSome,getLanguage:a.getLanguage,getPendingLanguage:a.getPendingLanguage,changeLanguage:a.changeLanguage,changeTranslation:a.changeTranslation,addActiveNs:a.addActiveNs,removeActiveNs:a.removeActiveNs,loadRecords:a.loadRecords,loadRecord:a.loadRecord,addStaticData:a.addStaticData,getRecord:a.getRecord,getAllRecords:a.getAllRecords,isLoaded:a.isLoaded,isInitialLoading:a.isInitialLoading,isLoading:a.isLoading,isFetching:a.isFetching,isRunning:a.isRunning,run:a.run,stop:a.stop,t:a.t,highlight:a.highlight,getInitialOptions:a.getInitialOptions,isDev:a.isDev,wrap:a.wrap,unwrap:a.unwrap,use:e=>(e&&o((()=>e(r,i))),r),init:e=>(o((()=>a.init(e))),r)});return r},exports.getFallback=n,exports.getFallbackArray=t,exports.getTranslateParams=L; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=()=>{let e=[];return Object.freeze({listen:n=>{const t=e=>{n(e)};return e.push(t),{unsubscribe:()=>{e=e.filter((e=>t!==e))}}},emit:n=>{e.forEach((e=>e({value:n})))}})};function n(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function t(e){return n(e)||[]}function a(e,n){return"object"!=typeof(a=n)||Array.isArray(a)||null===a?t(n):t(null==n?void 0:n[e]);var a}function i(e){return Array.from(new Set(e))}function o(e,n){const t=e.get(n)||0;e.set(n,t+1)}function r(e,n){let t=e.get(n)||1;t-=1,t<=0?e.delete(n):e.set(n,t)}const s=()=>{const e=new Set,n=new Set;let a=[];const i=()=>{if(0===a.length)return;e.forEach((e=>{e({value:void 0})}));let t=[],i=[];a.forEach((e=>{void 0===(null==e?void 0:e.ns)?t=void 0:void 0!==t&&(t=[...t,...e.ns]),void 0===(null==e?void 0:e.key)?i=void 0:void 0!==i&&(i=[...i,e.key])})),(i||[void 0]).forEach((e=>{((e,t)=>{n.forEach((n=>{const a=void 0!==t,i=n.namespaces.has(void 0)||-1!==(null==t?void 0:t.findIndex((e=>n.namespaces.has(e)))),o=void 0!==e,r=void 0===e||n.keys.has(e)||0===n.keys.size;a&&!i||o&&!r||n.fn({value:void 0})}))})(e,t)})),a=[]};return Object.freeze({listenSome:e=>{const a={fn:n=>{e(n)},keys:new Map,namespaces:new Map};n.add(a);const i={unsubscribe:()=>{n.delete(a)},subscribeNs:e=>(t(e).forEach((e=>o(a.namespaces,e))),i),unsubscribeNs:e=>(t(e).forEach((e=>r(a.namespaces,e))),i),subscribeKey:e=>{const{key:n,ns:r}=e;return o(a.keys,n),t(r).forEach((e=>o(a.namespaces,e))),void 0===r&&o(a.namespaces,void 0),i},unsubscribeKey:e=>{const{key:n,ns:o}=e;return r(a.keys,n),t(o).forEach((e=>r(a.namespaces,e))),void 0===o&&r(a.namespaces,void 0),i}};return i},listen:n=>{e.add(n);return{unsubscribe:()=>{e.delete(n)}}},emit:(e,n)=>{a.push(e),n?Promise.resolve().then((()=>{i()})):i()}})},g=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):Object.entries(g(t)).forEach((([t,a])=>{n.set(e+"."+t,a)})))})),n},c=e=>{const[n,t]=e.split(":");return{language:n,namespace:t||""}},u=({language:e,namespace:n})=>n?`${e}:${n}`:e,l=(e,n,a,i,o,r,s)=>{const l=new Map,d=new Map;let f={},v=0;function p(n,t,a){const i=u(n);d.set(i,{data:g(t),version:a}),e.emit(n)}function m(e,n){p(e,n,v)}function b(e,n=!1){const t=d.get(u(e));return t&&n?t.version===v:Boolean(t)}function h(e){var n;return null===(n=d.get(u(e)))||void 0===n?void 0:n.data}function y(e){let t;if(!t){const n=f[u(e)];"function"==typeof n?t=n():n&&(t=Promise.resolve(n))}return t||(t=n(e)),t||(t=Promise.resolve({})),t}return Object.freeze({addStaticData:function(e){e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=c(e),a=d.get(e);a&&0!==a.version||p(t,n,0)}})))},invalidate:function(){l.clear(),v+=1},addRecord:m,exists:b,getRecord:h,getTranslation:function(e,n){var t;return null===(t=d.get(u(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs:function(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=d.get(u({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return i}return Array.from(new Set(e))},getTranslationFallback:function(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=d.get(u({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation:function(n,t,a){var i;const o=null===(i=d.get(u(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching:function(e){if(o())return!0;if(void 0===e)return l.size>0;const n=t(e);return Boolean(Array.from(l.keys()).find((e=>n.includes(c(e).namespace))))},isLoading:function(e,n){const a=t(n);return Boolean(o()||Array.from(l.keys()).find((n=>{const t=c(n);return(!a.length||a.includes(t.namespace))&&!b({namespace:t.namespace,language:e})})))},loadRecords:async function(e,n){const t=e.map((e=>{const t=i(e),o=u(t),r=l.get(o);if(r)return{new:!1,promise:r,keyObject:t,cacheKey:o};const s=function(e,n){var t;let i;return n&&(i=null===(t=a(e))||void 0===t?void 0:t.catch((()=>(console.warn("Tolgee: Failed to fetch data from dev backend"),y(e))))),i||(i=y(e)),i}(t,n);return l.set(o,s),{new:!0,promise:s,keyObject:t,cacheKey:o}}));r.notify(),s.notify();const o=await Promise.all(t.map((e=>e.promise)));return t.forEach(((e,n)=>{const t=l.get(e.cacheKey)!==e.promise;if(e.new&&!t){l.delete(e.cacheKey);const t=o[n];t&&m(e.keyObject,t)}})),r.notify(),s.notify(),t.map((e=>h(e.keyObject)))},clear:function(){d.clear()},getAllRecords:function(){return Array.from(d.entries()).map((([e,n])=>Object.assign(Object.assign({},c(e)),{data:n.data})))}})};function d(e){return Boolean(e&&"function"==typeof e.then)}const f=(e,n)=>d(e)?Promise.resolve(e).then(n):n(e),v=e=>`Tolgee: You need to specify '${e}' option`,p=(e,n,a,i,o,r)=>{const s={ui:void 0},g={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},c=async(e,{keysAndDefaults:n})=>{var a;const r=n.map((({key:e,ns:n,defaultValue:a})=>({key:e,defaultValue:a,ns:t(i({key:e,ns:n,defaultValue:a})),translation:o({key:e,ns:n})})));null===(a=g.ui)||void 0===a||a.handleElementClick(e,r)},u=()=>{if(g.languageDetector){if(!a())throw new Error(v("availableLanguages"))}},l=(e,n)=>{var t,a;return(null===(a=null===(t=g.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},p=e=>{const n=o(e);return m(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))},m=({key:n,translation:t,defaultValue:a,noWrap:i,params:o,orEmpty:r,ns:s,formatEnabled:c})=>{var u;const l=t||a;let d=l||(r?"":n);g.observer&&!i&&(d=g.observer.wrap({key:n,translation:d,defaultValue:a,params:o,ns:s}));const f=e(),v=c||!(null===(u=g.observer)||void 0===u?void 0:u.outputNotFormattable);if(l&&f&&v)for(const e of g.formatters)d=e.format({translation:d,language:f,params:o});return g.finalFormatter&&l&&f&&v&&(d=g.finalFormatter.format({translation:d,language:f,params:o})),d};return Object.freeze({setFinalFormatter:e=>{g.finalFormatter=e},addFormatter:e=>{e&&g.formatters.push(e)},formatTranslation:m,setObserver:e=>{g.observer=null==e?void 0:e({translate:p,onClick:c})},getObserver:()=>g.observer,setUi:e=>{s.ui=(null==e?void 0:e.UI)||e},getUi:()=>s.ui,addBackend:e=>{e&&g.backends.push(e)},setDevBackend:e=>{g.devBackend=e},getDevBackend:()=>g.devBackend,getBackendRecord:({language:e,namespace:n})=>{for(const t of g.backends){const a=t.getRecord({language:e,namespace:n});if(d(a))return null==a?void 0:a.catch((e=>(console.error(e),{})));if(void 0!==a)return a}},getBackendDevRecord:({language:e,namespace:t})=>{var a;return null===(a=g.devBackend)||void 0===a?void 0:a.getRecord({apiKey:n().apiKey,apiUrl:n().apiUrl,language:e,namespace:t})},setLanguageDetector:e=>{g.languageDetector=e},setLanguageStorage:e=>{g.languageStorage=e},getInitialLanguage:()=>{var e;const n=a(),t=null===(e=g.languageStorage)||void 0===e?void 0:e.getLanguage();return f(t,(e=>n&&!n.includes(e)||!e?(()=>{if(!g.languageDetector)return;const e=a();return g.languageDetector.getLanguage({availableLanguages:e})})():e))},setStoredLanguage:e=>{var n;null===(n=g.languageStorage)||void 0===n||n.setLanguage(e)},run:()=>{var e;g.ui=s.ui&&new s.ui({apiKey:n().apiKey,apiUrl:n().apiUrl,highlight:l,changeTranslation:r}),null===(e=g.observer)||void 0===e||e.run({mouseHighlight:Boolean(g.ui)}),u()},stop:()=>{var e;g.ui=void 0,null===(e=g.observer)||void 0===e||e.stop()},retranslate:()=>{var e;null===(e=g.observer)||void 0===e||e.retranslate()},highlight:l,wrap:e=>{var n;return g.observer?null===(n=g.observer)||void 0===n?void 0:n.wrap(e):e.translation},unwrap:e=>{var n;return g.observer?null===(n=g.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}}})},m=(e,n,t)=>{let a=e;return Object.freeze({init:function(e){a=e},notify:function(){const e=n();a!==e&&t(e),a=e}})},b={enableLanguageStore:!0,defaultNs:"",filesUrlPrefix:"i18n/"},h=(e,n)=>{const t=Object.assign(Object.assign(Object.assign({},b),null==n?void 0:n.initialOptions),e),a=t.apiUrl;return t.apiUrl=a?a.replace(/\/+$/,""):a,{initialOptions:t,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}};function y(e){var{ns:n,noWrap:t,orEmpty:a,params:i}=e,o=function(e,n){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&n.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(a=Object.getOwnPropertySymbols(e);i<a.length;i++)n.indexOf(a[i])<0&&Object.prototype.propertyIsEnumerable.call(e,a[i])&&(t[a[i]]=e[a[i]])}return t}(e,["ns","noWrap","orEmpty","params"]);return{ns:n,noWrap:t,orEmpty:a,params:Object.assign(Object.assign({},o),i)}}const L=(e,...n)=>{let t,a={};return"object"==typeof e?a=e:(a.key=e,"string"==typeof n[0]?(a.defaultValue=n[0],t=n[1]):"object"==typeof n[0]&&(t=n[0])),t&&(a=Object.assign(Object.assign({},y(t)),a)),a},O=({events:e,options:n})=>{const o=m(!1,(()=>u.isFetching()),e.onFetchingChange.emit),r=m(!1,(()=>y()),e.onLoadingChange.emit),s=((e,n,o)=>{let r=h();function s(){return r.language||r.initialOptions.language}function g(){return r.initialOptions}return Object.freeze({init:function(e){r=h(e,r)},isRunning:function(){return r.isRunning},setRunning:function(e){r.isRunning!==e&&(r.isRunning=e,o.emit(e))},isInitialLoading:function(){return r.isInitialLoading},setInitialLoading:function(e){r.isInitialLoading=e},getLanguage:s,getLanguageOrFail:function(){const e=r.language||r.initialOptions.language;if(!e)throw new Error("No language set");return e},setLanguage:function(n){r.language!==n&&(r.language=n,e.emit(n))},getPendingLanguage:function(){return r.pendingLanguage||s()},setPendingLanguage:function(e){r.pendingLanguage!==e&&(r.pendingLanguage=e,n.emit(e))},getInitialOptions:g,addActiveNs:function(e){t(e).forEach((e=>{const n=r.activeNamespaces.get(e);void 0!==n?r.activeNamespaces.set(e,n+1):r.activeNamespaces.set(e,1)}))},removeActiveNs:function(e){t(e).forEach((e=>{const n=r.activeNamespaces.get(e);void 0!==n&&n>1?r.activeNamespaces.set(e,n-1):r.activeNamespaces.delete(e)}))},getRequiredNamespaces:function(){return i([...r.initialOptions.ns||[r.initialOptions.defaultNs],...r.activeNamespaces.keys()])},getFallbackLangs:function(e){const n=e||s();return n?i([n,...a(n,r.initialOptions.fallbackLanguage)]):[]},getFallbackNamespaces:function(){const e=r.initialOptions.defaultNs;return i([..."string"==typeof e?[e]:[],...t(r.initialOptions.fallbackNs)])},getAvailableLanguages:function(){if(r.initialOptions.availableLanguages)return r.initialOptions.availableLanguages;if(r.initialOptions.staticData){const e=Object.keys(r.initialOptions.staticData).map((e=>c(e).language));return Array.from(new Set(e))}},withDefaultNs:function(e){return{namespace:void 0===e.namespace?g().defaultNs:e.namespace,language:e.language}}})})(e.onLanguageChange,e.onPendingLanguageChange,e.onRunningChange),g=p(s.getLanguage,s.getInitialOptions,s.getAvailableLanguages,(function({key:e,ns:n}){const a=n?t(n):s.getFallbackNamespaces(),i=s.getFallbackLangs();return u.getTranslationNs(a,i,e)}),R,b),u=l(e.onCacheChange,g.getBackendRecord,g.getBackendDevRecord,s.withDefaultNs,s.isInitialLoading,o,r);s.init(n),u.addStaticData(s.getInitialOptions().staticData),O()&&u.invalidate(),e.onKeyUpdate.listen((()=>{s.isRunning()&&g.retranslate()}));function b(e,n,t){const a=s.withDefaultNs(e),i=u.getTranslation(a,n);return u.changeTranslation(a,n,t),{revert:()=>{u.changeTranslation(a,n,i)}}}function y(e){return u.isLoading(s.getLanguage(),e)}function O(){return Boolean(s.getInitialOptions().apiKey&&g.getDevBackend())}function k(e,n){const a=function(e,n){const a=s.getFallbackLangs(e),i=void 0!==n?t(n):s.getRequiredNamespaces(),o=[];return a.forEach((e=>{i.forEach((n=>{u.exists({language:e,namespace:n},!0)||o.push({language:e,namespace:n})}))})),o}(e,n);if(a.length)return f(E(a),(()=>{}))}function R({key:e,ns:n}){const a=n?t(n):s.getFallbackNamespaces(),i=s.getFallbackLangs();return u.getTranslationFallback(a,i,e)}function j(){const n=f(function(){if(s.getLanguage())return;if(!s.getInitialOptions().defaultLanguage)throw new Error(v("defaultLanguage"));const e=g.getInitialLanguage();return f(e,(e=>{const n=e||s.getInitialOptions().defaultLanguage;n&&s.setLanguage(n)}))}(),(()=>(s.getLanguageOrFail(),k())));if(d(n))return s.setInitialLoading(!0),o.notify(),r.notify(),Promise.resolve(n).then((()=>{s.setInitialLoading(!1),o.notify(),r.notify(),e.onInitialLoaded.emit()}));e.onInitialLoaded.emit()}function E(e){return u.loadRecords(e,O())}return Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign({},s),g),u),{init:function(e){s.init(e),u.addStaticData(s.getInitialOptions().staticData)},changeLanguage:async function(e){s.getPendingLanguage()===e&&s.getLanguage()===e||(s.setPendingLanguage(e),s.isRunning()&&await k(e),e===s.getPendingLanguage()&&(s.setLanguage(e),g.setStoredLanguage(e)))},getTranslation:R,changeTranslation:b,addActiveNs:async function(e,n){n||s.addActiveNs(e),s.isRunning()&&await k(void 0,e)},loadRequiredRecords:k,loadRecords:E,loadRecord:async function(e){return(await E([e]))[0]},isLoading:y,isLoaded:function(e){const n=s.getLanguage();if(!n)return!1;const a=s.getFallbackLangs(n),i=void 0!==e?t(e):s.getRequiredNamespaces(),o=[];return a.forEach((e=>{i.forEach((n=>{u.exists({language:e,namespace:n})||o.push({language:e,namespace:n})}))})),0===o.length},t:(...e)=>{const n=L(...e),t=R(n);return g.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:O,run:function(){let e;return s.isRunning()||(O()&&u.invalidate(),s.setRunning(!0),g.run(),e=j()),Promise.resolve(e)},stop:function(){s.isRunning()&&(g.stop(),s.setRunning(!1))}}))};exports.DEVTOOLS_ID="__tolgee_dev_tools",exports.RESTRICTED_ASCENDANT_ATTRIBUTE="data-tolgee-restricted",exports.TOLGEE_ATTRIBUTE_NAME="_tolgee",exports.TOLGEE_HIGHLIGHTER_CLASS="_tolgee-highlighter",exports.TOLGEE_WRAPPED_ONLY_DATA_ATTRIBUTE="data-tolgee-key-only",exports.Tolgee=n=>{const t=(()=>{const n=e(),t=e(),a=e(),i=e(),o=e(),r=e(),g=s(),c=e(),u=e();return r.listen((()=>g.emit())),t.listen((()=>g.emit())),c.listen((({value:e})=>{g.emit({ns:[e.namespace],key:e.key},!0)})),Object.freeze({onPendingLanguageChange:n,onLanguageChange:t,onKeyChange:a,onKeyUpdate:g,onLoadingChange:i,onFetchingChange:o,onInitialLoaded:r,onRunningChange:u,onCacheChange:c,on:(e,a)=>{switch(e){case"pendingLanguage":return n.listen(a);case"language":return t.listen(a);case"loading":return i.listen(a);case"fetching":return o.listen(a);case"initialLoad":return r.listen(a);case"running":return u.listen(a);case"cache":return c.listen(a);case"keyUpdate":return g.listen(a)}}})})(),a=O({events:t,options:n}),i=Object.freeze({setFinalFormatter:a.setFinalFormatter,addFormatter:a.addFormatter,setObserver:a.setObserver,getObserver:a.getObserver,setUi:a.setUi,getUi:a.getUi,setDevBackend:a.setDevBackend,addBackend:a.addBackend,setLanguageDetector:a.setLanguageDetector,setLanguageStorage:a.setLanguageStorage}),o=e=>{const n=a.isRunning();n&&a.stop(),e(),n&&a.run()},r=Object.freeze({on:t.on,onKeyUpdate:t.onKeyUpdate.listenSome,getLanguage:a.getLanguage,getPendingLanguage:a.getPendingLanguage,changeLanguage:a.changeLanguage,changeTranslation:a.changeTranslation,addActiveNs:a.addActiveNs,removeActiveNs:a.removeActiveNs,loadRecords:a.loadRecords,loadRecord:a.loadRecord,addStaticData:a.addStaticData,getRecord:a.getRecord,getAllRecords:a.getAllRecords,isLoaded:a.isLoaded,isInitialLoading:a.isInitialLoading,isLoading:a.isLoading,isFetching:a.isFetching,isRunning:a.isRunning,run:a.run,stop:a.stop,t:a.t,highlight:a.highlight,getInitialOptions:a.getInitialOptions,isDev:a.isDev,wrap:a.wrap,unwrap:a.unwrap,use:e=>(e&&o((()=>e(r,i))),r),init:e=>(o((()=>a.init(e))),r)});return r},exports.getFallback=n,exports.getFallbackArray=t,exports.getTranslateParams=L; | ||
//# sourceMappingURL=tolgee.cjs.min.js.map |
@@ -277,9 +277,10 @@ (function (global, factory) { | ||
function invalidate() { | ||
asyncRequests.clear(); | ||
version += 1; | ||
} | ||
function addRecordInternal(descriptor, data, version) { | ||
function addRecordInternal(descriptor, data, recordVersion) { | ||
const cacheKey = encodeCacheKey(descriptor); | ||
cache.set(cacheKey, { | ||
data: flattenTranslations(data), | ||
version: version, | ||
version: recordVersion, | ||
}); | ||
@@ -425,3 +426,6 @@ onCacheChange.emit(descriptor); | ||
withPromises.forEach((value, i) => { | ||
if (value.new) { | ||
const promiseChanged = asyncRequests.get(value.cacheKey) !== value.promise; | ||
// if promise has changed in between, it means cache been invalidated or | ||
// new data are being fetched | ||
if (value.new && !promiseChanged) { | ||
asyncRequests.delete(value.cacheKey); | ||
@@ -968,5 +972,2 @@ const data = results[i]; | ||
cache.addStaticData(state.getInitialOptions().staticData); | ||
if (isDev()) { | ||
cache.invalidate(); | ||
} | ||
} | ||
@@ -1098,2 +1099,5 @@ function isLoading(ns) { | ||
if (!state.isRunning()) { | ||
if (isDev()) { | ||
cache.invalidate(); | ||
} | ||
state.setRunning(true); | ||
@@ -1100,0 +1104,0 @@ pluginService.run(); |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@tolgee/core"]={})}(this,(function(e){"use strict";const n=()=>{let e=[];return Object.freeze({listen:n=>{const t=e=>{n(e)};return e.push(t),{unsubscribe:()=>{e=e.filter((e=>t!==e))}}},emit:n=>{e.forEach((e=>e({value:n})))}})};function t(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function a(e){return t(e)||[]}function i(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?a(n):a(null==n?void 0:n[e]);var t}function o(e){return Array.from(new Set(e))}function s(e,n){const t=e.get(n)||0;e.set(n,t+1)}function r(e,n){let t=e.get(n)||1;t-=1,t<=0?e.delete(n):e.set(n,t)}const g=()=>{const e=new Set,n=new Set;let t=[];const i=()=>{if(0===t.length)return;e.forEach((e=>{e({value:void 0})}));let a=[],i=[];t.forEach((e=>{void 0===(null==e?void 0:e.ns)?a=void 0:void 0!==a&&(a=[...a,...e.ns]),void 0===(null==e?void 0:e.key)?i=void 0:void 0!==i&&(i=[...i,e.key])})),(i||[void 0]).forEach((e=>{((e,t)=>{n.forEach((n=>{const a=void 0!==t,i=n.namespaces.has(void 0)||-1!==(null==t?void 0:t.findIndex((e=>n.namespaces.has(e)))),o=void 0!==e,s=void 0===e||n.keys.has(e)||0===n.keys.size;a&&!i||o&&!s||n.fn({value:void 0})}))})(e,a)})),t=[]};return Object.freeze({listenSome:e=>{const t={fn:n=>{e(n)},keys:new Map,namespaces:new Map};n.add(t);const i={unsubscribe:()=>{n.delete(t)},subscribeNs:e=>(a(e).forEach((e=>s(t.namespaces,e))),i),unsubscribeNs:e=>(a(e).forEach((e=>r(t.namespaces,e))),i),subscribeKey:e=>{const{key:n,ns:o}=e;return s(t.keys,n),a(o).forEach((e=>s(t.namespaces,e))),void 0===o&&s(t.namespaces,void 0),i},unsubscribeKey:e=>{const{key:n,ns:o}=e;return r(t.keys,n),a(o).forEach((e=>r(t.namespaces,e))),void 0===o&&r(t.namespaces,void 0),i}};return i},listen:n=>{e.add(n);return{unsubscribe:()=>{e.delete(n)}}},emit:(e,n)=>{t.push(e),n?Promise.resolve().then((()=>{i()})):i()}})},c=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):Object.entries(c(t)).forEach((([t,a])=>{n.set(e+"."+t,a)})))})),n},u=e=>{const[n,t]=e.split(":");return{language:n,namespace:t||""}},l=({language:e,namespace:n})=>n?`${e}:${n}`:e,d=(e,n,t,i,o,s,r)=>{const g=new Map,d=new Map;let f={},v=0;function p(n,t,a){const i=l(n);d.set(i,{data:c(t),version:a}),e.emit(n)}function m(e,n){p(e,n,v)}function b(e,n=!1){const t=d.get(l(e));return t&&n?t.version===v:Boolean(t)}function h(e){var n;return null===(n=d.get(l(e)))||void 0===n?void 0:n.data}function y(e){let t;if(!t){const n=f[l(e)];"function"==typeof n?t=n():n&&(t=Promise.resolve(n))}return t||(t=n(e)),t||(t=Promise.resolve({})),t}return Object.freeze({addStaticData:function(e){e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=u(e),a=d.get(e);a&&0!==a.version||p(t,n,0)}})))},invalidate:function(){v+=1},addRecord:m,exists:b,getRecord:h,getTranslation:function(e,n){var t;return null===(t=d.get(l(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs:function(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=d.get(l({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return i}return Array.from(new Set(e))},getTranslationFallback:function(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=d.get(l({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation:function(n,t,a){var i;const o=null===(i=d.get(l(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching:function(e){if(o())return!0;if(void 0===e)return g.size>0;const n=a(e);return Boolean(Array.from(g.keys()).find((e=>n.includes(u(e).namespace))))},isLoading:function(e,n){const t=a(n);return Boolean(o()||Array.from(g.keys()).find((n=>{const a=u(n);return(!t.length||t.includes(a.namespace))&&!b({namespace:a.namespace,language:e})})))},loadRecords:async function(e,n){const a=e.map((e=>{const a=i(e),o=l(a),s=g.get(o);if(s)return{new:!1,promise:s,keyObject:a,cacheKey:o};const r=function(e,n){var a;let i;return n&&(i=null===(a=t(e))||void 0===a?void 0:a.catch((()=>(console.warn("Tolgee: Failed to fetch data from dev backend"),y(e))))),i||(i=y(e)),i}(a,n);return g.set(o,r),{new:!0,promise:r,keyObject:a,cacheKey:o}}));s.notify(),r.notify();const o=await Promise.all(a.map((e=>e.promise)));return a.forEach(((e,n)=>{if(e.new){g.delete(e.cacheKey);const t=o[n];t&&m(e.keyObject,t)}})),s.notify(),r.notify(),a.map((e=>h(e.keyObject)))},clear:function(){d.clear()},getAllRecords:function(){return Array.from(d.entries()).map((([e,n])=>Object.assign(Object.assign({},u(e)),{data:n.data})))}})};function f(e){return Boolean(e&&"function"==typeof e.then)}const v=(e,n)=>f(e)?Promise.resolve(e).then(n):n(e),p=e=>`Tolgee: You need to specify '${e}' option`,m=(e,n,t,i,o,s)=>{const r={ui:void 0},g={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},c=async(e,{keysAndDefaults:n})=>{var t;const s=n.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,ns:a(i({key:e,ns:n,defaultValue:t})),translation:o({key:e,ns:n})})));null===(t=g.ui)||void 0===t||t.handleElementClick(e,s)},u=()=>{if(g.languageDetector){if(!t())throw new Error(p("availableLanguages"))}},l=(e,n)=>{var t,a;return(null===(a=null===(t=g.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},d=e=>{const n=o(e);return m(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))},m=({key:n,translation:t,defaultValue:a,noWrap:i,params:o,orEmpty:s,ns:r,formatEnabled:c})=>{var u;const l=t||a;let d=l||(s?"":n);g.observer&&!i&&(d=g.observer.wrap({key:n,translation:d,defaultValue:a,params:o,ns:r}));const f=e(),v=c||!(null===(u=g.observer)||void 0===u?void 0:u.outputNotFormattable);if(l&&f&&v)for(const e of g.formatters)d=e.format({translation:d,language:f,params:o});return g.finalFormatter&&l&&f&&v&&(d=g.finalFormatter.format({translation:d,language:f,params:o})),d};return Object.freeze({setFinalFormatter:e=>{g.finalFormatter=e},addFormatter:e=>{e&&g.formatters.push(e)},formatTranslation:m,setObserver:e=>{g.observer=null==e?void 0:e({translate:d,onClick:c})},getObserver:()=>g.observer,setUi:e=>{r.ui=(null==e?void 0:e.UI)||e},getUi:()=>r.ui,addBackend:e=>{e&&g.backends.push(e)},setDevBackend:e=>{g.devBackend=e},getDevBackend:()=>g.devBackend,getBackendRecord:({language:e,namespace:n})=>{for(const t of g.backends){const a=t.getRecord({language:e,namespace:n});if(f(a))return null==a?void 0:a.catch((e=>(console.error(e),{})));if(void 0!==a)return a}},getBackendDevRecord:({language:e,namespace:t})=>{var a;return null===(a=g.devBackend)||void 0===a?void 0:a.getRecord({apiKey:n().apiKey,apiUrl:n().apiUrl,language:e,namespace:t})},setLanguageDetector:e=>{g.languageDetector=e},setLanguageStorage:e=>{g.languageStorage=e},getInitialLanguage:()=>{var e;const n=t(),a=null===(e=g.languageStorage)||void 0===e?void 0:e.getLanguage();return v(a,(e=>n&&!n.includes(e)||!e?(()=>{if(!g.languageDetector)return;const e=t();return g.languageDetector.getLanguage({availableLanguages:e})})():e))},setStoredLanguage:e=>{var n;null===(n=g.languageStorage)||void 0===n||n.setLanguage(e)},run:()=>{var e;g.ui=r.ui&&new r.ui({apiKey:n().apiKey,apiUrl:n().apiUrl,highlight:l,changeTranslation:s}),null===(e=g.observer)||void 0===e||e.run({mouseHighlight:Boolean(g.ui)}),u()},stop:()=>{var e;g.ui=void 0,null===(e=g.observer)||void 0===e||e.stop()},retranslate:()=>{var e;null===(e=g.observer)||void 0===e||e.retranslate()},highlight:l,wrap:e=>{var n;return g.observer?null===(n=g.observer)||void 0===n?void 0:n.wrap(e):e.translation},unwrap:e=>{var n;return g.observer?null===(n=g.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}}})},b=(e,n,t)=>{let a=e;return Object.freeze({init:function(e){a=e},notify:function(){const e=n();a!==e&&t(e),a=e}})},h={enableLanguageStore:!0,defaultNs:"",filesUrlPrefix:"i18n/"},y=(e,n)=>{const t=Object.assign(Object.assign(Object.assign({},h),null==n?void 0:n.initialOptions),e),a=t.apiUrl;return t.apiUrl=a?a.replace(/\/+$/,""):a,{initialOptions:t,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}};function L(e){var{ns:n,noWrap:t,orEmpty:a,params:i}=e,o=function(e,n){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&n.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(a=Object.getOwnPropertySymbols(e);i<a.length;i++)n.indexOf(a[i])<0&&Object.prototype.propertyIsEnumerable.call(e,a[i])&&(t[a[i]]=e[a[i]])}return t}(e,["ns","noWrap","orEmpty","params"]);return{ns:n,noWrap:t,orEmpty:a,params:Object.assign(Object.assign({},o),i)}}const O=(e,...n)=>{let t,a={};return"object"==typeof e?a=e:(a.key=e,"string"==typeof n[0]?(a.defaultValue=n[0],t=n[1]):"object"==typeof n[0]&&(t=n[0])),t&&(a=Object.assign(Object.assign({},L(t)),a)),a},k=({events:e,options:n})=>{const t=b(!1,(()=>c.isFetching()),e.onFetchingChange.emit),s=b(!1,(()=>h()),e.onLoadingChange.emit),r=((e,n,t)=>{let s=y();function r(){return s.language||s.initialOptions.language}function g(){return s.initialOptions}return Object.freeze({init:function(e){s=y(e,s)},isRunning:function(){return s.isRunning},setRunning:function(e){s.isRunning!==e&&(s.isRunning=e,t.emit(e))},isInitialLoading:function(){return s.isInitialLoading},setInitialLoading:function(e){s.isInitialLoading=e},getLanguage:r,getLanguageOrFail:function(){const e=s.language||s.initialOptions.language;if(!e)throw new Error("No language set");return e},setLanguage:function(n){s.language!==n&&(s.language=n,e.emit(n))},getPendingLanguage:function(){return s.pendingLanguage||r()},setPendingLanguage:function(e){s.pendingLanguage!==e&&(s.pendingLanguage=e,n.emit(e))},getInitialOptions:g,addActiveNs:function(e){a(e).forEach((e=>{const n=s.activeNamespaces.get(e);void 0!==n?s.activeNamespaces.set(e,n+1):s.activeNamespaces.set(e,1)}))},removeActiveNs:function(e){a(e).forEach((e=>{const n=s.activeNamespaces.get(e);void 0!==n&&n>1?s.activeNamespaces.set(e,n-1):s.activeNamespaces.delete(e)}))},getRequiredNamespaces:function(){return o([...s.initialOptions.ns||[s.initialOptions.defaultNs],...s.activeNamespaces.keys()])},getFallbackLangs:function(e){const n=e||r();return n?o([n,...i(n,s.initialOptions.fallbackLanguage)]):[]},getFallbackNamespaces:function(){const e=s.initialOptions.defaultNs;return o([..."string"==typeof e?[e]:[],...a(s.initialOptions.fallbackNs)])},getAvailableLanguages:function(){if(s.initialOptions.availableLanguages)return s.initialOptions.availableLanguages;if(s.initialOptions.staticData){const e=Object.keys(s.initialOptions.staticData).map((e=>u(e).language));return Array.from(new Set(e))}},withDefaultNs:function(e){return{namespace:void 0===e.namespace?g().defaultNs:e.namespace,language:e.language}}})})(e.onLanguageChange,e.onPendingLanguageChange,e.onRunningChange),g=m(r.getLanguage,r.getInitialOptions,r.getAvailableLanguages,(function({key:e,ns:n}){const t=n?a(n):r.getFallbackNamespaces(),i=r.getFallbackLangs();return c.getTranslationNs(t,i,e)}),R,l),c=d(e.onCacheChange,g.getBackendRecord,g.getBackendDevRecord,r.withDefaultNs,r.isInitialLoading,t,s);r.init(n),c.addStaticData(r.getInitialOptions().staticData),L()&&c.invalidate(),e.onKeyUpdate.listen((()=>{r.isRunning()&&g.retranslate()}));function l(e,n,t){const a=r.withDefaultNs(e),i=c.getTranslation(a,n);return c.changeTranslation(a,n,t),{revert:()=>{c.changeTranslation(a,n,i)}}}function h(e){return c.isLoading(r.getLanguage(),e)}function L(){return Boolean(r.getInitialOptions().apiKey&&g.getDevBackend())}function k(e,n){const t=function(e,n){const t=r.getFallbackLangs(e),i=void 0!==n?a(n):r.getRequiredNamespaces(),o=[];return t.forEach((e=>{i.forEach((n=>{c.exists({language:e,namespace:n},!0)||o.push({language:e,namespace:n})}))})),o}(e,n);if(t.length)return v(E(t),(()=>{}))}function R({key:e,ns:n}){const t=n?a(n):r.getFallbackNamespaces(),i=r.getFallbackLangs();return c.getTranslationFallback(t,i,e)}function j(){const n=v(function(){if(r.getLanguage())return;if(!r.getInitialOptions().defaultLanguage)throw new Error(p("defaultLanguage"));const e=g.getInitialLanguage();return v(e,(e=>{const n=e||r.getInitialOptions().defaultLanguage;n&&r.setLanguage(n)}))}(),(()=>(r.getLanguageOrFail(),k())));if(f(n))return r.setInitialLoading(!0),t.notify(),s.notify(),Promise.resolve(n).then((()=>{r.setInitialLoading(!1),t.notify(),s.notify(),e.onInitialLoaded.emit()}));e.onInitialLoaded.emit()}function E(e){return c.loadRecords(e,L())}return Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign({},r),g),c),{init:function(e){r.init(e),c.addStaticData(r.getInitialOptions().staticData),L()&&c.invalidate()},changeLanguage:async function(e){r.getPendingLanguage()===e&&r.getLanguage()===e||(r.setPendingLanguage(e),r.isRunning()&&await k(e),e===r.getPendingLanguage()&&(r.setLanguage(e),g.setStoredLanguage(e)))},getTranslation:R,changeTranslation:l,addActiveNs:async function(e,n){n||r.addActiveNs(e),r.isRunning()&&await k(void 0,e)},loadRequiredRecords:k,loadRecords:E,loadRecord:async function(e){return(await E([e]))[0]},isLoading:h,isLoaded:function(e){const n=r.getLanguage();if(!n)return!1;const t=r.getFallbackLangs(n),i=void 0!==e?a(e):r.getRequiredNamespaces(),o=[];return t.forEach((e=>{i.forEach((n=>{c.exists({language:e,namespace:n})||o.push({language:e,namespace:n})}))})),0===o.length},t:(...e)=>{const n=O(...e),t=R(n);return g.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:L,run:function(){let e;return r.isRunning()||(r.setRunning(!0),g.run(),e=j()),Promise.resolve(e)},stop:function(){r.isRunning()&&(g.stop(),r.setRunning(!1))}}))};e.DEVTOOLS_ID="__tolgee_dev_tools",e.RESTRICTED_ASCENDANT_ATTRIBUTE="data-tolgee-restricted",e.TOLGEE_ATTRIBUTE_NAME="_tolgee",e.TOLGEE_HIGHLIGHTER_CLASS="_tolgee-highlighter",e.TOLGEE_WRAPPED_ONLY_DATA_ATTRIBUTE="data-tolgee-key-only",e.Tolgee=e=>{const t=(()=>{const e=n(),t=n(),a=n(),i=n(),o=n(),s=n(),r=g(),c=n(),u=n();return s.listen((()=>r.emit())),t.listen((()=>r.emit())),c.listen((({value:e})=>{r.emit({ns:[e.namespace],key:e.key},!0)})),Object.freeze({onPendingLanguageChange:e,onLanguageChange:t,onKeyChange:a,onKeyUpdate:r,onLoadingChange:i,onFetchingChange:o,onInitialLoaded:s,onRunningChange:u,onCacheChange:c,on:(n,a)=>{switch(n){case"pendingLanguage":return e.listen(a);case"language":return t.listen(a);case"loading":return i.listen(a);case"fetching":return o.listen(a);case"initialLoad":return s.listen(a);case"running":return u.listen(a);case"cache":return c.listen(a);case"keyUpdate":return r.listen(a)}}})})(),a=k({events:t,options:e}),i=Object.freeze({setFinalFormatter:a.setFinalFormatter,addFormatter:a.addFormatter,setObserver:a.setObserver,getObserver:a.getObserver,setUi:a.setUi,getUi:a.getUi,setDevBackend:a.setDevBackend,addBackend:a.addBackend,setLanguageDetector:a.setLanguageDetector,setLanguageStorage:a.setLanguageStorage}),o=e=>{const n=a.isRunning();n&&a.stop(),e(),n&&a.run()},s=Object.freeze({on:t.on,onKeyUpdate:t.onKeyUpdate.listenSome,getLanguage:a.getLanguage,getPendingLanguage:a.getPendingLanguage,changeLanguage:a.changeLanguage,changeTranslation:a.changeTranslation,addActiveNs:a.addActiveNs,removeActiveNs:a.removeActiveNs,loadRecords:a.loadRecords,loadRecord:a.loadRecord,addStaticData:a.addStaticData,getRecord:a.getRecord,getAllRecords:a.getAllRecords,isLoaded:a.isLoaded,isInitialLoading:a.isInitialLoading,isLoading:a.isLoading,isFetching:a.isFetching,isRunning:a.isRunning,run:a.run,stop:a.stop,t:a.t,highlight:a.highlight,getInitialOptions:a.getInitialOptions,isDev:a.isDev,wrap:a.wrap,unwrap:a.unwrap,use:e=>(e&&o((()=>e(s,i))),s),init:e=>(o((()=>a.init(e))),s)});return s},e.getFallback=t,e.getFallbackArray=a,e.getTranslateParams=O,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@tolgee/core"]={})}(this,(function(e){"use strict";const n=()=>{let e=[];return Object.freeze({listen:n=>{const t=e=>{n(e)};return e.push(t),{unsubscribe:()=>{e=e.filter((e=>t!==e))}}},emit:n=>{e.forEach((e=>e({value:n})))}})};function t(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function a(e){return t(e)||[]}function i(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?a(n):a(null==n?void 0:n[e]);var t}function o(e){return Array.from(new Set(e))}function s(e,n){const t=e.get(n)||0;e.set(n,t+1)}function r(e,n){let t=e.get(n)||1;t-=1,t<=0?e.delete(n):e.set(n,t)}const g=()=>{const e=new Set,n=new Set;let t=[];const i=()=>{if(0===t.length)return;e.forEach((e=>{e({value:void 0})}));let a=[],i=[];t.forEach((e=>{void 0===(null==e?void 0:e.ns)?a=void 0:void 0!==a&&(a=[...a,...e.ns]),void 0===(null==e?void 0:e.key)?i=void 0:void 0!==i&&(i=[...i,e.key])})),(i||[void 0]).forEach((e=>{((e,t)=>{n.forEach((n=>{const a=void 0!==t,i=n.namespaces.has(void 0)||-1!==(null==t?void 0:t.findIndex((e=>n.namespaces.has(e)))),o=void 0!==e,s=void 0===e||n.keys.has(e)||0===n.keys.size;a&&!i||o&&!s||n.fn({value:void 0})}))})(e,a)})),t=[]};return Object.freeze({listenSome:e=>{const t={fn:n=>{e(n)},keys:new Map,namespaces:new Map};n.add(t);const i={unsubscribe:()=>{n.delete(t)},subscribeNs:e=>(a(e).forEach((e=>s(t.namespaces,e))),i),unsubscribeNs:e=>(a(e).forEach((e=>r(t.namespaces,e))),i),subscribeKey:e=>{const{key:n,ns:o}=e;return s(t.keys,n),a(o).forEach((e=>s(t.namespaces,e))),void 0===o&&s(t.namespaces,void 0),i},unsubscribeKey:e=>{const{key:n,ns:o}=e;return r(t.keys,n),a(o).forEach((e=>r(t.namespaces,e))),void 0===o&&r(t.namespaces,void 0),i}};return i},listen:n=>{e.add(n);return{unsubscribe:()=>{e.delete(n)}}},emit:(e,n)=>{t.push(e),n?Promise.resolve().then((()=>{i()})):i()}})},c=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):Object.entries(c(t)).forEach((([t,a])=>{n.set(e+"."+t,a)})))})),n},u=e=>{const[n,t]=e.split(":");return{language:n,namespace:t||""}},l=({language:e,namespace:n})=>n?`${e}:${n}`:e,d=(e,n,t,i,o,s,r)=>{const g=new Map,d=new Map;let f={},v=0;function p(n,t,a){const i=l(n);d.set(i,{data:c(t),version:a}),e.emit(n)}function m(e,n){p(e,n,v)}function b(e,n=!1){const t=d.get(l(e));return t&&n?t.version===v:Boolean(t)}function h(e){var n;return null===(n=d.get(l(e)))||void 0===n?void 0:n.data}function y(e){let t;if(!t){const n=f[l(e)];"function"==typeof n?t=n():n&&(t=Promise.resolve(n))}return t||(t=n(e)),t||(t=Promise.resolve({})),t}return Object.freeze({addStaticData:function(e){e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=u(e),a=d.get(e);a&&0!==a.version||p(t,n,0)}})))},invalidate:function(){g.clear(),v+=1},addRecord:m,exists:b,getRecord:h,getTranslation:function(e,n){var t;return null===(t=d.get(l(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs:function(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=d.get(l({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return i}return Array.from(new Set(e))},getTranslationFallback:function(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=d.get(l({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation:function(n,t,a){var i;const o=null===(i=d.get(l(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching:function(e){if(o())return!0;if(void 0===e)return g.size>0;const n=a(e);return Boolean(Array.from(g.keys()).find((e=>n.includes(u(e).namespace))))},isLoading:function(e,n){const t=a(n);return Boolean(o()||Array.from(g.keys()).find((n=>{const a=u(n);return(!t.length||t.includes(a.namespace))&&!b({namespace:a.namespace,language:e})})))},loadRecords:async function(e,n){const a=e.map((e=>{const a=i(e),o=l(a),s=g.get(o);if(s)return{new:!1,promise:s,keyObject:a,cacheKey:o};const r=function(e,n){var a;let i;return n&&(i=null===(a=t(e))||void 0===a?void 0:a.catch((()=>(console.warn("Tolgee: Failed to fetch data from dev backend"),y(e))))),i||(i=y(e)),i}(a,n);return g.set(o,r),{new:!0,promise:r,keyObject:a,cacheKey:o}}));s.notify(),r.notify();const o=await Promise.all(a.map((e=>e.promise)));return a.forEach(((e,n)=>{const t=g.get(e.cacheKey)!==e.promise;if(e.new&&!t){g.delete(e.cacheKey);const t=o[n];t&&m(e.keyObject,t)}})),s.notify(),r.notify(),a.map((e=>h(e.keyObject)))},clear:function(){d.clear()},getAllRecords:function(){return Array.from(d.entries()).map((([e,n])=>Object.assign(Object.assign({},u(e)),{data:n.data})))}})};function f(e){return Boolean(e&&"function"==typeof e.then)}const v=(e,n)=>f(e)?Promise.resolve(e).then(n):n(e),p=e=>`Tolgee: You need to specify '${e}' option`,m=(e,n,t,i,o,s)=>{const r={ui:void 0},g={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},c=async(e,{keysAndDefaults:n})=>{var t;const s=n.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,ns:a(i({key:e,ns:n,defaultValue:t})),translation:o({key:e,ns:n})})));null===(t=g.ui)||void 0===t||t.handleElementClick(e,s)},u=()=>{if(g.languageDetector){if(!t())throw new Error(p("availableLanguages"))}},l=(e,n)=>{var t,a;return(null===(a=null===(t=g.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},d=e=>{const n=o(e);return m(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))},m=({key:n,translation:t,defaultValue:a,noWrap:i,params:o,orEmpty:s,ns:r,formatEnabled:c})=>{var u;const l=t||a;let d=l||(s?"":n);g.observer&&!i&&(d=g.observer.wrap({key:n,translation:d,defaultValue:a,params:o,ns:r}));const f=e(),v=c||!(null===(u=g.observer)||void 0===u?void 0:u.outputNotFormattable);if(l&&f&&v)for(const e of g.formatters)d=e.format({translation:d,language:f,params:o});return g.finalFormatter&&l&&f&&v&&(d=g.finalFormatter.format({translation:d,language:f,params:o})),d};return Object.freeze({setFinalFormatter:e=>{g.finalFormatter=e},addFormatter:e=>{e&&g.formatters.push(e)},formatTranslation:m,setObserver:e=>{g.observer=null==e?void 0:e({translate:d,onClick:c})},getObserver:()=>g.observer,setUi:e=>{r.ui=(null==e?void 0:e.UI)||e},getUi:()=>r.ui,addBackend:e=>{e&&g.backends.push(e)},setDevBackend:e=>{g.devBackend=e},getDevBackend:()=>g.devBackend,getBackendRecord:({language:e,namespace:n})=>{for(const t of g.backends){const a=t.getRecord({language:e,namespace:n});if(f(a))return null==a?void 0:a.catch((e=>(console.error(e),{})));if(void 0!==a)return a}},getBackendDevRecord:({language:e,namespace:t})=>{var a;return null===(a=g.devBackend)||void 0===a?void 0:a.getRecord({apiKey:n().apiKey,apiUrl:n().apiUrl,language:e,namespace:t})},setLanguageDetector:e=>{g.languageDetector=e},setLanguageStorage:e=>{g.languageStorage=e},getInitialLanguage:()=>{var e;const n=t(),a=null===(e=g.languageStorage)||void 0===e?void 0:e.getLanguage();return v(a,(e=>n&&!n.includes(e)||!e?(()=>{if(!g.languageDetector)return;const e=t();return g.languageDetector.getLanguage({availableLanguages:e})})():e))},setStoredLanguage:e=>{var n;null===(n=g.languageStorage)||void 0===n||n.setLanguage(e)},run:()=>{var e;g.ui=r.ui&&new r.ui({apiKey:n().apiKey,apiUrl:n().apiUrl,highlight:l,changeTranslation:s}),null===(e=g.observer)||void 0===e||e.run({mouseHighlight:Boolean(g.ui)}),u()},stop:()=>{var e;g.ui=void 0,null===(e=g.observer)||void 0===e||e.stop()},retranslate:()=>{var e;null===(e=g.observer)||void 0===e||e.retranslate()},highlight:l,wrap:e=>{var n;return g.observer?null===(n=g.observer)||void 0===n?void 0:n.wrap(e):e.translation},unwrap:e=>{var n;return g.observer?null===(n=g.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}}})},b=(e,n,t)=>{let a=e;return Object.freeze({init:function(e){a=e},notify:function(){const e=n();a!==e&&t(e),a=e}})},h={enableLanguageStore:!0,defaultNs:"",filesUrlPrefix:"i18n/"},y=(e,n)=>{const t=Object.assign(Object.assign(Object.assign({},h),null==n?void 0:n.initialOptions),e),a=t.apiUrl;return t.apiUrl=a?a.replace(/\/+$/,""):a,{initialOptions:t,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}};function L(e){var{ns:n,noWrap:t,orEmpty:a,params:i}=e,o=function(e,n){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&n.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(a=Object.getOwnPropertySymbols(e);i<a.length;i++)n.indexOf(a[i])<0&&Object.prototype.propertyIsEnumerable.call(e,a[i])&&(t[a[i]]=e[a[i]])}return t}(e,["ns","noWrap","orEmpty","params"]);return{ns:n,noWrap:t,orEmpty:a,params:Object.assign(Object.assign({},o),i)}}const O=(e,...n)=>{let t,a={};return"object"==typeof e?a=e:(a.key=e,"string"==typeof n[0]?(a.defaultValue=n[0],t=n[1]):"object"==typeof n[0]&&(t=n[0])),t&&(a=Object.assign(Object.assign({},L(t)),a)),a},k=({events:e,options:n})=>{const t=b(!1,(()=>c.isFetching()),e.onFetchingChange.emit),s=b(!1,(()=>h()),e.onLoadingChange.emit),r=((e,n,t)=>{let s=y();function r(){return s.language||s.initialOptions.language}function g(){return s.initialOptions}return Object.freeze({init:function(e){s=y(e,s)},isRunning:function(){return s.isRunning},setRunning:function(e){s.isRunning!==e&&(s.isRunning=e,t.emit(e))},isInitialLoading:function(){return s.isInitialLoading},setInitialLoading:function(e){s.isInitialLoading=e},getLanguage:r,getLanguageOrFail:function(){const e=s.language||s.initialOptions.language;if(!e)throw new Error("No language set");return e},setLanguage:function(n){s.language!==n&&(s.language=n,e.emit(n))},getPendingLanguage:function(){return s.pendingLanguage||r()},setPendingLanguage:function(e){s.pendingLanguage!==e&&(s.pendingLanguage=e,n.emit(e))},getInitialOptions:g,addActiveNs:function(e){a(e).forEach((e=>{const n=s.activeNamespaces.get(e);void 0!==n?s.activeNamespaces.set(e,n+1):s.activeNamespaces.set(e,1)}))},removeActiveNs:function(e){a(e).forEach((e=>{const n=s.activeNamespaces.get(e);void 0!==n&&n>1?s.activeNamespaces.set(e,n-1):s.activeNamespaces.delete(e)}))},getRequiredNamespaces:function(){return o([...s.initialOptions.ns||[s.initialOptions.defaultNs],...s.activeNamespaces.keys()])},getFallbackLangs:function(e){const n=e||r();return n?o([n,...i(n,s.initialOptions.fallbackLanguage)]):[]},getFallbackNamespaces:function(){const e=s.initialOptions.defaultNs;return o([..."string"==typeof e?[e]:[],...a(s.initialOptions.fallbackNs)])},getAvailableLanguages:function(){if(s.initialOptions.availableLanguages)return s.initialOptions.availableLanguages;if(s.initialOptions.staticData){const e=Object.keys(s.initialOptions.staticData).map((e=>u(e).language));return Array.from(new Set(e))}},withDefaultNs:function(e){return{namespace:void 0===e.namespace?g().defaultNs:e.namespace,language:e.language}}})})(e.onLanguageChange,e.onPendingLanguageChange,e.onRunningChange),g=m(r.getLanguage,r.getInitialOptions,r.getAvailableLanguages,(function({key:e,ns:n}){const t=n?a(n):r.getFallbackNamespaces(),i=r.getFallbackLangs();return c.getTranslationNs(t,i,e)}),R,l),c=d(e.onCacheChange,g.getBackendRecord,g.getBackendDevRecord,r.withDefaultNs,r.isInitialLoading,t,s);r.init(n),c.addStaticData(r.getInitialOptions().staticData),L()&&c.invalidate(),e.onKeyUpdate.listen((()=>{r.isRunning()&&g.retranslate()}));function l(e,n,t){const a=r.withDefaultNs(e),i=c.getTranslation(a,n);return c.changeTranslation(a,n,t),{revert:()=>{c.changeTranslation(a,n,i)}}}function h(e){return c.isLoading(r.getLanguage(),e)}function L(){return Boolean(r.getInitialOptions().apiKey&&g.getDevBackend())}function k(e,n){const t=function(e,n){const t=r.getFallbackLangs(e),i=void 0!==n?a(n):r.getRequiredNamespaces(),o=[];return t.forEach((e=>{i.forEach((n=>{c.exists({language:e,namespace:n},!0)||o.push({language:e,namespace:n})}))})),o}(e,n);if(t.length)return v(E(t),(()=>{}))}function R({key:e,ns:n}){const t=n?a(n):r.getFallbackNamespaces(),i=r.getFallbackLangs();return c.getTranslationFallback(t,i,e)}function j(){const n=v(function(){if(r.getLanguage())return;if(!r.getInitialOptions().defaultLanguage)throw new Error(p("defaultLanguage"));const e=g.getInitialLanguage();return v(e,(e=>{const n=e||r.getInitialOptions().defaultLanguage;n&&r.setLanguage(n)}))}(),(()=>(r.getLanguageOrFail(),k())));if(f(n))return r.setInitialLoading(!0),t.notify(),s.notify(),Promise.resolve(n).then((()=>{r.setInitialLoading(!1),t.notify(),s.notify(),e.onInitialLoaded.emit()}));e.onInitialLoaded.emit()}function E(e){return c.loadRecords(e,L())}return Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign({},r),g),c),{init:function(e){r.init(e),c.addStaticData(r.getInitialOptions().staticData)},changeLanguage:async function(e){r.getPendingLanguage()===e&&r.getLanguage()===e||(r.setPendingLanguage(e),r.isRunning()&&await k(e),e===r.getPendingLanguage()&&(r.setLanguage(e),g.setStoredLanguage(e)))},getTranslation:R,changeTranslation:l,addActiveNs:async function(e,n){n||r.addActiveNs(e),r.isRunning()&&await k(void 0,e)},loadRequiredRecords:k,loadRecords:E,loadRecord:async function(e){return(await E([e]))[0]},isLoading:h,isLoaded:function(e){const n=r.getLanguage();if(!n)return!1;const t=r.getFallbackLangs(n),i=void 0!==e?a(e):r.getRequiredNamespaces(),o=[];return t.forEach((e=>{i.forEach((n=>{c.exists({language:e,namespace:n})||o.push({language:e,namespace:n})}))})),0===o.length},t:(...e)=>{const n=O(...e),t=R(n);return g.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:L,run:function(){let e;return r.isRunning()||(L()&&c.invalidate(),r.setRunning(!0),g.run(),e=j()),Promise.resolve(e)},stop:function(){r.isRunning()&&(g.stop(),r.setRunning(!1))}}))};e.DEVTOOLS_ID="__tolgee_dev_tools",e.RESTRICTED_ASCENDANT_ATTRIBUTE="data-tolgee-restricted",e.TOLGEE_ATTRIBUTE_NAME="_tolgee",e.TOLGEE_HIGHLIGHTER_CLASS="_tolgee-highlighter",e.TOLGEE_WRAPPED_ONLY_DATA_ATTRIBUTE="data-tolgee-key-only",e.Tolgee=e=>{const t=(()=>{const e=n(),t=n(),a=n(),i=n(),o=n(),s=n(),r=g(),c=n(),u=n();return s.listen((()=>r.emit())),t.listen((()=>r.emit())),c.listen((({value:e})=>{r.emit({ns:[e.namespace],key:e.key},!0)})),Object.freeze({onPendingLanguageChange:e,onLanguageChange:t,onKeyChange:a,onKeyUpdate:r,onLoadingChange:i,onFetchingChange:o,onInitialLoaded:s,onRunningChange:u,onCacheChange:c,on:(n,a)=>{switch(n){case"pendingLanguage":return e.listen(a);case"language":return t.listen(a);case"loading":return i.listen(a);case"fetching":return o.listen(a);case"initialLoad":return s.listen(a);case"running":return u.listen(a);case"cache":return c.listen(a);case"keyUpdate":return r.listen(a)}}})})(),a=k({events:t,options:e}),i=Object.freeze({setFinalFormatter:a.setFinalFormatter,addFormatter:a.addFormatter,setObserver:a.setObserver,getObserver:a.getObserver,setUi:a.setUi,getUi:a.getUi,setDevBackend:a.setDevBackend,addBackend:a.addBackend,setLanguageDetector:a.setLanguageDetector,setLanguageStorage:a.setLanguageStorage}),o=e=>{const n=a.isRunning();n&&a.stop(),e(),n&&a.run()},s=Object.freeze({on:t.on,onKeyUpdate:t.onKeyUpdate.listenSome,getLanguage:a.getLanguage,getPendingLanguage:a.getPendingLanguage,changeLanguage:a.changeLanguage,changeTranslation:a.changeTranslation,addActiveNs:a.addActiveNs,removeActiveNs:a.removeActiveNs,loadRecords:a.loadRecords,loadRecord:a.loadRecord,addStaticData:a.addStaticData,getRecord:a.getRecord,getAllRecords:a.getAllRecords,isLoaded:a.isLoaded,isInitialLoading:a.isInitialLoading,isLoading:a.isLoading,isFetching:a.isFetching,isRunning:a.isRunning,run:a.run,stop:a.stop,t:a.t,highlight:a.highlight,getInitialOptions:a.getInitialOptions,isDev:a.isDev,wrap:a.wrap,unwrap:a.unwrap,use:e=>(e&&o((()=>e(s,i))),s),init:e=>(o((()=>a.init(e))),s)});return s},e.getFallback=t,e.getFallbackArray=a,e.getTranslateParams=O,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=tolgee.umd.min.js.map |
{ | ||
"name": "@tolgee/core", | ||
"version": "5.0.0-rc.4aa3be8.0", | ||
"version": "5.0.0-rc.9be0f0e.0", | ||
"description": "Library providing ability to translate messages directly in context of developed application.", | ||
@@ -61,3 +61,6 @@ "main": "./dist/tolgee.cjs.js", | ||
}, | ||
"gitHead": "2d0148af0a6398057122d53edcf2ad2a5b90163a" | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "f2d1821b4e9c6af080a331a8a3378e5ad6488f92" | ||
} |
@@ -57,2 +57,3 @@ import { | ||
function invalidate() { | ||
asyncRequests.clear(); | ||
version += 1; | ||
@@ -64,3 +65,3 @@ } | ||
data: TreeTranslationsData, | ||
version: number | ||
recordVersion: number | ||
) { | ||
@@ -70,3 +71,3 @@ const cacheKey = encodeCacheKey(descriptor); | ||
data: flattenTranslations(data), | ||
version: version, | ||
version: recordVersion, | ||
}); | ||
@@ -256,3 +257,7 @@ onCacheChange.emit(descriptor); | ||
withPromises.forEach((value, i) => { | ||
if (value.new) { | ||
const promiseChanged = | ||
asyncRequests.get(value.cacheKey) !== value.promise; | ||
// if promise has changed in between, it means cache been invalidated or | ||
// new data are being fetched | ||
if (value.new && !promiseChanged) { | ||
asyncRequests.delete(value.cacheKey); | ||
@@ -259,0 +264,0 @@ const data = results[i]; |
@@ -96,5 +96,2 @@ import type { EventServiceType } from '../Events/Events'; | ||
cache.addStaticData(state.getInitialOptions().staticData); | ||
if (isDev()) { | ||
cache.invalidate(); | ||
} | ||
} | ||
@@ -255,2 +252,5 @@ | ||
if (!state.isRunning()) { | ||
if (isDev()) { | ||
cache.invalidate(); | ||
} | ||
state.setRunning(true); | ||
@@ -257,0 +257,0 @@ pluginService.run(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
567830
7062
0