@logistically/i18n-react-core
Version:
React integration for @logistically/i18n with state management agnostic design
3 lines (2 loc) • 5.98 kB
JavaScript
;var e=require("@reduxjs/toolkit");const t=e=>e?{message:e.message,name:e.name,stack:e.stack}:null,a=e=>{if(!e)return null;const t=new Error(e.message);return t.name=e.name,t.stack=e.stack,t},s=e.createAsyncThunk("translation/loadTranslations",async(e,{dispatch:t,getState:a})=>{const s=performance.now();try{const r=a(),n=a.core||r.core||r.translation.core;if(!n)throw new Error("Translation core not initialized");await n.loadTranslations(e);const o=performance.now();return t(p({translationTime:o-s})),{locale:e,success:!0}}catch(e){const a=performance.now();throw t(p({translationTime:a-s})),e}}),r=e.createAsyncThunk("translation/reloadTranslations",async(e,{dispatch:t,getState:a})=>{const s=performance.now();try{const e=a(),r=a.core||e.core||e.translation.core;if(!r)throw new Error("Translation core not initialized");await r.reloadTranslations();const n=performance.now();return t(p({translationTime:n-s})),{success:!0}}catch(e){const a=performance.now();throw t(p({translationTime:a-s})),e}}),n=e.createSlice({name:"translation",initialState:{locale:"en",translations:{},isLoading:!1,error:null,isInitialized:!1,lastUpdated:Date.now(),pendingActions:[],performanceMetrics:{translationTime:0,cacheHitRate:0,lastCacheCleanup:Date.now()}},reducers:{setLocale:(e,t)=>{e.locale=t.payload,e.lastUpdated=Date.now()},setLoading:(e,t)=>{e.isLoading=t.payload},setError:(e,a)=>{a.payload instanceof Error?e.error=t(a.payload):e.error=a.payload??null,e.lastUpdated=Date.now()},setInitialized:(e,t)=>{e.isInitialized=t.payload},setTranslations:(e,t)=>{e.translations=t.payload,e.lastUpdated=Date.now()},setPerformanceMetrics:(e,t)=>{e.performanceMetrics={...e.performanceMetrics,...t.payload}},addPendingAction:(e,t)=>{e.pendingActions.includes(t.payload)||e.pendingActions.push(t.payload)},removePendingAction:(e,t)=>{e.pendingActions=e.pendingActions.filter(e=>e!==t.payload)},updateLastUpdated:e=>{e.lastUpdated=Date.now()},clearCache:e=>{e.translations={},e.performanceMetrics.lastCacheCleanup=Date.now(),e.lastUpdated=Date.now()}},extraReducers:e=>{e.addCase(s.pending,e=>{e.isLoading=!0,e.error=null,e.pendingActions.includes("loadTranslations")||e.pendingActions.push("loadTranslations")}).addCase(s.fulfilled,(e,t)=>{e.isLoading=!1,e.error=null,e.pendingActions=e.pendingActions.filter(e=>"loadTranslations"!==e),e.lastUpdated=Date.now()}).addCase(s.rejected,(e,a)=>{e.isLoading=!1,e.error=t(a.error),e.pendingActions=e.pendingActions.filter(e=>"loadTranslations"!==e),e.lastUpdated=Date.now()}).addCase(r.pending,e=>{e.isLoading=!0,e.error=null,e.pendingActions.includes("reloadTranslations")||e.pendingActions.push("reloadTranslations")}).addCase(r.fulfilled,e=>{e.isLoading=!1,e.error=null,e.pendingActions=e.pendingActions.filter(e=>"reloadTranslations"!==e),e.lastUpdated=Date.now()}).addCase(r.rejected,(e,a)=>{e.isLoading=!1,e.error=t(a.error),e.pendingActions=e.pendingActions.filter(e=>"reloadTranslations"!==e),e.lastUpdated=Date.now()})}}),{setLocale:o,setLoading:i,setError:c,setInitialized:l,setTranslations:d,setPerformanceMetrics:p,addPendingAction:u,removePendingAction:h,updateLastUpdated:g,clearCache:m}=n.actions,T=n.reducer;class f{constructor(e,t){this.unsubscribe=null,this.core=e,this.store=t,this.unsubscribe=this.core.subscribe(e=>{this.store.dispatch(d(e.translations)),this.store.dispatch(o(e.locale)),this.store.dispatch(i(e.isLoading)),this.store.dispatch(c(e.error)),this.store.dispatch(l(e.isInitialized))})}getState(){const e=this.store.getState().translation;return{...e,error:a(e.error)}}getActions(){const e=this.store.dispatch;return{setLocale:async t=>{await this.core.setLocale(t),e(o(t))},loadTranslations:async t=>{await this.core.loadTranslations(t),e(s(t))},translate:(t,a)=>{const s=this.core.translate(t,a);return e(g()),s},translatePlural:(t,a,s)=>{const r=this.core.translatePlural(t,a,s);return e(g()),r},formatDate:(t,a)=>{const s=this.core.formatDate(t,a);return e(g()),s},formatNumber:(t,a)=>{const s=this.core.formatNumber(t,a);return e(g()),s},getTextDirection:e=>this.core.getTextDirection(e),isRTLLocale:e=>this.core.isRTLLocale(e),reloadTranslations:async()=>{await this.core.reloadTranslations(),e(r())},clearCache:()=>{this.core.clearCache(),e(m())}}}subscribe(e){let t=this.getState();return this.store.subscribe(()=>{const a=this.getState();a!==t&&(t=a,e(a))})}async initialize(e){try{this.store.dispatch(i(!0)),this.store.dispatch(c(null)),await this.core.initialize(),this.store.dispatch(l(!0)),this.store.dispatch(i(!1))}catch(e){throw this.store.dispatch(c(e)),this.store.dispatch(i(!1)),e}}destroy(){this.unsubscribe&&"function"==typeof this.unsubscribe&&(this.unsubscribe(),this.unsubscribe=null),this.core.destroy()}}exports.ReduxAdapter=f,exports.addPendingAction=u,exports.clearCache=m,exports.createReduxAdapter=(e,t)=>new f(e,t),exports.loadTranslationsAsync=s,exports.reloadTranslationsAsync=r,exports.removePendingAction=h,exports.setError=c,exports.setInitialized=l,exports.setLoading=i,exports.setLocale=o,exports.setPerformanceMetrics=p,exports.setTranslations=d,exports.translationReducer=T,exports.translationSlice=n,exports.updateLastUpdated=g,exports.useReduxTranslation=()=>{const{useSelector:e,useDispatch:t}=require("react-redux"),n=t(),i=e(e=>e.translation),c={setLocale:e=>n(o(e)),loadTranslations:e=>n(s(e)),reloadTranslations:()=>n(r()),clearCache:()=>n(m()),setPerformanceMetrics:e=>n(p(e))};return{...i,error:a(i.error),...c}},exports.useReduxTranslationWithMetrics=()=>{const{useSelector:e,useDispatch:t}=require("react-redux"),n=t(),i=e(e=>e.translation),c={setLocale:e=>{const t=performance.now();n(o(e));const a=performance.now();n(p({translationTime:a-t}))},loadTranslations:e=>n(s(e)),reloadTranslations:()=>n(r()),clearCache:()=>n(m()),setPerformanceMetrics:e=>n(p(e))};return{...i,error:a(i.error),...c,getPerformanceMetrics:()=>i.performanceMetrics,isActionPending:e=>i.pendingActions.includes(e),getLastUpdated:()=>i.lastUpdated}};
//# sourceMappingURL=redux.js.map