@re-flex/i18n
Version:
Re-flex UI Lozalization Package
3 lines (2 loc) • 2.18 kB
JavaScript
import n,{createContext as t,useState as e,useLayoutEffect as a,useContext as r}from"react";import i from"@re-flex/object-path";var s=new(/*#__PURE__*/function(){function n(){this.updater=void 0,this.httpConfig=void 0,this.browserLanguage=navigator.languages&&navigator.languages[0]||navigator.language,this.initialLanguage="en",this.currentLanguage="en",this.resources=new i({}),this.languages=void 0,this.namespaces=void 0,this.currentLanguage="en",this.initialLanguage="en"}var t=n.prototype;return t.httpBackendAdapter=function(){var n=this;if(this.httpConfig){var t=this.namespaces.map(function(t){var e={lng:n.currentLanguage,ns:t};return{namespace:t,path:n.httpConfig.path.replace(/{{\s*\w+\s*}}/gim,function(n){var t=n.replace(/{{\s*|\s*}}/gim,"");return e[t]})}});Promise.all(t.map(function(t){var e,a=t.namespace;return fetch(t.path,{headers:null==(e=n.httpConfig)?void 0:e.headers}).then(function(n){return n.json()}).then(function(n){var t;return(t={})[a]=n,t})})).then(function(t){n.resources=new i(Object.assign.apply(Object,[{}].concat(t))),n.updater&&n.updater()})}else console.warn("HTTP config is not assigned")},t.init=function(n){var t=n.initialLanguage,e=n.httpConfig,a=n.resources,r=n.languages,s=n.namespaces;a&&(this.resources=new i(a)),r&&(this.languages=r),s&&(this.namespaces=s),t&&(this.initialLanguage=t,this.currentLanguage=t),e&&(this.httpConfig=e,this.httpBackendAdapter())},t.onLoadNamepaces=function(n){},t.onChangeLanguage=function(n){},t.changeLanguage=function(n){this.currentLanguage=n,this.httpBackendAdapter(),this.updater&&this.updater()},t.translate=function(n,t){var e=this.resources.get([this.currentLanguage,n].join("."));return void 0===e?n:(t&&(e=e.replace(/{{\s*\w+\s*}}/gim,function(n){var e=n.replace(/{{\s*|\s*}}/gim,"");return t[e]})),e)},n}()),u=t(null),o=u.Provider,c=function(t){var r=t.children,i=e(0),u=i[0],c=i[1];return a(function(){s.updater=function(){return c(function(n){return n+1})}},[]),n.createElement(o,{value:{version:u,i18n:s}},r)},g=function(){var n=r(u).i18n;return{t:function(t,e){return n.translate(t,e)},i18n:n}};export{s as I18n,c as I18nProvider,g as useTranslate};
//# sourceMappingURL=i18n.module.js.map