@logistically/i18n-react-core
Version:
React integration for @logistically/i18n with state management agnostic design
12 lines (11 loc) • 9.74 kB
JavaScript
import e,{createContext as t,useState as r,useEffect as a,useContext as i}from"react";import{TranslationService as n}from"@logistically/i18n";var s,o={exports:{}},l={};var c,u={};
/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/"production"===process.env.NODE_ENV?o.exports=function(){if(s)return l;s=1;var e=Symbol.for("react.transitional.element"),t=Symbol.for("react.fragment");function r(t,r,a){var i=null;if(void 0!==a&&(i=""+a),void 0!==r.key&&(i=""+r.key),"key"in r)for(var n in a={},r)"key"!==n&&(a[n]=r[n]);else a=r;return r=a.ref,{$$typeof:e,type:t,key:i,ref:void 0!==r?r:null,props:a}}return l.Fragment=t,l.jsx=r,l.jsxs=r,l}():o.exports=(c||(c=1,"production"!==process.env.NODE_ENV&&function(){function t(e){if(null==e)return null;if("function"==typeof e)return e.$$typeof===k?null:e.displayName||e.name||null;if("string"==typeof e)return e;switch(e){case y:return"Fragment";case g:return"Profiler";case b:return"StrictMode";case v:return"Suspense";case L:return"SuspenseList";case w:return"Activity"}if("object"==typeof e)switch(e.tag,e.$$typeof){case d:return"Portal";case p:return(e.displayName||"Context")+".Provider";case m:return(e._context.displayName||"Context")+".Consumer";case S:var r=e.render;return(e=e.displayName)||(e=""!==(e=r.displayName||r.name||"")?"ForwardRef("+e+")":"ForwardRef"),e;case T:return null!==(r=e.displayName||null)?r:t(e.type)||"Memo";case _:r=e._payload,e=e._init;try{return t(e(r))}catch(e){}}return null}function r(e){return""+e}function a(e){try{r(e);var t=!1}catch(e){t=!0}if(t){var a=(t=console).error,i="function"==typeof Symbol&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return a.call(t,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",i),r(e)}}function i(e){if(e===y)return"<>";if("object"==typeof e&&null!==e&&e.$$typeof===_)return"<...>";try{var r=t(e);return r?"<"+r+">":"<...>"}catch(e){return"<...>"}}function n(){return Error("react-stack-top-frame")}function s(){var e=t(this.type);return P[e]||(P[e]=!0),void 0!==(e=this.props.ref)?e:null}function o(e,r,i,n,o,u,f,d){var y,b=r.children;if(void 0!==b)if(n){if(N(b)){for(n=0;n<b.length;n++)l(b[n]);Object.freeze&&Object.freeze(b)}}else l(b);if(O.call(r,"key")){b=t(e);var g=Object.keys(r).filter(function(e){return"key"!==e});n=0<g.length?"{key: someKey, "+g.join(": ..., ")+": ...}":"{key: someKey}",A[b+n]||(g=0<g.length?"{"+g.join(": ..., ")+": ...}":"{}",A[b+n]=!0)}if(b=null,void 0!==i&&(a(i),b=""+i),function(e){if(O.call(e,"key")){var t=Object.getOwnPropertyDescriptor(e,"key").get;if(t&&t.isReactWarning)return!1}return void 0!==e.key}(r)&&(a(r.key),b=""+r.key),"key"in r)for(var m in i={},r)"key"!==m&&(i[m]=r[m]);else i=r;return b&&function(e){function t(){c||(c=!0)}t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}(i,"function"==typeof e&&(e.displayName||e.name)),function(e,t,r,a,i,n,o,l){return r=n.ref,e={$$typeof:h,type:e,key:t,props:n,_owner:i},null!==(void 0!==r?r:null)?Object.defineProperty(e,"ref",{enumerable:!1,get:s}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:o}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:l}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}(e,b,u,0,null===(y=j.A)?null:y.getOwner(),i,f,d)}function l(e){"object"==typeof e&&null!==e&&e.$$typeof===h&&e._store&&(e._store.validated=1)}var c,f=e,h=Symbol.for("react.transitional.element"),d=Symbol.for("react.portal"),y=Symbol.for("react.fragment"),b=Symbol.for("react.strict_mode"),g=Symbol.for("react.profiler"),m=Symbol.for("react.consumer"),p=Symbol.for("react.context"),S=Symbol.for("react.forward_ref"),v=Symbol.for("react.suspense"),L=Symbol.for("react.suspense_list"),T=Symbol.for("react.memo"),_=Symbol.for("react.lazy"),w=Symbol.for("react.activity"),k=Symbol.for("react.client.reference"),j=f.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,O=Object.prototype.hasOwnProperty,N=Array.isArray,x=console.createTask?console.createTask:function(){return null},P={},R=(f={react_stack_bottom_frame:function(e){return e()}}).react_stack_bottom_frame.bind(f,n)(),D=x(i(n)),A={};u.Fragment=y,u.jsx=function(e,t,r,a,n){var s=1e4>j.recentlyCreatedOwnerStacks++;return o(e,t,r,!1,0,n,s?Error("react-stack-top-frame"):R,s?x(i(e)):D)},u.jsxs=function(e,t,r,a,n){var s=1e4>j.recentlyCreatedOwnerStacks++;return o(e,t,r,!0,0,n,s?Error("react-stack-top-frame"):R,s?x(i(e)):D)}}()),u);var f=o.exports;class h{constructor(e){this.listeners=new Set,this.config=e;const t={...e,debug:"boolean"==typeof e.debug?e.debug:e.debug?.enabled||!1};this.translationService=new n(t),this.state={locale:e.defaultLocale||"en",translations:{},isLoading:!1,error:null,isInitialized:!1}}getState(){return{...this.state}}setState(e){this.state={...this.state,...e},this.notifyListeners()}notifyListeners(){this.listeners.forEach(e=>e(this.getState()))}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}getActions(){return{setLocale:this.setLocale.bind(this),loadTranslations:this.loadTranslations.bind(this),translate:this.translate.bind(this),translatePlural:this.translatePlural.bind(this),formatDate:this.formatDate.bind(this),formatNumber:this.formatNumber.bind(this),getTextDirection:this.getTextDirection.bind(this),isRTLLocale:this.isRTLLocale.bind(this),reloadTranslations:this.reloadTranslations.bind(this),clearCache:this.clearCache.bind(this)}}async setLocale(e){if(this.state.locale!==e){this.setState({locale:e,isLoading:!0,error:null});try{await this.loadTranslations(e),this.setState({isLoading:!1,isInitialized:!0})}catch(e){throw this.setState({isLoading:!1,error:e}),e}}}async loadTranslations(e){try{this.setState({isLoading:!0,error:null}),this.translationService.reloadTranslations();const t=this.translationService.getKeys(e),r={};t.forEach(t=>{r[t]=this.translationService.translate(t,e)}),this.setState({translations:r,isLoading:!1,isInitialized:!0})}catch(e){throw this.setState({isLoading:!1,error:e}),e}}translate(e,t){try{return this.translationService.translate(e,this.state.locale,t)}catch(t){return("object"==typeof this.config.debug?this.config.debug:{enabled:!1}).logMissingKeys,e}}translatePlural(e,t,r){try{return this.translationService.translatePlural(e,t,this.state.locale,r)}catch(t){return("object"==typeof this.config.debug?this.config.debug:{enabled:!1}).logMissingKeys,e}}formatDate(e,t){return this.translationService.formatDateForLocale(e,this.state.locale,t)}formatNumber(e,t){return this.translationService.formatNumberForLocale(e,this.state.locale)}getTextDirection(e){return this.translationService.getTextDirection(e)}isRTLLocale(e){return this.translationService.isRTLLocale(e)}async reloadTranslations(){await this.loadTranslations(this.state.locale)}clearCache(){this.translationService.clearCache()}setAdapter(e){this.adapter=e}getAdapter(){return this.adapter}async initialize(e){const t=e||this.config.defaultLocale||"en";try{this.setState({isLoading:!0,error:null}),this.setState({locale:t}),await this.loadTranslations(t),this.setState({isLoading:!1,isInitialized:!0})}catch(e){throw this.setState({isLoading:!1,error:e}),e}}destroy(){this.listeners.clear(),this.translationService.clearCache()}getTranslationService(){return this.translationService}getConfig(){return this.config}isInitialized(){return this.state.isInitialized}}const d=t(null);class y{constructor(e){this.listeners=new Set,this.core=e,this.state=e.getState(),this.core.subscribe(e=>{this.state=e,this.notifyListeners()})}getState(){return this.state}getActions(){return this.core.getActions()}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}notifyListeners(){this.listeners.forEach(e=>e(this.state))}async initialize(){await this.core.initialize()}destroy(){this.listeners.clear(),this.core.destroy()}}const b=({children:e,config:t,initialLocale:i})=>{const[n,s]=r(null),[o,l]=r(!1);return a(()=>{const e=new h(t),r=new y(e),a={...r.getState(),...r.getActions()};s(a);const n=r.subscribe(e=>{const t=r.getActions();s({...e,...t})});return(async()=>{try{await e.initialize(i);const t=r.getState(),a=r.getActions();s({...t,...a}),l(!0)}catch(e){s({...r.getState(),...r.getActions()}),l(!0)}})(),()=>{n(),r.destroy()}},[t,i]),n?f.jsx(d.Provider,{value:n,children:e}):f.jsx(d.Provider,{value:null,children:f.jsx("div",{children:"Loading..."})})},g=()=>{const e=i(d);if(!e)throw new Error("useTranslationStore must be used within a TranslationProvider");return e},m=()=>{const e=g();return{t:(t,r)=>e.translate(t,r?.params),translate:e.translate,translatePlural:e.translatePlural,formatDate:e.formatDate,formatNumber:e.formatNumber,getTextDirection:e.getTextDirection,isLoading:e.isLoading,error:e.error,locale:e.locale,setLocale:e.setLocale,isRTLLocale:e.isRTLLocale,reloadTranslations:e.reloadTranslations,clearCache:e.clearCache}},p=()=>{const e=g();return{locale:e.locale,setLocale:e.setLocale,supportedLocales:["en","fr","es"],isRTL:e.isRTLLocale(e.locale),direction:e.getTextDirection(""),isRTLLocale:e.isRTLLocale}},S=e=>new y(e);export{y as ContextAdapter,b as TranslationProvider,S as createContextAdapter,p as useLocale,m as useTranslation,g as useTranslationStore};
//# sourceMappingURL=context.esm.js.map