UNPKG

@frank-auth/react

Version:

Flexible and customizable React UI components for Frank Authentication

3 lines (2 loc) 4.24 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const L=require("./defaults.cjs"),s=require("../locales/index.cjs");var d=Object.defineProperty,m=(a,e,t)=>e in a?d(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t,c=(a,e,t)=>m(a,typeof e!="symbol"?e+"":e,t);class f{constructor(e){c(this,"config"),c(this,"currentLocale"),c(this,"loadedMessages",new Map),c(this,"listeners",new Set),this.config={...L.DEFAULT_LOCALIZATION_CONFIG,...e},this.currentLocale=this.config.defaultLocale,this.loadedMessages.set(this.currentLocale,{...s.LOCALE_INFO[this.currentLocale],...this.config.messages})}getCurrentLocale(){return this.currentLocale}getCurrentLocaleMetadata(){return s.LOCALE_INFO[this.currentLocale]}async setLocale(e){this.config.supportedLocales.includes(e)||(console.warn(`Locale ${e} is not supported. Falling back to ${this.config.fallbackLocale}`),e=this.config.fallbackLocale),this.currentLocale=e,this.loadedMessages.has(e)||await this.loadLocaleMessages(e),this.notifyListeners()}t(e,t){const r=this.getCurrentMessages(),o=this.getNestedValue(r,e);return typeof o!="string"?(console.warn(`Translation key "${e}" not found for locale "${this.currentLocale}"`),e):this.interpolate(o,t)}plural(e,t){const{count:r}=t,i=s.LOCALE_INFO[this.currentLocale].pluralRules.select(r);let n;if(r===0&&t.zero)return this.interpolate(t.zero,{count:r});switch(i){case"one":n=t.one||t.other;break;case"two":n=t.two||t.other;break;case"few":n=t.few||t.other;break;case"many":n=t.many||t.other;break;default:n=t.other}return this.interpolate(n,{count:r})}formatDate(e,t){return s.LOCALE_INFO[this.currentLocale],new Intl.DateTimeFormat(this.currentLocale,{...t,...t||{}}).format(e)}formatTime(e,t){return new Intl.DateTimeFormat(this.currentLocale,{timeStyle:"short",...t}).format(e)}formatNumber(e,t){const r=s.LOCALE_INFO[this.currentLocale];return new Intl.NumberFormat(this.currentLocale,{...r.numberFormat,...t}).format(e)}formatCurrency(e,t,r){return new Intl.NumberFormat(this.currentLocale,{style:"currency",currency:t,...r}).format(e)}formatRelativeTime(e,t){const r=new Intl.RelativeTimeFormat(this.currentLocale,{numeric:"auto",...t}),o=new Date,i=e.getTime()-o.getTime(),n=Math.round(i/1e3),l=Math.round(n/60),u=Math.round(l/60),g=Math.round(u/24);return Math.abs(n)<60?r.format(n,"second"):Math.abs(l)<60?r.format(l,"minute"):Math.abs(u)<24?r.format(u,"hour"):r.format(g,"day")}getAvailableLocales(){return this.config.supportedLocales.map(e=>({code:e,name:s.LOCALE_INFO[e].name,nativeName:s.LOCALE_INFO[e].nativeName}))}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}updateConfig(e){this.config={...this.config,...e},e.messages&&this.loadedMessages.set(this.currentLocale,{...s.LOCALE_INFO[this.currentLocale],...this.config.messages})}getCurrentMessages(){return this.loadedMessages.get(this.currentLocale)||s.LOCALE_INFO[this.currentLocale]}getNestedValue(e,t){return t.split(".").reduce((r,o)=>r?.[o],e)}interpolate(e,t){return t?e.replace(/\{\{(\w+)\}\}/g,(r,o)=>{const i=t[o];return i===void 0?r:i instanceof Date?this.formatDate(i):String(i)}):e}async loadLocaleMessages(e){try{const t=s.LOCALE_INFO[e]||s.LOCALE_INFO[this.config.fallbackLocale];this.loadedMessages.set(e,{...t,...this.config.messages})}catch(t){console.error(`Failed to load messages for locale ${e}:`,t);const r=s.LOCALE_INFO[this.config.fallbackLocale];this.loadedMessages.set(e,r)}}notifyListeners(){this.listeners.forEach(e=>e(this.currentLocale))}}function O(a){return new f(a)}function F(a){if(typeof navigator>"u")return"en";const e=[navigator.language,...navigator.languages||[]];for(const t of e){const r=t.split("-")[0];if(a.includes(r))return r}return"en"}function h(a){return s.LOCALE_INFO[a]?.direction||"ltr"}function M(a){return h(a)==="rtl"}function A(a,e){return{t:(t,r)=>a.t(`${e}.${t}`,r),plural:(t,r)=>a.plural(`${e}.${t}`,r)}}exports.DEFAULT_LOCALE_MESSAGES=L.DEFAULT_LOCALE_MESSAGES;exports.DEFAULT_LOCALIZATION_CONFIG=L.DEFAULT_LOCALIZATION_CONFIG;exports.LocalizationManager=f;exports.createLocalizationManager=O;exports.createTranslationNamespace=A;exports.detectBrowserLocale=F;exports.getLocaleDirection=h;exports.isRTL=M; //# sourceMappingURL=localization.cjs.map