@tolgee/core
Advanced tools
Comparing version 5.10.0 to 5.10.1
@@ -1127,5 +1127,2 @@ 'use strict'; | ||
if (!state.isRunning()) { | ||
if (self.isDev()) { | ||
cache.invalidate(); | ||
} | ||
state.setRunning(true); | ||
@@ -1150,2 +1147,6 @@ pluginService.run(); | ||
}); | ||
if (controller.isDev()) { | ||
// override existing data in DevMode | ||
controller.invalidate(); | ||
} | ||
// restarts tolgee while applying callback | ||
@@ -1156,2 +1157,4 @@ function withRestart(callback) { | ||
callback(); | ||
// invalidate cache when tolgee configuration is updated/plugin added in DevMode | ||
controller.isDev() && controller.invalidate(); | ||
wasRunning && controller.run(); | ||
@@ -1158,0 +1161,0 @@ } |
@@ -1127,5 +1127,2 @@ 'use strict'; | ||
if (!state.isRunning()) { | ||
if (self.isDev()) { | ||
cache.invalidate(); | ||
} | ||
state.setRunning(true); | ||
@@ -1150,2 +1147,6 @@ pluginService.run(); | ||
}); | ||
if (controller.isDev()) { | ||
// override existing data in DevMode | ||
controller.invalidate(); | ||
} | ||
// restarts tolgee while applying callback | ||
@@ -1156,2 +1157,4 @@ function withRestart(callback) { | ||
callback(); | ||
// invalidate cache when tolgee configuration is updated/plugin added in DevMode | ||
controller.isDev() && controller.invalidate(); | ||
wasRunning && controller.run(); | ||
@@ -1158,0 +1161,0 @@ } |
@@ -1,2 +0,2 @@ | ||
"use strict";function e(e){return Boolean(e&&"function"==typeof e.then)}function n(n,t){return e(n)?Promise.resolve(n).then(t):t(n)}function t(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function a(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function i(e){return a(e)||[]}function o(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?i(n):i(null==n?void 0:n[e]);var t}function r(e){return Array.from(new Set(e))}function s(e){return e?e.replace(/\/+$/,""):e}function g(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function c(e,n,t){const a=new Set,o=new Set;let r=[];function s(){if(0===r.length)return;const e=r;r=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());o.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(r.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};o.add(n);const a={unsubscribe:()=>{o.delete(n)},subscribeNs:e=>(i(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}Object.defineProperty(exports,"__esModule",{value:!0});const l=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):l(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},u=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},d=({language:e,namespace:n})=>n?`${e}:${n}`:e;function p(e,n,t,a,o,s,g){const c=new Map,p=new Map;let f={},v=0;function h(n,t,a){const i=d(n);p.set(i,{data:l(t),version:a}),e.emit(n)}function b(e){let t;if(!t){const n=f[d(e)];"function"==typeof n&&(t=n())}return t||(t=n(e)),t}const m=Object.freeze({addStaticData(e){e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=u(e),a=p.get(e);a&&0!==a.version||h(t,n,0)}})))},invalidate(){c.clear(),v+=1},addRecord(e,n){h(e,n,v)},exists(e,n=!1){const t=p.get(d(e));return t&&n?t.version===v:Boolean(t)},getRecord(e){var n;return null===(n=p.get(d(a(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=p.get(d(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=p.get(d({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return r(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=p.get(d({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(n,t,a){var i;const o=null===(i=p.get(d(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching(e){if(o())return!0;if(void 0===e)return c.size>0;const n=i(e);return Boolean(Array.from(c.keys()).find((e=>n.includes(u(e).namespace))))},isLoading(e,n){const t=i(n);return Boolean(o()||Array.from(c.keys()).find((n=>{const a=u(n);return(!t.length||t.includes(a.namespace))&&!m.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const i=e.map((e=>{const i=a(e),o=d(i),r=c.get(o);if(r)return{new:!1,promise:r,keyObject:i,cacheKey:o};const s=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"),b(e))))),i||(i=b(e)),i}(i,n)||Promise.resolve(void 0);return c.set(o,s),{new:!0,promise:s,keyObject:i,cacheKey:o}}));s.notify(),g.notify();const o=await Promise.all(i.map((e=>e.promise)));return i.forEach(((e,n)=>{const t=c.get(e.cacheKey)!==e.promise;if(e.new&&!t){c.delete(e.cacheKey);const t=o[n];t?m.addRecord(e.keyObject,t):m.getRecord(e.keyObject)||m.addRecord(e.keyObject,{})}})),s.notify(),g.notify(),i.map((e=>m.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(p.entries()).map((([e,n])=>Object.assign(Object.assign({},u(e)),{data:n.data})))});return m}function f(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}const v="invalid",h={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"]},observerType:"invisible",onFormatError:v,apiUrl:"https://app.tolgee.io"},b=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function m(e,n){const t=b(h,null==n?void 0:n.initialOptions,e);return t.apiUrl=s(t.apiUrl),{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(t,a,i,o,r,s){const g={ui:void 0},c={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},l=async({keysAndDefaults:e,event:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,ns:o({key:e,ns:n}),translation:r({key:e,ns:n})})));null===(t=c.ui)||void 0===t||t.handleElementClick(a,n)},u=(e,n)=>{var t;return(null===(t=c.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function d(e){const n=r({key:e.key,ns:e.ns});return w.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function p(e){c.observer=null==e?void 0:e()}function h(){return Boolean(c.observer)}function b(e){e&&c.formatters.push(e)}function m(e){c.finalFormatter=e}function y(e){g.ui=e}function L(){return Boolean(g.ui)}function O(e){c.languageStorage=e}function j(e){c.languageDetector=e}function k(e){e&&c.backends.push(e)}function R(e){c.devBackend=e}const w=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:m,addFormatter:b,setObserver:p,hasObserver:h,setUi:y,hasUi:L,setDevBackend:R,addBackend:k,setLanguageDetector:j,setLanguageStorage:O}))},run(){var e,n;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r}=a();c.ui=null===(e=g.ui)||void 0===e?void 0:e.call(g,{apiKey:t,apiUrl:i,projectId:o,highlight:w.highlight,changeTranslation:s,findPositions:u}),null===(n=c.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:d,onClick:l})},stop(){var e;c.ui=void 0,null===(e=c.observer)||void 0===e||e.stop()},getLanguageStorage:()=>c.languageStorage,getInitialLanguage(){var e;const t=i();return n(null===(e=c.languageStorage)||void 0===e?void 0:e.getLanguage(),(e=>t&&!t.includes(e)||!e?function(){if(!c.languageDetector)return;const e=i();return c.languageDetector.getLanguage({availableLanguages:e})}():e))},setStoredLanguage(e){var n;null===(n=c.languageStorage)||void 0===n||n.setLanguage(e)},getDevBackend:()=>c.devBackend,getBackendRecord:({language:n,namespace:t})=>{for(const a of c.backends){const i=a.getRecord({language:n,namespace:t});if(e(i))return null==i?void 0:i.catch((e=>(console.error(e),{})));if(void 0!==i)return i}},getBackendDevRecord:({language:e,namespace:n})=>{var t;const{apiKey:i,apiUrl:o,projectId:r}=a();return null===(t=c.devBackend)||void 0===t?void 0:t.getRecord({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n})},getLanguageDetector:()=>c.languageDetector,retranslate(){var e;null===(e=c.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=c.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(w.getDevBackend()),formatTranslation(e){var n,{formatEnabled:i}=e,o=f(e,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:l,params:u,orEmpty:d,ns:p}=o,h=s||g;let b=h||(d?"":r);const m=t(),y=i||!(null===(n=c.observer)||void 0===n?void 0:n.outputNotFormattable),L=e=>c.observer&&!l?c.observer.wrap({key:r,translation:e,defaultValue:g,params:u,ns:p}):e;b=L(b);try{if(h&&m&&y)for(const e of c.formatters)b=e.format({translation:b,language:m,params:u});c.finalFormatter&&h&&m&&y&&(b=c.finalFormatter.format({translation:b,language:m,params:u}))}catch(e){console.error(e);const n=("string"==typeof(O=e)?O:"string"==typeof(null==O?void 0:O.message)?O.message:void 0)||v,t=a().onFormatError,i=typeof t;b="string"===i?t:"function"===i?t(n,o):v,b=L(b)}var O;return b}});return w}const L=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};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({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=f(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function j({options:a}){const l=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:g(a),onLanguageChange:g(a),onLoadingChange:g(a),onFetchingChange:g(a),onInitialLoaded:g(a),onRunningChange:g(a),onCacheChange:g(a),onUpdate:c(a,e,n),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(j,k),d=L(!1,(()=>b.isFetching()),l.onFetchingChange.emit),f=L(!1,(()=>F.isLoading()),l.onLoadingChange.emit),v=function(e,n,t){let a,g=m();const c=Object.freeze({init(e){g=m(e,g)},isRunning:()=>g.isRunning,setRunning(e){g.isRunning!==e&&(g.isRunning=e,t.emit(e))},isInitialLoading:()=>g.isInitialLoading,setInitialLoading(e){g.isInitialLoading=e},getLanguage:()=>g.language||g.initialOptions.language,setLanguage(n){g.language!==n&&(g.language=n,e.emit(n))},getPendingLanguage:()=>g.pendingLanguage||c.getLanguage(),setPendingLanguage(e){g.pendingLanguage!==e&&(g.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},g.initialOptions),a),addActiveNs(e){i(e).forEach((e=>{const n=g.activeNamespaces.get(e);void 0!==n?g.activeNamespaces.set(e,n+1):g.activeNamespaces.set(e,1)}))},removeActiveNs(e){i(e).forEach((e=>{const n=g.activeNamespaces.get(e);void 0!==n&&n>1?g.activeNamespaces.set(e,n-1):g.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>r([...g.initialOptions.ns||[g.initialOptions.defaultNs],...i(g.initialOptions.fallbackNs),...g.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||c.getLanguage();return n?r([n,...o(n,g.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>i(g.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?g.initialOptions.defaultNs:e,getAvailableLanguages(){if(g.initialOptions.availableLanguages)return g.initialOptions.availableLanguages;if(g.initialOptions.staticData){const e=Object.keys(g.initialOptions.staticData).map((e=>u(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?c.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:s(e.apiUrl)}):void 0}});return c}(l.onLanguageChange,l.onPendingLanguageChange,l.onRunningChange),h=y(v.getLanguage,v.getInitialOptions,v.getAvailableLanguages,(function({key:e,ns:n}){const t=v.getFallbackLangs(),a=R(n||void 0);return b.getTranslationNs(a,t,e)}),D,E),b=p(l.onCacheChange,h.getBackendRecord,h.getBackendDevRecord,v.withDefaultNs,v.isInitialLoading,d,f);function j(){return v.getFallbackNs()}function k(e){return v.getDefaultNs(e)}function R(e){return[...i(k(e)),...j()]}function w(e){return[...i(e||k()),...v.getRequiredNamespaces()]}function E(e,n,t){const a=v.withDefaultNs(e),i=b.getTranslation(a,n);return b.changeTranslation(a,n,t),{revert(){b.changeTranslation(a,n,i)}}}function N(e){v.init(e),b.addStaticData(v.getInitialOptions().staticData)}function A(e,t){const a=function(e,n){const t=v.getFallbackLangs(e),a=w(n),i=[];return t.forEach((e=>{a.forEach((n=>{b.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}(e,t);if(a.length)return n(F.loadRecords(a),(()=>{}))}function D({key:e,ns:n,language:t}){const a=R(n||void 0),i=v.getFallbackLangs(t);return b.getTranslationFallback(a,i,e)}function I(){const t=n(function(){if(v.getLanguage())return;return n(h.getInitialLanguage(),(e=>{const n=e||v.getInitialOptions().defaultLanguage;n&&v.setLanguage(n)}))}(),(()=>A()));if(e(t))return v.setInitialLoading(!0),d.notify(),f.notify(),Promise.resolve(t).then((()=>{v.setInitialLoading(!1),d.notify(),f.notify(),l.onInitialLoaded.emit()}));l.onInitialLoaded.emit()}a&&N(a),l.onUpdate.listen((()=>{v.isRunning()&&h.retranslate()}));const F=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},l),v),h),b),{init:N,getTranslation:D,changeTranslation:E,async changeLanguage(e){v.getPendingLanguage()===e&&v.getLanguage()===e||(v.setPendingLanguage(e),v.isRunning()&&await A(e),e===v.getPendingLanguage()&&(v.setLanguage(e),h.setStoredLanguage(e)))},async addActiveNs(e,n){n||v.addActiveNs(e),v.isRunning()&&await A(void 0,e)},loadRecords:e=>b.loadRecords(e,F.isDev()),loadRecord:async e=>(await F.loadRecords([e]))[0],isLoading:e=>b.isLoading(v.getLanguage(),e),isLoaded(e){const n=v.getLanguage();if(!n)return!1;const t=v.getFallbackLangs(n),a=w(e),i=[];return t.forEach((e=>{a.forEach((n=>{b.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=O(...e),t=D(n);return h.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(v.getInitialOptions().apiKey&&v.getInitialOptions().apiUrl),run(){let e;return function(){if((h.getLanguageDetector()||h.getLanguageStorage())&&!v.getAvailableLanguages())throw new Error(t("availableLanguages"));if(!v.getLanguage()&&!v.getInitialOptions().defaultLanguage)throw new Error(t(["defaultLanguage","language"]))}(),v.isRunning()||(F.isDev()&&b.invalidate(),v.setRunning(!0),h.run(),e=I()),Promise.resolve(e)},stop(){v.isRunning()&&(h.stop(),v.setRunning(!1))}}));return F}const k=0,R=1,w=2;class E extends Error{constructor(e,n,t){let a;a=e===k?"Empty parameter":e===R?"Unexpected character":"Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`),this.code=e,this.index=n}}function N(e){return/\s/.test(e)}const A=0,D=1,I=2,F=3,C=4,S=new Set([I,D,A]),P="'",T=new Set(["{","}",P]),U=e=>/[0-9a-zA-Z_]/.test(e);function x(e,n){const[t,a]=function(e){let n=A,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new E(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(k),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case A:i===P?(t+=i,n=D):"{"===i?(c(),n=F):(t+=i,n=A);break;case D:T.has(i)?(t=t.slice(0,-1)+i,n=I):(t+=i,n=A);break;case I:i===P?n=A:(t+=i,n=I);break;case F:"}"===i?(l(),n=A):N(i)?""!==a&&(l(),n=C):(U(i)||g(R),a+=i,n=F);break;case C:"}"==i?n=A:N(i)?n=C:g(R)}return S.has(n)||g(w),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}exports.FormatSimple=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>x(e,n)}),e),exports.TolgeeCore=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=b(e.options,t),n),init(n){const t=function(e){const n=j({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),t&&n.run()}const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(b(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},exports.getFallback=a,exports.getFallbackArray=i,exports.getTranslateProps=O; | ||
"use strict";function e(e){return Boolean(e&&"function"==typeof e.then)}function n(n,t){return e(n)?Promise.resolve(n).then(t):t(n)}function t(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function a(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function i(e){return a(e)||[]}function o(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?i(n):i(null==n?void 0:n[e]);var t}function r(e){return Array.from(new Set(e))}function s(e){return e?e.replace(/\/+$/,""):e}function g(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function c(e,n,t){const a=new Set,o=new Set;let r=[];function s(){if(0===r.length)return;const e=r;r=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());o.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(r.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};o.add(n);const a={unsubscribe:()=>{o.delete(n)},subscribeNs:e=>(i(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}Object.defineProperty(exports,"__esModule",{value:!0});const l=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):l(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},u=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},d=({language:e,namespace:n})=>n?`${e}:${n}`:e;function p(e,n,t,a,o,s,g){const c=new Map,p=new Map;let f={},v=0;function h(n,t,a){const i=d(n);p.set(i,{data:l(t),version:a}),e.emit(n)}function b(e){let t;if(!t){const n=f[d(e)];"function"==typeof n&&(t=n())}return t||(t=n(e)),t}const m=Object.freeze({addStaticData(e){e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=u(e),a=p.get(e);a&&0!==a.version||h(t,n,0)}})))},invalidate(){c.clear(),v+=1},addRecord(e,n){h(e,n,v)},exists(e,n=!1){const t=p.get(d(e));return t&&n?t.version===v:Boolean(t)},getRecord(e){var n;return null===(n=p.get(d(a(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=p.get(d(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=p.get(d({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return r(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=p.get(d({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(n,t,a){var i;const o=null===(i=p.get(d(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching(e){if(o())return!0;if(void 0===e)return c.size>0;const n=i(e);return Boolean(Array.from(c.keys()).find((e=>n.includes(u(e).namespace))))},isLoading(e,n){const t=i(n);return Boolean(o()||Array.from(c.keys()).find((n=>{const a=u(n);return(!t.length||t.includes(a.namespace))&&!m.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const i=e.map((e=>{const i=a(e),o=d(i),r=c.get(o);if(r)return{new:!1,promise:r,keyObject:i,cacheKey:o};const s=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"),b(e))))),i||(i=b(e)),i}(i,n)||Promise.resolve(void 0);return c.set(o,s),{new:!0,promise:s,keyObject:i,cacheKey:o}}));s.notify(),g.notify();const o=await Promise.all(i.map((e=>e.promise)));return i.forEach(((e,n)=>{const t=c.get(e.cacheKey)!==e.promise;if(e.new&&!t){c.delete(e.cacheKey);const t=o[n];t?m.addRecord(e.keyObject,t):m.getRecord(e.keyObject)||m.addRecord(e.keyObject,{})}})),s.notify(),g.notify(),i.map((e=>m.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(p.entries()).map((([e,n])=>Object.assign(Object.assign({},u(e)),{data:n.data})))});return m}function f(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}const v="invalid",h={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"]},observerType:"invisible",onFormatError:v,apiUrl:"https://app.tolgee.io"},b=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function m(e,n){const t=b(h,null==n?void 0:n.initialOptions,e);return t.apiUrl=s(t.apiUrl),{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(t,a,i,o,r,s){const g={ui:void 0},c={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},l=async({keysAndDefaults:e,event:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,ns:o({key:e,ns:n}),translation:r({key:e,ns:n})})));null===(t=c.ui)||void 0===t||t.handleElementClick(a,n)},u=(e,n)=>{var t;return(null===(t=c.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function d(e){const n=r({key:e.key,ns:e.ns});return w.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function p(e){c.observer=null==e?void 0:e()}function h(){return Boolean(c.observer)}function b(e){e&&c.formatters.push(e)}function m(e){c.finalFormatter=e}function y(e){g.ui=e}function L(){return Boolean(g.ui)}function O(e){c.languageStorage=e}function j(e){c.languageDetector=e}function k(e){e&&c.backends.push(e)}function R(e){c.devBackend=e}const w=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:m,addFormatter:b,setObserver:p,hasObserver:h,setUi:y,hasUi:L,setDevBackend:R,addBackend:k,setLanguageDetector:j,setLanguageStorage:O}))},run(){var e,n;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r}=a();c.ui=null===(e=g.ui)||void 0===e?void 0:e.call(g,{apiKey:t,apiUrl:i,projectId:o,highlight:w.highlight,changeTranslation:s,findPositions:u}),null===(n=c.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:d,onClick:l})},stop(){var e;c.ui=void 0,null===(e=c.observer)||void 0===e||e.stop()},getLanguageStorage:()=>c.languageStorage,getInitialLanguage(){var e;const t=i();return n(null===(e=c.languageStorage)||void 0===e?void 0:e.getLanguage(),(e=>t&&!t.includes(e)||!e?function(){if(!c.languageDetector)return;const e=i();return c.languageDetector.getLanguage({availableLanguages:e})}():e))},setStoredLanguage(e){var n;null===(n=c.languageStorage)||void 0===n||n.setLanguage(e)},getDevBackend:()=>c.devBackend,getBackendRecord:({language:n,namespace:t})=>{for(const a of c.backends){const i=a.getRecord({language:n,namespace:t});if(e(i))return null==i?void 0:i.catch((e=>(console.error(e),{})));if(void 0!==i)return i}},getBackendDevRecord:({language:e,namespace:n})=>{var t;const{apiKey:i,apiUrl:o,projectId:r}=a();return null===(t=c.devBackend)||void 0===t?void 0:t.getRecord({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n})},getLanguageDetector:()=>c.languageDetector,retranslate(){var e;null===(e=c.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=c.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(w.getDevBackend()),formatTranslation(e){var n,{formatEnabled:i}=e,o=f(e,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:l,params:u,orEmpty:d,ns:p}=o,h=s||g;let b=h||(d?"":r);const m=t(),y=i||!(null===(n=c.observer)||void 0===n?void 0:n.outputNotFormattable),L=e=>c.observer&&!l?c.observer.wrap({key:r,translation:e,defaultValue:g,params:u,ns:p}):e;b=L(b);try{if(h&&m&&y)for(const e of c.formatters)b=e.format({translation:b,language:m,params:u});c.finalFormatter&&h&&m&&y&&(b=c.finalFormatter.format({translation:b,language:m,params:u}))}catch(e){console.error(e);const n=("string"==typeof(O=e)?O:"string"==typeof(null==O?void 0:O.message)?O.message:void 0)||v,t=a().onFormatError,i=typeof t;b="string"===i?t:"function"===i?t(n,o):v,b=L(b)}var O;return b}});return w}const L=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};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({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=f(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function j({options:a}){const l=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:g(a),onLanguageChange:g(a),onLoadingChange:g(a),onFetchingChange:g(a),onInitialLoaded:g(a),onRunningChange:g(a),onCacheChange:g(a),onUpdate:c(a,e,n),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(j,k),d=L(!1,(()=>b.isFetching()),l.onFetchingChange.emit),f=L(!1,(()=>F.isLoading()),l.onLoadingChange.emit),v=function(e,n,t){let a,g=m();const c=Object.freeze({init(e){g=m(e,g)},isRunning:()=>g.isRunning,setRunning(e){g.isRunning!==e&&(g.isRunning=e,t.emit(e))},isInitialLoading:()=>g.isInitialLoading,setInitialLoading(e){g.isInitialLoading=e},getLanguage:()=>g.language||g.initialOptions.language,setLanguage(n){g.language!==n&&(g.language=n,e.emit(n))},getPendingLanguage:()=>g.pendingLanguage||c.getLanguage(),setPendingLanguage(e){g.pendingLanguage!==e&&(g.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},g.initialOptions),a),addActiveNs(e){i(e).forEach((e=>{const n=g.activeNamespaces.get(e);void 0!==n?g.activeNamespaces.set(e,n+1):g.activeNamespaces.set(e,1)}))},removeActiveNs(e){i(e).forEach((e=>{const n=g.activeNamespaces.get(e);void 0!==n&&n>1?g.activeNamespaces.set(e,n-1):g.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>r([...g.initialOptions.ns||[g.initialOptions.defaultNs],...i(g.initialOptions.fallbackNs),...g.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||c.getLanguage();return n?r([n,...o(n,g.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>i(g.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?g.initialOptions.defaultNs:e,getAvailableLanguages(){if(g.initialOptions.availableLanguages)return g.initialOptions.availableLanguages;if(g.initialOptions.staticData){const e=Object.keys(g.initialOptions.staticData).map((e=>u(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?c.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:s(e.apiUrl)}):void 0}});return c}(l.onLanguageChange,l.onPendingLanguageChange,l.onRunningChange),h=y(v.getLanguage,v.getInitialOptions,v.getAvailableLanguages,(function({key:e,ns:n}){const t=v.getFallbackLangs(),a=R(n||void 0);return b.getTranslationNs(a,t,e)}),D,E),b=p(l.onCacheChange,h.getBackendRecord,h.getBackendDevRecord,v.withDefaultNs,v.isInitialLoading,d,f);function j(){return v.getFallbackNs()}function k(e){return v.getDefaultNs(e)}function R(e){return[...i(k(e)),...j()]}function w(e){return[...i(e||k()),...v.getRequiredNamespaces()]}function E(e,n,t){const a=v.withDefaultNs(e),i=b.getTranslation(a,n);return b.changeTranslation(a,n,t),{revert(){b.changeTranslation(a,n,i)}}}function N(e){v.init(e),b.addStaticData(v.getInitialOptions().staticData)}function A(e,t){const a=function(e,n){const t=v.getFallbackLangs(e),a=w(n),i=[];return t.forEach((e=>{a.forEach((n=>{b.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}(e,t);if(a.length)return n(F.loadRecords(a),(()=>{}))}function D({key:e,ns:n,language:t}){const a=R(n||void 0),i=v.getFallbackLangs(t);return b.getTranslationFallback(a,i,e)}function I(){const t=n(function(){if(v.getLanguage())return;return n(h.getInitialLanguage(),(e=>{const n=e||v.getInitialOptions().defaultLanguage;n&&v.setLanguage(n)}))}(),(()=>A()));if(e(t))return v.setInitialLoading(!0),d.notify(),f.notify(),Promise.resolve(t).then((()=>{v.setInitialLoading(!1),d.notify(),f.notify(),l.onInitialLoaded.emit()}));l.onInitialLoaded.emit()}a&&N(a),l.onUpdate.listen((()=>{v.isRunning()&&h.retranslate()}));const F=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},l),v),h),b),{init:N,getTranslation:D,changeTranslation:E,async changeLanguage(e){v.getPendingLanguage()===e&&v.getLanguage()===e||(v.setPendingLanguage(e),v.isRunning()&&await A(e),e===v.getPendingLanguage()&&(v.setLanguage(e),h.setStoredLanguage(e)))},async addActiveNs(e,n){n||v.addActiveNs(e),v.isRunning()&&await A(void 0,e)},loadRecords:e=>b.loadRecords(e,F.isDev()),loadRecord:async e=>(await F.loadRecords([e]))[0],isLoading:e=>b.isLoading(v.getLanguage(),e),isLoaded(e){const n=v.getLanguage();if(!n)return!1;const t=v.getFallbackLangs(n),a=w(e),i=[];return t.forEach((e=>{a.forEach((n=>{b.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=O(...e),t=D(n);return h.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(v.getInitialOptions().apiKey&&v.getInitialOptions().apiUrl),run(){let e;return function(){if((h.getLanguageDetector()||h.getLanguageStorage())&&!v.getAvailableLanguages())throw new Error(t("availableLanguages"));if(!v.getLanguage()&&!v.getInitialOptions().defaultLanguage)throw new Error(t(["defaultLanguage","language"]))}(),v.isRunning()||(v.setRunning(!0),h.run(),e=I()),Promise.resolve(e)},stop(){v.isRunning()&&(h.stop(),v.setRunning(!1))}}));return F}const k=0,R=1,w=2;class E extends Error{constructor(e,n,t){let a;a=e===k?"Empty parameter":e===R?"Unexpected character":"Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`),this.code=e,this.index=n}}function N(e){return/\s/.test(e)}const A=0,D=1,I=2,F=3,C=4,S=new Set([I,D,A]),P="'",T=new Set(["{","}",P]),U=e=>/[0-9a-zA-Z_]/.test(e);function x(e,n){const[t,a]=function(e){let n=A,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new E(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(k),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case A:i===P?(t+=i,n=D):"{"===i?(c(),n=F):(t+=i,n=A);break;case D:T.has(i)?(t=t.slice(0,-1)+i,n=I):(t+=i,n=A);break;case I:i===P?n=A:(t+=i,n=I);break;case F:"}"===i?(l(),n=A):N(i)?""!==a&&(l(),n=C):(U(i)||g(R),a+=i,n=F);break;case C:"}"==i?n=A:N(i)?n=C:g(R)}return S.has(n)||g(w),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}exports.FormatSimple=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>x(e,n)}),e),exports.TolgeeCore=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=b(e.options,t),n),init(n){const t=function(e){const n=j({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),n.isDev()&&n.invalidate(),t&&n.run()}n.isDev()&&n.invalidate();const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(b(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},exports.getFallback=a,exports.getFallbackArray=i,exports.getTranslateProps=O; | ||
//# sourceMappingURL=tolgee.cjs.min.js.map |
@@ -1123,5 +1123,2 @@ function isPromise(value) { | ||
if (!state.isRunning()) { | ||
if (self.isDev()) { | ||
cache.invalidate(); | ||
} | ||
state.setRunning(true); | ||
@@ -1146,2 +1143,6 @@ pluginService.run(); | ||
}); | ||
if (controller.isDev()) { | ||
// override existing data in DevMode | ||
controller.invalidate(); | ||
} | ||
// restarts tolgee while applying callback | ||
@@ -1152,2 +1153,4 @@ function withRestart(callback) { | ||
callback(); | ||
// invalidate cache when tolgee configuration is updated/plugin added in DevMode | ||
controller.isDev() && controller.invalidate(); | ||
wasRunning && controller.run(); | ||
@@ -1154,0 +1157,0 @@ } |
@@ -1,2 +0,2 @@ | ||
function e(e){return Boolean(e&&"function"==typeof e.then)}function n(n,t){return e(n)?Promise.resolve(n).then(t):t(n)}function t(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function a(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function i(e){return a(e)||[]}function o(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?i(n):i(null==n?void 0:n[e]);var t}function r(e){return Array.from(new Set(e))}function s(e){return e?e.replace(/\/+$/,""):e}function g(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function c(e,n,t){const a=new Set,o=new Set;let r=[];function s(){if(0===r.length)return;const e=r;r=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());o.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(r.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};o.add(n);const a={unsubscribe:()=>{o.delete(n)},subscribeNs:e=>(i(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}const l=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):l(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},u=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},d=({language:e,namespace:n})=>n?`${e}:${n}`:e;function p(e,n,t,a,o,s,g){const c=new Map,p=new Map;let f={},v=0;function h(n,t,a){const i=d(n);p.set(i,{data:l(t),version:a}),e.emit(n)}function m(e){let t;if(!t){const n=f[d(e)];"function"==typeof n&&(t=n())}return t||(t=n(e)),t}const b=Object.freeze({addStaticData(e){e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=u(e),a=p.get(e);a&&0!==a.version||h(t,n,0)}})))},invalidate(){c.clear(),v+=1},addRecord(e,n){h(e,n,v)},exists(e,n=!1){const t=p.get(d(e));return t&&n?t.version===v:Boolean(t)},getRecord(e){var n;return null===(n=p.get(d(a(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=p.get(d(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=p.get(d({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return r(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=p.get(d({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(n,t,a){var i;const o=null===(i=p.get(d(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching(e){if(o())return!0;if(void 0===e)return c.size>0;const n=i(e);return Boolean(Array.from(c.keys()).find((e=>n.includes(u(e).namespace))))},isLoading(e,n){const t=i(n);return Boolean(o()||Array.from(c.keys()).find((n=>{const a=u(n);return(!t.length||t.includes(a.namespace))&&!b.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const i=e.map((e=>{const i=a(e),o=d(i),r=c.get(o);if(r)return{new:!1,promise:r,keyObject:i,cacheKey:o};const s=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"),m(e))))),i||(i=m(e)),i}(i,n)||Promise.resolve(void 0);return c.set(o,s),{new:!0,promise:s,keyObject:i,cacheKey:o}}));s.notify(),g.notify();const o=await Promise.all(i.map((e=>e.promise)));return i.forEach(((e,n)=>{const t=c.get(e.cacheKey)!==e.promise;if(e.new&&!t){c.delete(e.cacheKey);const t=o[n];t?b.addRecord(e.keyObject,t):b.getRecord(e.keyObject)||b.addRecord(e.keyObject,{})}})),s.notify(),g.notify(),i.map((e=>b.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(p.entries()).map((([e,n])=>Object.assign(Object.assign({},u(e)),{data:n.data})))});return b}function f(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}const v="invalid",h={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"]},observerType:"invisible",onFormatError:v,apiUrl:"https://app.tolgee.io"},m=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function b(e,n){const t=m(h,null==n?void 0:n.initialOptions,e);return t.apiUrl=s(t.apiUrl),{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(t,a,i,o,r,s){const g={ui:void 0},c={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},l=async({keysAndDefaults:e,event:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,ns:o({key:e,ns:n}),translation:r({key:e,ns:n})})));null===(t=c.ui)||void 0===t||t.handleElementClick(a,n)},u=(e,n)=>{var t;return(null===(t=c.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function d(e){const n=r({key:e.key,ns:e.ns});return w.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function p(e){c.observer=null==e?void 0:e()}function h(){return Boolean(c.observer)}function m(e){e&&c.formatters.push(e)}function b(e){c.finalFormatter=e}function L(e){g.ui=e}function y(){return Boolean(g.ui)}function O(e){c.languageStorage=e}function j(e){c.languageDetector=e}function k(e){e&&c.backends.push(e)}function R(e){c.devBackend=e}const w=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:b,addFormatter:m,setObserver:p,hasObserver:h,setUi:L,hasUi:y,setDevBackend:R,addBackend:k,setLanguageDetector:j,setLanguageStorage:O}))},run(){var e,n;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r}=a();c.ui=null===(e=g.ui)||void 0===e?void 0:e.call(g,{apiKey:t,apiUrl:i,projectId:o,highlight:w.highlight,changeTranslation:s,findPositions:u}),null===(n=c.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:d,onClick:l})},stop(){var e;c.ui=void 0,null===(e=c.observer)||void 0===e||e.stop()},getLanguageStorage:()=>c.languageStorage,getInitialLanguage(){var e;const t=i();return n(null===(e=c.languageStorage)||void 0===e?void 0:e.getLanguage(),(e=>t&&!t.includes(e)||!e?function(){if(!c.languageDetector)return;const e=i();return c.languageDetector.getLanguage({availableLanguages:e})}():e))},setStoredLanguage(e){var n;null===(n=c.languageStorage)||void 0===n||n.setLanguage(e)},getDevBackend:()=>c.devBackend,getBackendRecord:({language:n,namespace:t})=>{for(const a of c.backends){const i=a.getRecord({language:n,namespace:t});if(e(i))return null==i?void 0:i.catch((e=>(console.error(e),{})));if(void 0!==i)return i}},getBackendDevRecord:({language:e,namespace:n})=>{var t;const{apiKey:i,apiUrl:o,projectId:r}=a();return null===(t=c.devBackend)||void 0===t?void 0:t.getRecord({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n})},getLanguageDetector:()=>c.languageDetector,retranslate(){var e;null===(e=c.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=c.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(w.getDevBackend()),formatTranslation(e){var n,{formatEnabled:i}=e,o=f(e,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:l,params:u,orEmpty:d,ns:p}=o,h=s||g;let m=h||(d?"":r);const b=t(),L=i||!(null===(n=c.observer)||void 0===n?void 0:n.outputNotFormattable),y=e=>c.observer&&!l?c.observer.wrap({key:r,translation:e,defaultValue:g,params:u,ns:p}):e;m=y(m);try{if(h&&b&&L)for(const e of c.formatters)m=e.format({translation:m,language:b,params:u});c.finalFormatter&&h&&b&&L&&(m=c.finalFormatter.format({translation:m,language:b,params:u}))}catch(e){console.error(e);const n=("string"==typeof(O=e)?O:"string"==typeof(null==O?void 0:O.message)?O.message:void 0)||v,t=a().onFormatError,i=typeof t;m="string"===i?t:"function"===i?t(n,o):v,m=y(m)}var O;return m}});return w}const y=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};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({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=f(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function j({options:a}){const l=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:g(a),onLanguageChange:g(a),onLoadingChange:g(a),onFetchingChange:g(a),onInitialLoaded:g(a),onRunningChange:g(a),onCacheChange:g(a),onUpdate:c(a,e,n),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(j,k),d=y(!1,(()=>m.isFetching()),l.onFetchingChange.emit),f=y(!1,(()=>C.isLoading()),l.onLoadingChange.emit),v=function(e,n,t){let a,g=b();const c=Object.freeze({init(e){g=b(e,g)},isRunning:()=>g.isRunning,setRunning(e){g.isRunning!==e&&(g.isRunning=e,t.emit(e))},isInitialLoading:()=>g.isInitialLoading,setInitialLoading(e){g.isInitialLoading=e},getLanguage:()=>g.language||g.initialOptions.language,setLanguage(n){g.language!==n&&(g.language=n,e.emit(n))},getPendingLanguage:()=>g.pendingLanguage||c.getLanguage(),setPendingLanguage(e){g.pendingLanguage!==e&&(g.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},g.initialOptions),a),addActiveNs(e){i(e).forEach((e=>{const n=g.activeNamespaces.get(e);void 0!==n?g.activeNamespaces.set(e,n+1):g.activeNamespaces.set(e,1)}))},removeActiveNs(e){i(e).forEach((e=>{const n=g.activeNamespaces.get(e);void 0!==n&&n>1?g.activeNamespaces.set(e,n-1):g.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>r([...g.initialOptions.ns||[g.initialOptions.defaultNs],...i(g.initialOptions.fallbackNs),...g.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||c.getLanguage();return n?r([n,...o(n,g.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>i(g.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?g.initialOptions.defaultNs:e,getAvailableLanguages(){if(g.initialOptions.availableLanguages)return g.initialOptions.availableLanguages;if(g.initialOptions.staticData){const e=Object.keys(g.initialOptions.staticData).map((e=>u(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?c.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:s(e.apiUrl)}):void 0}});return c}(l.onLanguageChange,l.onPendingLanguageChange,l.onRunningChange),h=L(v.getLanguage,v.getInitialOptions,v.getAvailableLanguages,(function({key:e,ns:n}){const t=v.getFallbackLangs(),a=R(n||void 0);return m.getTranslationNs(a,t,e)}),D,E),m=p(l.onCacheChange,h.getBackendRecord,h.getBackendDevRecord,v.withDefaultNs,v.isInitialLoading,d,f);function j(){return v.getFallbackNs()}function k(e){return v.getDefaultNs(e)}function R(e){return[...i(k(e)),...j()]}function w(e){return[...i(e||k()),...v.getRequiredNamespaces()]}function E(e,n,t){const a=v.withDefaultNs(e),i=m.getTranslation(a,n);return m.changeTranslation(a,n,t),{revert(){m.changeTranslation(a,n,i)}}}function N(e){v.init(e),m.addStaticData(v.getInitialOptions().staticData)}function A(e,t){const a=function(e,n){const t=v.getFallbackLangs(e),a=w(n),i=[];return t.forEach((e=>{a.forEach((n=>{m.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}(e,t);if(a.length)return n(C.loadRecords(a),(()=>{}))}function D({key:e,ns:n,language:t}){const a=R(n||void 0),i=v.getFallbackLangs(t);return m.getTranslationFallback(a,i,e)}function I(){const t=n(function(){if(v.getLanguage())return;return n(h.getInitialLanguage(),(e=>{const n=e||v.getInitialOptions().defaultLanguage;n&&v.setLanguage(n)}))}(),(()=>A()));if(e(t))return v.setInitialLoading(!0),d.notify(),f.notify(),Promise.resolve(t).then((()=>{v.setInitialLoading(!1),d.notify(),f.notify(),l.onInitialLoaded.emit()}));l.onInitialLoaded.emit()}a&&N(a),l.onUpdate.listen((()=>{v.isRunning()&&h.retranslate()}));const C=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},l),v),h),m),{init:N,getTranslation:D,changeTranslation:E,async changeLanguage(e){v.getPendingLanguage()===e&&v.getLanguage()===e||(v.setPendingLanguage(e),v.isRunning()&&await A(e),e===v.getPendingLanguage()&&(v.setLanguage(e),h.setStoredLanguage(e)))},async addActiveNs(e,n){n||v.addActiveNs(e),v.isRunning()&&await A(void 0,e)},loadRecords:e=>m.loadRecords(e,C.isDev()),loadRecord:async e=>(await C.loadRecords([e]))[0],isLoading:e=>m.isLoading(v.getLanguage(),e),isLoaded(e){const n=v.getLanguage();if(!n)return!1;const t=v.getFallbackLangs(n),a=w(e),i=[];return t.forEach((e=>{a.forEach((n=>{m.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=O(...e),t=D(n);return h.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(v.getInitialOptions().apiKey&&v.getInitialOptions().apiUrl),run(){let e;return function(){if((h.getLanguageDetector()||h.getLanguageStorage())&&!v.getAvailableLanguages())throw new Error(t("availableLanguages"));if(!v.getLanguage()&&!v.getInitialOptions().defaultLanguage)throw new Error(t(["defaultLanguage","language"]))}(),v.isRunning()||(C.isDev()&&m.invalidate(),v.setRunning(!0),h.run(),e=I()),Promise.resolve(e)},stop(){v.isRunning()&&(h.stop(),v.setRunning(!1))}}));return C}const k=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=m(e.options,t),n),init(n){const t=function(e){const n=j({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),t&&n.run()}const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(m(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},R=0,w=1,E=2;class N extends Error{constructor(e,n,t){let a;a=e===R?"Empty parameter":e===w?"Unexpected character":"Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`),this.code=e,this.index=n}}function A(e){return/\s/.test(e)}const D=0,I=1,C=2,F=3,S=4,P=new Set([C,I,D]),T="'",U=new Set(["{","}",T]),B=e=>/[0-9a-zA-Z_]/.test(e);function x(e,n){const[t,a]=function(e){let n=D,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new N(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(R),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case D:i===T?(t+=i,n=I):"{"===i?(c(),n=F):(t+=i,n=D);break;case I:U.has(i)?(t=t.slice(0,-1)+i,n=C):(t+=i,n=D);break;case C:i===T?n=D:(t+=i,n=C);break;case F:"}"===i?(l(),n=D):A(i)?""!==a&&(l(),n=S):(B(i)||g(w),a+=i,n=F);break;case S:"}"==i?n=D:A(i)?n=S:g(w)}return P.has(n)||g(E),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}const z=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>x(e,n)}),e);export{z as FormatSimple,k as TolgeeCore,a as getFallback,i as getFallbackArray,O as getTranslateProps}; | ||
function e(e){return Boolean(e&&"function"==typeof e.then)}function n(n,t){return e(n)?Promise.resolve(n).then(t):t(n)}function t(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function a(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function i(e){return a(e)||[]}function o(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?i(n):i(null==n?void 0:n[e]);var t}function r(e){return Array.from(new Set(e))}function s(e){return e?e.replace(/\/+$/,""):e}function g(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function c(e,n,t){const a=new Set,o=new Set;let r=[];function s(){if(0===r.length)return;const e=r;r=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());o.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(r.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};o.add(n);const a={unsubscribe:()=>{o.delete(n)},subscribeNs:e=>(i(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}const l=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):l(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},u=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},d=({language:e,namespace:n})=>n?`${e}:${n}`:e;function p(e,n,t,a,o,s,g){const c=new Map,p=new Map;let f={},v=0;function h(n,t,a){const i=d(n);p.set(i,{data:l(t),version:a}),e.emit(n)}function m(e){let t;if(!t){const n=f[d(e)];"function"==typeof n&&(t=n())}return t||(t=n(e)),t}const b=Object.freeze({addStaticData(e){e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=u(e),a=p.get(e);a&&0!==a.version||h(t,n,0)}})))},invalidate(){c.clear(),v+=1},addRecord(e,n){h(e,n,v)},exists(e,n=!1){const t=p.get(d(e));return t&&n?t.version===v:Boolean(t)},getRecord(e){var n;return null===(n=p.get(d(a(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=p.get(d(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=p.get(d({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return r(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=p.get(d({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(n,t,a){var i;const o=null===(i=p.get(d(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching(e){if(o())return!0;if(void 0===e)return c.size>0;const n=i(e);return Boolean(Array.from(c.keys()).find((e=>n.includes(u(e).namespace))))},isLoading(e,n){const t=i(n);return Boolean(o()||Array.from(c.keys()).find((n=>{const a=u(n);return(!t.length||t.includes(a.namespace))&&!b.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const i=e.map((e=>{const i=a(e),o=d(i),r=c.get(o);if(r)return{new:!1,promise:r,keyObject:i,cacheKey:o};const s=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"),m(e))))),i||(i=m(e)),i}(i,n)||Promise.resolve(void 0);return c.set(o,s),{new:!0,promise:s,keyObject:i,cacheKey:o}}));s.notify(),g.notify();const o=await Promise.all(i.map((e=>e.promise)));return i.forEach(((e,n)=>{const t=c.get(e.cacheKey)!==e.promise;if(e.new&&!t){c.delete(e.cacheKey);const t=o[n];t?b.addRecord(e.keyObject,t):b.getRecord(e.keyObject)||b.addRecord(e.keyObject,{})}})),s.notify(),g.notify(),i.map((e=>b.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(p.entries()).map((([e,n])=>Object.assign(Object.assign({},u(e)),{data:n.data})))});return b}function f(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}const v="invalid",h={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"]},observerType:"invisible",onFormatError:v,apiUrl:"https://app.tolgee.io"},m=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function b(e,n){const t=m(h,null==n?void 0:n.initialOptions,e);return t.apiUrl=s(t.apiUrl),{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(t,a,i,o,r,s){const g={ui:void 0},c={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},l=async({keysAndDefaults:e,event:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,ns:o({key:e,ns:n}),translation:r({key:e,ns:n})})));null===(t=c.ui)||void 0===t||t.handleElementClick(a,n)},u=(e,n)=>{var t;return(null===(t=c.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function d(e){const n=r({key:e.key,ns:e.ns});return w.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function p(e){c.observer=null==e?void 0:e()}function h(){return Boolean(c.observer)}function m(e){e&&c.formatters.push(e)}function b(e){c.finalFormatter=e}function L(e){g.ui=e}function y(){return Boolean(g.ui)}function O(e){c.languageStorage=e}function j(e){c.languageDetector=e}function k(e){e&&c.backends.push(e)}function R(e){c.devBackend=e}const w=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:b,addFormatter:m,setObserver:p,hasObserver:h,setUi:L,hasUi:y,setDevBackend:R,addBackend:k,setLanguageDetector:j,setLanguageStorage:O}))},run(){var e,n;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r}=a();c.ui=null===(e=g.ui)||void 0===e?void 0:e.call(g,{apiKey:t,apiUrl:i,projectId:o,highlight:w.highlight,changeTranslation:s,findPositions:u}),null===(n=c.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:d,onClick:l})},stop(){var e;c.ui=void 0,null===(e=c.observer)||void 0===e||e.stop()},getLanguageStorage:()=>c.languageStorage,getInitialLanguage(){var e;const t=i();return n(null===(e=c.languageStorage)||void 0===e?void 0:e.getLanguage(),(e=>t&&!t.includes(e)||!e?function(){if(!c.languageDetector)return;const e=i();return c.languageDetector.getLanguage({availableLanguages:e})}():e))},setStoredLanguage(e){var n;null===(n=c.languageStorage)||void 0===n||n.setLanguage(e)},getDevBackend:()=>c.devBackend,getBackendRecord:({language:n,namespace:t})=>{for(const a of c.backends){const i=a.getRecord({language:n,namespace:t});if(e(i))return null==i?void 0:i.catch((e=>(console.error(e),{})));if(void 0!==i)return i}},getBackendDevRecord:({language:e,namespace:n})=>{var t;const{apiKey:i,apiUrl:o,projectId:r}=a();return null===(t=c.devBackend)||void 0===t?void 0:t.getRecord({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n})},getLanguageDetector:()=>c.languageDetector,retranslate(){var e;null===(e=c.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=c.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(w.getDevBackend()),formatTranslation(e){var n,{formatEnabled:i}=e,o=f(e,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:l,params:u,orEmpty:d,ns:p}=o,h=s||g;let m=h||(d?"":r);const b=t(),L=i||!(null===(n=c.observer)||void 0===n?void 0:n.outputNotFormattable),y=e=>c.observer&&!l?c.observer.wrap({key:r,translation:e,defaultValue:g,params:u,ns:p}):e;m=y(m);try{if(h&&b&&L)for(const e of c.formatters)m=e.format({translation:m,language:b,params:u});c.finalFormatter&&h&&b&&L&&(m=c.finalFormatter.format({translation:m,language:b,params:u}))}catch(e){console.error(e);const n=("string"==typeof(O=e)?O:"string"==typeof(null==O?void 0:O.message)?O.message:void 0)||v,t=a().onFormatError,i=typeof t;m="string"===i?t:"function"===i?t(n,o):v,m=y(m)}var O;return m}});return w}const y=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};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({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=f(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function j({options:a}){const l=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:g(a),onLanguageChange:g(a),onLoadingChange:g(a),onFetchingChange:g(a),onInitialLoaded:g(a),onRunningChange:g(a),onCacheChange:g(a),onUpdate:c(a,e,n),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(j,k),d=y(!1,(()=>m.isFetching()),l.onFetchingChange.emit),f=y(!1,(()=>C.isLoading()),l.onLoadingChange.emit),v=function(e,n,t){let a,g=b();const c=Object.freeze({init(e){g=b(e,g)},isRunning:()=>g.isRunning,setRunning(e){g.isRunning!==e&&(g.isRunning=e,t.emit(e))},isInitialLoading:()=>g.isInitialLoading,setInitialLoading(e){g.isInitialLoading=e},getLanguage:()=>g.language||g.initialOptions.language,setLanguage(n){g.language!==n&&(g.language=n,e.emit(n))},getPendingLanguage:()=>g.pendingLanguage||c.getLanguage(),setPendingLanguage(e){g.pendingLanguage!==e&&(g.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},g.initialOptions),a),addActiveNs(e){i(e).forEach((e=>{const n=g.activeNamespaces.get(e);void 0!==n?g.activeNamespaces.set(e,n+1):g.activeNamespaces.set(e,1)}))},removeActiveNs(e){i(e).forEach((e=>{const n=g.activeNamespaces.get(e);void 0!==n&&n>1?g.activeNamespaces.set(e,n-1):g.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>r([...g.initialOptions.ns||[g.initialOptions.defaultNs],...i(g.initialOptions.fallbackNs),...g.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||c.getLanguage();return n?r([n,...o(n,g.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>i(g.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?g.initialOptions.defaultNs:e,getAvailableLanguages(){if(g.initialOptions.availableLanguages)return g.initialOptions.availableLanguages;if(g.initialOptions.staticData){const e=Object.keys(g.initialOptions.staticData).map((e=>u(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?c.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:s(e.apiUrl)}):void 0}});return c}(l.onLanguageChange,l.onPendingLanguageChange,l.onRunningChange),h=L(v.getLanguage,v.getInitialOptions,v.getAvailableLanguages,(function({key:e,ns:n}){const t=v.getFallbackLangs(),a=R(n||void 0);return m.getTranslationNs(a,t,e)}),A,E),m=p(l.onCacheChange,h.getBackendRecord,h.getBackendDevRecord,v.withDefaultNs,v.isInitialLoading,d,f);function j(){return v.getFallbackNs()}function k(e){return v.getDefaultNs(e)}function R(e){return[...i(k(e)),...j()]}function w(e){return[...i(e||k()),...v.getRequiredNamespaces()]}function E(e,n,t){const a=v.withDefaultNs(e),i=m.getTranslation(a,n);return m.changeTranslation(a,n,t),{revert(){m.changeTranslation(a,n,i)}}}function N(e){v.init(e),m.addStaticData(v.getInitialOptions().staticData)}function D(e,t){const a=function(e,n){const t=v.getFallbackLangs(e),a=w(n),i=[];return t.forEach((e=>{a.forEach((n=>{m.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}(e,t);if(a.length)return n(C.loadRecords(a),(()=>{}))}function A({key:e,ns:n,language:t}){const a=R(n||void 0),i=v.getFallbackLangs(t);return m.getTranslationFallback(a,i,e)}function I(){const t=n(function(){if(v.getLanguage())return;return n(h.getInitialLanguage(),(e=>{const n=e||v.getInitialOptions().defaultLanguage;n&&v.setLanguage(n)}))}(),(()=>D()));if(e(t))return v.setInitialLoading(!0),d.notify(),f.notify(),Promise.resolve(t).then((()=>{v.setInitialLoading(!1),d.notify(),f.notify(),l.onInitialLoaded.emit()}));l.onInitialLoaded.emit()}a&&N(a),l.onUpdate.listen((()=>{v.isRunning()&&h.retranslate()}));const C=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},l),v),h),m),{init:N,getTranslation:A,changeTranslation:E,async changeLanguage(e){v.getPendingLanguage()===e&&v.getLanguage()===e||(v.setPendingLanguage(e),v.isRunning()&&await D(e),e===v.getPendingLanguage()&&(v.setLanguage(e),h.setStoredLanguage(e)))},async addActiveNs(e,n){n||v.addActiveNs(e),v.isRunning()&&await D(void 0,e)},loadRecords:e=>m.loadRecords(e,C.isDev()),loadRecord:async e=>(await C.loadRecords([e]))[0],isLoading:e=>m.isLoading(v.getLanguage(),e),isLoaded(e){const n=v.getLanguage();if(!n)return!1;const t=v.getFallbackLangs(n),a=w(e),i=[];return t.forEach((e=>{a.forEach((n=>{m.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=O(...e),t=A(n);return h.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(v.getInitialOptions().apiKey&&v.getInitialOptions().apiUrl),run(){let e;return function(){if((h.getLanguageDetector()||h.getLanguageStorage())&&!v.getAvailableLanguages())throw new Error(t("availableLanguages"));if(!v.getLanguage()&&!v.getInitialOptions().defaultLanguage)throw new Error(t(["defaultLanguage","language"]))}(),v.isRunning()||(v.setRunning(!0),h.run(),e=I()),Promise.resolve(e)},stop(){v.isRunning()&&(h.stop(),v.setRunning(!1))}}));return C}const k=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=m(e.options,t),n),init(n){const t=function(e){const n=j({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),n.isDev()&&n.invalidate(),t&&n.run()}n.isDev()&&n.invalidate();const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(m(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},R=0,w=1,E=2;class N extends Error{constructor(e,n,t){let a;a=e===R?"Empty parameter":e===w?"Unexpected character":"Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`),this.code=e,this.index=n}}function D(e){return/\s/.test(e)}const A=0,I=1,C=2,F=3,S=4,P=new Set([C,I,A]),T="'",U=new Set(["{","}",T]),B=e=>/[0-9a-zA-Z_]/.test(e);function x(e,n){const[t,a]=function(e){let n=A,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new N(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(R),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case A:i===T?(t+=i,n=I):"{"===i?(c(),n=F):(t+=i,n=A);break;case I:U.has(i)?(t=t.slice(0,-1)+i,n=C):(t+=i,n=A);break;case C:i===T?n=A:(t+=i,n=C);break;case F:"}"===i?(l(),n=A):D(i)?""!==a&&(l(),n=S):(B(i)||g(w),a+=i,n=F);break;case S:"}"==i?n=A:D(i)?n=S:g(w)}return P.has(n)||g(E),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}const z=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>x(e,n)}),e);export{z as FormatSimple,k as TolgeeCore,a as getFallback,i as getFallbackArray,O as getTranslateProps}; | ||
//# sourceMappingURL=tolgee.esm.min.js.map |
@@ -1129,5 +1129,2 @@ (function (global, factory) { | ||
if (!state.isRunning()) { | ||
if (self.isDev()) { | ||
cache.invalidate(); | ||
} | ||
state.setRunning(true); | ||
@@ -1152,2 +1149,6 @@ pluginService.run(); | ||
}); | ||
if (controller.isDev()) { | ||
// override existing data in DevMode | ||
controller.invalidate(); | ||
} | ||
// restarts tolgee while applying callback | ||
@@ -1158,2 +1159,4 @@ function withRestart(callback) { | ||
callback(); | ||
// invalidate cache when tolgee configuration is updated/plugin added in DevMode | ||
controller.isDev() && controller.invalidate(); | ||
wasRunning && controller.run(); | ||
@@ -1160,0 +1163,0 @@ } |
@@ -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";function n(e){return Boolean(e&&"function"==typeof e.then)}function t(e,t){return n(e)?Promise.resolve(e).then(t):t(e)}function a(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function i(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function o(e){return i(e)||[]}function r(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?o(n):o(null==n?void 0:n[e]);var t}function s(e){return Array.from(new Set(e))}function g(e){return e?e.replace(/\/+$/,""):e}function c(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function l(e,n,t){const a=new Set,i=new Set;let r=[];function s(){if(0===r.length)return;const e=r;r=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());i.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(r.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};i.add(n);const a={unsubscribe:()=>{i.delete(n)},subscribeNs:e=>(o(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}const u=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):u(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},d=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},f=({language:e,namespace:n})=>n?`${e}:${n}`:e;function p(e,n,t,a,i,r,g){const c=new Map,l=new Map;let p={},v=0;function h(n,t,a){const i=f(n);l.set(i,{data:u(t),version:a}),e.emit(n)}function b(e){let t;if(!t){const n=p[f(e)];"function"==typeof n&&(t=n())}return t||(t=n(e)),t}const m=Object.freeze({addStaticData(e){e&&(p=Object.assign(Object.assign({},p),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=d(e),a=l.get(e);a&&0!==a.version||h(t,n,0)}})))},invalidate(){c.clear(),v+=1},addRecord(e,n){h(e,n,v)},exists(e,n=!1){const t=l.get(f(e));return t&&n?t.version===v:Boolean(t)},getRecord(e){var n;return null===(n=l.get(f(a(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=l.get(f(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=l.get(f({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return s(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=l.get(f({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(n,t,a){var i;const o=null===(i=l.get(f(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching(e){if(i())return!0;if(void 0===e)return c.size>0;const n=o(e);return Boolean(Array.from(c.keys()).find((e=>n.includes(d(e).namespace))))},isLoading(e,n){const t=o(n);return Boolean(i()||Array.from(c.keys()).find((n=>{const a=d(n);return(!t.length||t.includes(a.namespace))&&!m.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const i=e.map((e=>{const i=a(e),o=f(i),r=c.get(o);if(r)return{new:!1,promise:r,keyObject:i,cacheKey:o};const s=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"),b(e))))),i||(i=b(e)),i}(i,n)||Promise.resolve(void 0);return c.set(o,s),{new:!0,promise:s,keyObject:i,cacheKey:o}}));r.notify(),g.notify();const o=await Promise.all(i.map((e=>e.promise)));return i.forEach(((e,n)=>{const t=c.get(e.cacheKey)!==e.promise;if(e.new&&!t){c.delete(e.cacheKey);const t=o[n];t?m.addRecord(e.keyObject,t):m.getRecord(e.keyObject)||m.addRecord(e.keyObject,{})}})),r.notify(),g.notify(),i.map((e=>m.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(l.entries()).map((([e,n])=>Object.assign(Object.assign({},d(e)),{data:n.data})))});return m}function v(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}const h="invalid",b={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"]},observerType:"invisible",onFormatError:h,apiUrl:"https://app.tolgee.io"},m=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function y(e,n){const t=m(b,null==n?void 0:n.initialOptions,e);return t.apiUrl=g(t.apiUrl),{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,a,i,o,r,s){const g={ui:void 0},c={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},l=async({keysAndDefaults:e,event:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,ns:o({key:e,ns:n}),translation:r({key:e,ns:n})})));null===(t=c.ui)||void 0===t||t.handleElementClick(a,n)},u=(e,n)=>{var t;return(null===(t=c.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function d(e){const n=r({key:e.key,ns:e.ns});return w.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function f(e){c.observer=null==e?void 0:e()}function p(){return Boolean(c.observer)}function b(e){e&&c.formatters.push(e)}function m(e){c.finalFormatter=e}function y(e){g.ui=e}function L(){return Boolean(g.ui)}function O(e){c.languageStorage=e}function j(e){c.languageDetector=e}function k(e){e&&c.backends.push(e)}function R(e){c.devBackend=e}const w=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:m,addFormatter:b,setObserver:f,hasObserver:p,setUi:y,hasUi:L,setDevBackend:R,addBackend:k,setLanguageDetector:j,setLanguageStorage:O}))},run(){var e,n;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r}=a();c.ui=null===(e=g.ui)||void 0===e?void 0:e.call(g,{apiKey:t,apiUrl:i,projectId:o,highlight:w.highlight,changeTranslation:s,findPositions:u}),null===(n=c.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:d,onClick:l})},stop(){var e;c.ui=void 0,null===(e=c.observer)||void 0===e||e.stop()},getLanguageStorage:()=>c.languageStorage,getInitialLanguage(){var e;const n=i();return t(null===(e=c.languageStorage)||void 0===e?void 0:e.getLanguage(),(e=>n&&!n.includes(e)||!e?function(){if(!c.languageDetector)return;const e=i();return c.languageDetector.getLanguage({availableLanguages:e})}():e))},setStoredLanguage(e){var n;null===(n=c.languageStorage)||void 0===n||n.setLanguage(e)},getDevBackend:()=>c.devBackend,getBackendRecord:({language:e,namespace:t})=>{for(const a of c.backends){const i=a.getRecord({language:e,namespace:t});if(n(i))return null==i?void 0:i.catch((e=>(console.error(e),{})));if(void 0!==i)return i}},getBackendDevRecord:({language:e,namespace:n})=>{var t;const{apiKey:i,apiUrl:o,projectId:r}=a();return null===(t=c.devBackend)||void 0===t?void 0:t.getRecord({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n})},getLanguageDetector:()=>c.languageDetector,retranslate(){var e;null===(e=c.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=c.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(w.getDevBackend()),formatTranslation(n){var t,{formatEnabled:i}=n,o=v(n,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:l,params:u,orEmpty:d,ns:f}=o,p=s||g;let b=p||(d?"":r);const m=e(),y=i||!(null===(t=c.observer)||void 0===t?void 0:t.outputNotFormattable),L=e=>c.observer&&!l?c.observer.wrap({key:r,translation:e,defaultValue:g,params:u,ns:f}):e;b=L(b);try{if(p&&m&&y)for(const e of c.formatters)b=e.format({translation:b,language:m,params:u});c.finalFormatter&&p&&m&&y&&(b=c.finalFormatter.format({translation:b,language:m,params:u}))}catch(e){console.error(e);const n=("string"==typeof(O=e)?O:"string"==typeof(null==O?void 0:O.message)?O.message:void 0)||h,t=a().onFormatError,i=typeof t;b="string"===i?t:"function"===i?t(n,o):h,b=L(b)}var O;return b}});return w}const O=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};const j=(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({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=v(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function k({options:e}){const i=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:c(a),onLanguageChange:c(a),onLoadingChange:c(a),onFetchingChange:c(a),onInitialLoaded:c(a),onRunningChange:c(a),onCacheChange:c(a),onUpdate:l(a,e,n),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(m,k),u=O(!1,(()=>b.isFetching()),i.onFetchingChange.emit),f=O(!1,(()=>F.isLoading()),i.onLoadingChange.emit),v=function(e,n,t){let a,i=y();const c=Object.freeze({init(e){i=y(e,i)},isRunning:()=>i.isRunning,setRunning(e){i.isRunning!==e&&(i.isRunning=e,t.emit(e))},isInitialLoading:()=>i.isInitialLoading,setInitialLoading(e){i.isInitialLoading=e},getLanguage:()=>i.language||i.initialOptions.language,setLanguage(n){i.language!==n&&(i.language=n,e.emit(n))},getPendingLanguage:()=>i.pendingLanguage||c.getLanguage(),setPendingLanguage(e){i.pendingLanguage!==e&&(i.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},i.initialOptions),a),addActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n?i.activeNamespaces.set(e,n+1):i.activeNamespaces.set(e,1)}))},removeActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n&&n>1?i.activeNamespaces.set(e,n-1):i.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>s([...i.initialOptions.ns||[i.initialOptions.defaultNs],...o(i.initialOptions.fallbackNs),...i.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||c.getLanguage();return n?s([n,...r(n,i.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>o(i.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?i.initialOptions.defaultNs:e,getAvailableLanguages(){if(i.initialOptions.availableLanguages)return i.initialOptions.availableLanguages;if(i.initialOptions.staticData){const e=Object.keys(i.initialOptions.staticData).map((e=>d(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?c.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:g(e.apiUrl)}):void 0}});return c}(i.onLanguageChange,i.onPendingLanguageChange,i.onRunningChange),h=L(v.getLanguage,v.getInitialOptions,v.getAvailableLanguages,(function({key:e,ns:n}){const t=v.getFallbackLangs(),a=R(n||void 0);return b.getTranslationNs(a,t,e)}),D,E),b=p(i.onCacheChange,h.getBackendRecord,h.getBackendDevRecord,v.withDefaultNs,v.isInitialLoading,u,f);function m(){return v.getFallbackNs()}function k(e){return v.getDefaultNs(e)}function R(e){return[...o(k(e)),...m()]}function w(e){return[...o(e||k()),...v.getRequiredNamespaces()]}function E(e,n,t){const a=v.withDefaultNs(e),i=b.getTranslation(a,n);return b.changeTranslation(a,n,t),{revert(){b.changeTranslation(a,n,i)}}}function N(e){v.init(e),b.addStaticData(v.getInitialOptions().staticData)}function A(e,n){const a=function(e,n){const t=v.getFallbackLangs(e),a=w(n),i=[];return t.forEach((e=>{a.forEach((n=>{b.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}(e,n);if(a.length)return t(F.loadRecords(a),(()=>{}))}function D({key:e,ns:n,language:t}){const a=R(n||void 0),i=v.getFallbackLangs(t);return b.getTranslationFallback(a,i,e)}function I(){const e=t(function(){if(v.getLanguage())return;return t(h.getInitialLanguage(),(e=>{const n=e||v.getInitialOptions().defaultLanguage;n&&v.setLanguage(n)}))}(),(()=>A()));if(n(e))return v.setInitialLoading(!0),u.notify(),f.notify(),Promise.resolve(e).then((()=>{v.setInitialLoading(!1),u.notify(),f.notify(),i.onInitialLoaded.emit()}));i.onInitialLoaded.emit()}e&&N(e),i.onUpdate.listen((()=>{v.isRunning()&&h.retranslate()}));const F=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},i),v),h),b),{init:N,getTranslation:D,changeTranslation:E,async changeLanguage(e){v.getPendingLanguage()===e&&v.getLanguage()===e||(v.setPendingLanguage(e),v.isRunning()&&await A(e),e===v.getPendingLanguage()&&(v.setLanguage(e),h.setStoredLanguage(e)))},async addActiveNs(e,n){n||v.addActiveNs(e),v.isRunning()&&await A(void 0,e)},loadRecords:e=>b.loadRecords(e,F.isDev()),loadRecord:async e=>(await F.loadRecords([e]))[0],isLoading:e=>b.isLoading(v.getLanguage(),e),isLoaded(e){const n=v.getLanguage();if(!n)return!1;const t=v.getFallbackLangs(n),a=w(e),i=[];return t.forEach((e=>{a.forEach((n=>{b.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=j(...e),t=D(n);return h.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(v.getInitialOptions().apiKey&&v.getInitialOptions().apiUrl),run(){let e;return function(){if((h.getLanguageDetector()||h.getLanguageStorage())&&!v.getAvailableLanguages())throw new Error(a("availableLanguages"));if(!v.getLanguage()&&!v.getInitialOptions().defaultLanguage)throw new Error(a(["defaultLanguage","language"]))}(),v.isRunning()||(F.isDev()&&b.invalidate(),v.setRunning(!0),h.run(),e=I()),Promise.resolve(e)},stop(){v.isRunning()&&(h.stop(),v.setRunning(!1))}}));return F}const R=0,w=1,E=2;class N extends Error{constructor(e,n,t){let a;a=e===R?"Empty parameter":e===w?"Unexpected character":"Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`),this.code=e,this.index=n}}function A(e){return/\s/.test(e)}const D=0,I=1,F=2,C=3,S=4,P=new Set([F,I,D]),T="'",U=new Set(["{","}",T]),B=e=>/[0-9a-zA-Z_]/.test(e);function x(e,n){const[t,a]=function(e){let n=D,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new N(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(R),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case D:i===T?(t+=i,n=I):"{"===i?(c(),n=C):(t+=i,n=D);break;case I:U.has(i)?(t=t.slice(0,-1)+i,n=F):(t+=i,n=D);break;case F:i===T?n=D:(t+=i,n=F);break;case C:"}"===i?(l(),n=D):A(i)?""!==a&&(l(),n=S):(B(i)||g(w),a+=i,n=C);break;case S:"}"==i?n=D:A(i)?n=S:g(w)}return P.has(n)||g(E),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}e.FormatSimple=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>x(e,n)}),e),e.TolgeeCore=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=m(e.options,t),n),init(n){const t=function(e){const n=k({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),t&&n.run()}const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(m(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},e.getFallback=i,e.getFallbackArray=o,e.getTranslateProps=j,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";function n(e){return Boolean(e&&"function"==typeof e.then)}function t(e,t){return n(e)?Promise.resolve(e).then(t):t(e)}function a(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function i(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function o(e){return i(e)||[]}function r(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?o(n):o(null==n?void 0:n[e]);var t}function s(e){return Array.from(new Set(e))}function g(e){return e?e.replace(/\/+$/,""):e}function c(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function l(e,n,t){const a=new Set,i=new Set;let r=[];function s(){if(0===r.length)return;const e=r;r=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());i.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(r.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};i.add(n);const a={unsubscribe:()=>{i.delete(n)},subscribeNs:e=>(o(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}const u=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):u(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},d=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},f=({language:e,namespace:n})=>n?`${e}:${n}`:e;function p(e,n,t,a,i,r,g){const c=new Map,l=new Map;let p={},v=0;function h(n,t,a){const i=f(n);l.set(i,{data:u(t),version:a}),e.emit(n)}function b(e){let t;if(!t){const n=p[f(e)];"function"==typeof n&&(t=n())}return t||(t=n(e)),t}const m=Object.freeze({addStaticData(e){e&&(p=Object.assign(Object.assign({},p),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=d(e),a=l.get(e);a&&0!==a.version||h(t,n,0)}})))},invalidate(){c.clear(),v+=1},addRecord(e,n){h(e,n,v)},exists(e,n=!1){const t=l.get(f(e));return t&&n?t.version===v:Boolean(t)},getRecord(e){var n;return null===(n=l.get(f(a(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=l.get(f(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=l.get(f({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return s(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=l.get(f({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(n,t,a){var i;const o=null===(i=l.get(f(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching(e){if(i())return!0;if(void 0===e)return c.size>0;const n=o(e);return Boolean(Array.from(c.keys()).find((e=>n.includes(d(e).namespace))))},isLoading(e,n){const t=o(n);return Boolean(i()||Array.from(c.keys()).find((n=>{const a=d(n);return(!t.length||t.includes(a.namespace))&&!m.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const i=e.map((e=>{const i=a(e),o=f(i),r=c.get(o);if(r)return{new:!1,promise:r,keyObject:i,cacheKey:o};const s=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"),b(e))))),i||(i=b(e)),i}(i,n)||Promise.resolve(void 0);return c.set(o,s),{new:!0,promise:s,keyObject:i,cacheKey:o}}));r.notify(),g.notify();const o=await Promise.all(i.map((e=>e.promise)));return i.forEach(((e,n)=>{const t=c.get(e.cacheKey)!==e.promise;if(e.new&&!t){c.delete(e.cacheKey);const t=o[n];t?m.addRecord(e.keyObject,t):m.getRecord(e.keyObject)||m.addRecord(e.keyObject,{})}})),r.notify(),g.notify(),i.map((e=>m.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(l.entries()).map((([e,n])=>Object.assign(Object.assign({},d(e)),{data:n.data})))});return m}function v(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}const h="invalid",b={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"]},observerType:"invisible",onFormatError:h,apiUrl:"https://app.tolgee.io"},m=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function y(e,n){const t=m(b,null==n?void 0:n.initialOptions,e);return t.apiUrl=g(t.apiUrl),{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,a,i,o,r,s){const g={ui:void 0},c={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},l=async({keysAndDefaults:e,event:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,ns:o({key:e,ns:n}),translation:r({key:e,ns:n})})));null===(t=c.ui)||void 0===t||t.handleElementClick(a,n)},u=(e,n)=>{var t;return(null===(t=c.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function d(e){const n=r({key:e.key,ns:e.ns});return w.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function f(e){c.observer=null==e?void 0:e()}function p(){return Boolean(c.observer)}function b(e){e&&c.formatters.push(e)}function m(e){c.finalFormatter=e}function y(e){g.ui=e}function L(){return Boolean(g.ui)}function O(e){c.languageStorage=e}function j(e){c.languageDetector=e}function k(e){e&&c.backends.push(e)}function R(e){c.devBackend=e}const w=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:m,addFormatter:b,setObserver:f,hasObserver:p,setUi:y,hasUi:L,setDevBackend:R,addBackend:k,setLanguageDetector:j,setLanguageStorage:O}))},run(){var e,n;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r}=a();c.ui=null===(e=g.ui)||void 0===e?void 0:e.call(g,{apiKey:t,apiUrl:i,projectId:o,highlight:w.highlight,changeTranslation:s,findPositions:u}),null===(n=c.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:d,onClick:l})},stop(){var e;c.ui=void 0,null===(e=c.observer)||void 0===e||e.stop()},getLanguageStorage:()=>c.languageStorage,getInitialLanguage(){var e;const n=i();return t(null===(e=c.languageStorage)||void 0===e?void 0:e.getLanguage(),(e=>n&&!n.includes(e)||!e?function(){if(!c.languageDetector)return;const e=i();return c.languageDetector.getLanguage({availableLanguages:e})}():e))},setStoredLanguage(e){var n;null===(n=c.languageStorage)||void 0===n||n.setLanguage(e)},getDevBackend:()=>c.devBackend,getBackendRecord:({language:e,namespace:t})=>{for(const a of c.backends){const i=a.getRecord({language:e,namespace:t});if(n(i))return null==i?void 0:i.catch((e=>(console.error(e),{})));if(void 0!==i)return i}},getBackendDevRecord:({language:e,namespace:n})=>{var t;const{apiKey:i,apiUrl:o,projectId:r}=a();return null===(t=c.devBackend)||void 0===t?void 0:t.getRecord({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n})},getLanguageDetector:()=>c.languageDetector,retranslate(){var e;null===(e=c.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=c.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return c.observer?null===(n=c.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(w.getDevBackend()),formatTranslation(n){var t,{formatEnabled:i}=n,o=v(n,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:l,params:u,orEmpty:d,ns:f}=o,p=s||g;let b=p||(d?"":r);const m=e(),y=i||!(null===(t=c.observer)||void 0===t?void 0:t.outputNotFormattable),L=e=>c.observer&&!l?c.observer.wrap({key:r,translation:e,defaultValue:g,params:u,ns:f}):e;b=L(b);try{if(p&&m&&y)for(const e of c.formatters)b=e.format({translation:b,language:m,params:u});c.finalFormatter&&p&&m&&y&&(b=c.finalFormatter.format({translation:b,language:m,params:u}))}catch(e){console.error(e);const n=("string"==typeof(O=e)?O:"string"==typeof(null==O?void 0:O.message)?O.message:void 0)||h,t=a().onFormatError,i=typeof t;b="string"===i?t:"function"===i?t(n,o):h,b=L(b)}var O;return b}});return w}const O=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};const j=(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({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=v(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function k({options:e}){const i=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:c(a),onLanguageChange:c(a),onLoadingChange:c(a),onFetchingChange:c(a),onInitialLoaded:c(a),onRunningChange:c(a),onCacheChange:c(a),onUpdate:l(a,e,n),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(m,k),u=O(!1,(()=>b.isFetching()),i.onFetchingChange.emit),f=O(!1,(()=>F.isLoading()),i.onLoadingChange.emit),v=function(e,n,t){let a,i=y();const c=Object.freeze({init(e){i=y(e,i)},isRunning:()=>i.isRunning,setRunning(e){i.isRunning!==e&&(i.isRunning=e,t.emit(e))},isInitialLoading:()=>i.isInitialLoading,setInitialLoading(e){i.isInitialLoading=e},getLanguage:()=>i.language||i.initialOptions.language,setLanguage(n){i.language!==n&&(i.language=n,e.emit(n))},getPendingLanguage:()=>i.pendingLanguage||c.getLanguage(),setPendingLanguage(e){i.pendingLanguage!==e&&(i.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},i.initialOptions),a),addActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n?i.activeNamespaces.set(e,n+1):i.activeNamespaces.set(e,1)}))},removeActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n&&n>1?i.activeNamespaces.set(e,n-1):i.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>s([...i.initialOptions.ns||[i.initialOptions.defaultNs],...o(i.initialOptions.fallbackNs),...i.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||c.getLanguage();return n?s([n,...r(n,i.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>o(i.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?i.initialOptions.defaultNs:e,getAvailableLanguages(){if(i.initialOptions.availableLanguages)return i.initialOptions.availableLanguages;if(i.initialOptions.staticData){const e=Object.keys(i.initialOptions.staticData).map((e=>d(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?c.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:g(e.apiUrl)}):void 0}});return c}(i.onLanguageChange,i.onPendingLanguageChange,i.onRunningChange),h=L(v.getLanguage,v.getInitialOptions,v.getAvailableLanguages,(function({key:e,ns:n}){const t=v.getFallbackLangs(),a=R(n||void 0);return b.getTranslationNs(a,t,e)}),D,E),b=p(i.onCacheChange,h.getBackendRecord,h.getBackendDevRecord,v.withDefaultNs,v.isInitialLoading,u,f);function m(){return v.getFallbackNs()}function k(e){return v.getDefaultNs(e)}function R(e){return[...o(k(e)),...m()]}function w(e){return[...o(e||k()),...v.getRequiredNamespaces()]}function E(e,n,t){const a=v.withDefaultNs(e),i=b.getTranslation(a,n);return b.changeTranslation(a,n,t),{revert(){b.changeTranslation(a,n,i)}}}function N(e){v.init(e),b.addStaticData(v.getInitialOptions().staticData)}function A(e,n){const a=function(e,n){const t=v.getFallbackLangs(e),a=w(n),i=[];return t.forEach((e=>{a.forEach((n=>{b.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}(e,n);if(a.length)return t(F.loadRecords(a),(()=>{}))}function D({key:e,ns:n,language:t}){const a=R(n||void 0),i=v.getFallbackLangs(t);return b.getTranslationFallback(a,i,e)}function I(){const e=t(function(){if(v.getLanguage())return;return t(h.getInitialLanguage(),(e=>{const n=e||v.getInitialOptions().defaultLanguage;n&&v.setLanguage(n)}))}(),(()=>A()));if(n(e))return v.setInitialLoading(!0),u.notify(),f.notify(),Promise.resolve(e).then((()=>{v.setInitialLoading(!1),u.notify(),f.notify(),i.onInitialLoaded.emit()}));i.onInitialLoaded.emit()}e&&N(e),i.onUpdate.listen((()=>{v.isRunning()&&h.retranslate()}));const F=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},i),v),h),b),{init:N,getTranslation:D,changeTranslation:E,async changeLanguage(e){v.getPendingLanguage()===e&&v.getLanguage()===e||(v.setPendingLanguage(e),v.isRunning()&&await A(e),e===v.getPendingLanguage()&&(v.setLanguage(e),h.setStoredLanguage(e)))},async addActiveNs(e,n){n||v.addActiveNs(e),v.isRunning()&&await A(void 0,e)},loadRecords:e=>b.loadRecords(e,F.isDev()),loadRecord:async e=>(await F.loadRecords([e]))[0],isLoading:e=>b.isLoading(v.getLanguage(),e),isLoaded(e){const n=v.getLanguage();if(!n)return!1;const t=v.getFallbackLangs(n),a=w(e),i=[];return t.forEach((e=>{a.forEach((n=>{b.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=j(...e),t=D(n);return h.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(v.getInitialOptions().apiKey&&v.getInitialOptions().apiUrl),run(){let e;return function(){if((h.getLanguageDetector()||h.getLanguageStorage())&&!v.getAvailableLanguages())throw new Error(a("availableLanguages"));if(!v.getLanguage()&&!v.getInitialOptions().defaultLanguage)throw new Error(a(["defaultLanguage","language"]))}(),v.isRunning()||(v.setRunning(!0),h.run(),e=I()),Promise.resolve(e)},stop(){v.isRunning()&&(h.stop(),v.setRunning(!1))}}));return F}const R=0,w=1,E=2;class N extends Error{constructor(e,n,t){let a;a=e===R?"Empty parameter":e===w?"Unexpected character":"Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`),this.code=e,this.index=n}}function A(e){return/\s/.test(e)}const D=0,I=1,F=2,C=3,S=4,P=new Set([F,I,D]),T="'",U=new Set(["{","}",T]),B=e=>/[0-9a-zA-Z_]/.test(e);function x(e,n){const[t,a]=function(e){let n=D,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new N(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(R),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case D:i===T?(t+=i,n=I):"{"===i?(c(),n=C):(t+=i,n=D);break;case I:U.has(i)?(t=t.slice(0,-1)+i,n=F):(t+=i,n=D);break;case F:i===T?n=D:(t+=i,n=F);break;case C:"}"===i?(l(),n=D):A(i)?""!==a&&(l(),n=S):(B(i)||g(w),a+=i,n=C);break;case S:"}"==i?n=D:A(i)?n=S:g(w)}return P.has(n)||g(E),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}e.FormatSimple=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>x(e,n)}),e),e.TolgeeCore=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=m(e.options,t),n),init(n){const t=function(e){const n=k({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),n.isDev()&&n.invalidate(),t&&n.run()}n.isDev()&&n.invalidate();const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(m(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},e.getFallback=i,e.getFallbackArray=o,e.getTranslateProps=j,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=tolgee.umd.min.js.map |
@@ -137,3 +137,5 @@ import { TolgeeOptions, TolgeePlugin, DevCredentials, TFnType, DefaultParamType, TranslationKey } from './types'; | ||
availableLanguages?: string[] | undefined; | ||
fallbackLanguage?: import("./types").FallbackLanguageOption; | ||
fallbackLanguage?: import("./types").FallbackLanguageOption; /** | ||
* Turn off/on events emitting. Is on by default. | ||
*/ | ||
ns?: string[] | undefined; | ||
@@ -140,0 +142,0 @@ fallbackNs?: import("./types").FallbackGeneral; |
{ | ||
"name": "@tolgee/core", | ||
"version": "5.10.0", | ||
"version": "5.10.1", | ||
"description": "Library providing ability to translate messages directly in context of developed application.", | ||
@@ -71,3 +71,3 @@ "main": "./dist/tolgee.cjs.js", | ||
"sideEffects": false, | ||
"gitHead": "2fd3b3f7cdf7e71df7a5e40fc1374afd5a02f2ca" | ||
"gitHead": "619a60f30c44c59db1866be0fcbc7f5dc26eb81d" | ||
} |
@@ -280,5 +280,2 @@ import { Events } from './Events/Events'; | ||
if (!state.isRunning()) { | ||
if (self.isDev()) { | ||
cache.invalidate(); | ||
} | ||
state.setRunning(true); | ||
@@ -285,0 +282,0 @@ pluginService.run(); |
@@ -17,2 +17,7 @@ import { Controller } from './Controller/Controller'; | ||
if (controller.isDev()) { | ||
// override existing data in DevMode | ||
controller.invalidate(); | ||
} | ||
// restarts tolgee while applying callback | ||
@@ -23,2 +28,4 @@ function withRestart(callback: () => void) { | ||
callback(); | ||
// invalidate cache when tolgee configuration is updated/plugin added in DevMode | ||
controller.isDev() && controller.invalidate(); | ||
wasRunning && controller.run(); | ||
@@ -25,0 +32,0 @@ } |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
912032
12300