@logistically/i18n-react-core
Version:
React integration for @logistically/i18n with state management agnostic design
12 lines (11 loc) • 9.74 kB
JavaScript
"use strict";var e,t=require("react"),r=require("@logistically/i18n"),a={exports:{}},i={};var n,s={};
/**
* @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?a.exports=function(){if(e)return i;e=1;var t=Symbol.for("react.transitional.element"),r=Symbol.for("react.fragment");function a(e,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:t,type:e,key:i,ref:void 0!==r?r:null,props:a}}return i.Fragment=r,i.jsx=a,i.jsxs=a,i}():a.exports=(n||(n=1,"production"!==process.env.NODE_ENV&&function(){function e(t){if(null==t)return null;if("function"==typeof t)return t.$$typeof===x?null:t.displayName||t.name||null;if("string"==typeof t)return t;switch(t){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 t)switch(t.tag,t.$$typeof){case h:return"Portal";case m:return(t.displayName||"Context")+".Provider";case p:return(t._context.displayName||"Context")+".Consumer";case S:var r=t.render;return(t=t.displayName)||(t=""!==(t=r.displayName||r.name||"")?"ForwardRef("+t+")":"ForwardRef"),t;case T:return null!==(r=t.displayName||null)?r:e(t.type)||"Memo";case _:r=t._payload,t=t._init;try{return e(t(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(t){if(t===y)return"<>";if("object"==typeof t&&null!==t&&t.$$typeof===_)return"<...>";try{var r=e(t);return r?"<"+r+">":"<...>"}catch(e){return"<...>"}}function n(){return Error("react-stack-top-frame")}function o(){var t=e(this.type);return P[t]||(P[t]=!0),void 0!==(t=this.props.ref)?t:null}function l(t,r,i,n,s,l,f,h){var y,b=r.children;if(void 0!==b)if(n){if(O(b)){for(n=0;n<b.length;n++)c(b[n]);Object.freeze&&Object.freeze(b)}}else c(b);if(j.call(r,"key")){b=e(t);var g=Object.keys(r).filter(function(e){return"key"!==e});n=0<g.length?"{key: someKey, "+g.join(": ..., ")+": ...}":"{key: someKey}",R[b+n]||(g=0<g.length?"{"+g.join(": ..., ")+": ...}":"{}",R[b+n]=!0)}if(b=null,void 0!==i&&(a(i),b=""+i),function(e){if(j.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 p in i={},r)"key"!==p&&(i[p]=r[p]);else i=r;return b&&function(e){function t(){u||(u=!0)}t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}(i,"function"==typeof t&&(t.displayName||t.name)),function(e,t,r,a,i,n,s,l){return r=n.ref,e={$$typeof:d,type:e,key:t,props:n,_owner:i},null!==(void 0!==r?r:null)?Object.defineProperty(e,"ref",{enumerable:!1,get:o}):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:s}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:l}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}(t,b,l,0,null===(y=k.A)?null:y.getOwner(),i,f,h)}function c(e){"object"==typeof e&&null!==e&&e.$$typeof===d&&e._store&&(e._store.validated=1)}var u,f=t,d=Symbol.for("react.transitional.element"),h=Symbol.for("react.portal"),y=Symbol.for("react.fragment"),b=Symbol.for("react.strict_mode"),g=Symbol.for("react.profiler"),p=Symbol.for("react.consumer"),m=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"),x=Symbol.for("react.client.reference"),k=f.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,j=Object.prototype.hasOwnProperty,O=Array.isArray,N=console.createTask?console.createTask:function(){return null},P={},A=(f={react_stack_bottom_frame:function(e){return e()}}).react_stack_bottom_frame.bind(f,n)(),C=N(i(n)),R={};s.Fragment=y,s.jsx=function(e,t,r,a,n){var s=1e4>k.recentlyCreatedOwnerStacks++;return l(e,t,r,!1,0,n,s?Error("react-stack-top-frame"):A,s?N(i(e)):C)},s.jsxs=function(e,t,r,a,n){var s=1e4>k.recentlyCreatedOwnerStacks++;return l(e,t,r,!0,0,n,s?Error("react-stack-top-frame"):A,s?N(i(e)):C)}}()),s);var o=a.exports;class l{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 r.TranslationService(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 c=t.createContext(null);class u{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 f=()=>{const e=t.useContext(c);if(!e)throw new Error("useTranslationStore must be used within a TranslationProvider");return e};exports.ContextAdapter=u,exports.TranslationProvider=({children:e,config:r,initialLocale:a})=>{const[i,n]=t.useState(null),[s,f]=t.useState(!1);return t.useEffect(()=>{const e=new l(r),t=new u(e),i={...t.getState(),...t.getActions()};n(i);const s=t.subscribe(e=>{const r=t.getActions();n({...e,...r})});return(async()=>{try{await e.initialize(a);const r=t.getState(),i=t.getActions();n({...r,...i}),f(!0)}catch(e){n({...t.getState(),...t.getActions()}),f(!0)}})(),()=>{s(),t.destroy()}},[r,a]),i?o.jsx(c.Provider,{value:i,children:e}):o.jsx(c.Provider,{value:null,children:o.jsx("div",{children:"Loading..."})})},exports.createContextAdapter=e=>new u(e),exports.useLocale=()=>{const e=f();return{locale:e.locale,setLocale:e.setLocale,supportedLocales:["en","fr","es"],isRTL:e.isRTLLocale(e.locale),direction:e.getTextDirection(""),isRTLLocale:e.isRTLLocale}},exports.useTranslation=()=>{const e=f();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}},exports.useTranslationStore=f;
//# sourceMappingURL=context.js.map