UNPKG

use-intl

Version:

Internationalization (i18n) for React

2 lines (1 loc) 5.95 kB
import{IntlMessageFormat as e}from"intl-messageformat";import{isValidElement as t,cloneElement as r}from"react";import{memoize as n,strategies as o}from"@formatjs/fast-memoize";class a extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),this.code=e,t&&(this.originalMessage=t)}}var s=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}(s||{});function i(...e){return e.filter(Boolean).join(".")}function u(e){return i(e.namespace,e.key)}function c(e){console.error(e)}function m(){return{dateTime:{},number:{},message:{},relativeTime:{},pluralRules:{},list:{},displayNames:{}}}function f(e,t){return n(e,{cache:(r=t,{create:()=>({get:e=>r[e],set(e,t){r[e]=t}})}),strategy:o.variadic});var r}function l(e,t){return f(((...t)=>new e(...t)),t)}function g(e){return{getDateTimeFormat:l(Intl.DateTimeFormat,e.dateTime),getNumberFormat:l(Intl.NumberFormat,e.number),getPluralRules:l(Intl.PluralRules,e.pluralRules),getRelativeTimeFormat:l(Intl.RelativeTimeFormat,e.relativeTime),getListFormat:l(Intl.ListFormat,e.list),getDisplayNames:l(Intl.DisplayNames,e.displayNames)}}function I(e,t,r,n){const o=i(n,r);if(!t)throw new Error(o);let a=t;return r.split(".").forEach((t=>{const r=a[t];if(null==t||null==r)throw new Error(o+` (${e})`);a=r})),a}function E(n){const o=function(e,t,r,n=c){try{if(!t)throw new Error(void 0);const n=r?I(e,t,r):t;if(!n)throw new Error(r);return n}catch(e){const t=new a(s.MISSING_MESSAGE,e.message);return n(t),t}}(n.locale,n.messages,n.namespace,n.onError);return function({cache:n,formats:o,formatters:i,getMessageFallback:c=u,locale:m,messagesOrError:l,namespace:g,onError:E,timeZone:S}){const T=l instanceof a;function N(e,t,r){const n=new a(t,r);return E(n),c({error:n,key:e,namespace:g})}function y(a,u,E){if(T)return c({error:l,key:a,namespace:g});const y=l;let A,M;try{A=I(m,y,a,g)}catch(e){return N(a,s.MISSING_MESSAGE,e.message)}if("object"==typeof A){let e,t;return e=Array.isArray(A)?s.INVALID_MESSAGE:s.INSUFFICIENT_PATH,N(a,e,t)}const F=function(e,t){return t?void 0:e}(A,u);if(F)return F;i.getMessageFormat||(i.getMessageFormat=function(t,r){return f(((...t)=>new e(t[0],t[1],t[2],{formatters:r,...t[3]})),t.message)}(n,i));try{M=i.getMessageFormat(A,m,function(t,r,n){const o=e.formats.date,a=e.formats.time,s={...t?.dateTime,...r?.dateTime},i={date:{...o,...s},time:{...a,...s},number:{...t?.number,...r?.number}};return n&&["date","time"].forEach((e=>{const t=i[e];for(const[e,r]of Object.entries(t))t[e]={timeZone:n,...r}})),i}(o,E,S),{formatters:{...i,getDateTimeFormat:(e,t)=>i.getDateTimeFormat(e,{timeZone:S,...t})}})}catch(e){const t=e;return N(a,s.INVALID_MESSAGE,t.message)}try{const e=M.format(u?function(e){const n={};return Object.keys(e).forEach((o=>{let a=0;const s=e[o];let i;i="function"==typeof s?e=>{const n=s(e);return t(n)?r(n,{key:o+a++}):n}:s,n[o]=i})),n}(u):u);if(null==e)throw new Error(void 0);return t(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(e){return N(a,s.FORMATTING_ERROR,e.message)}}function A(e,t,r){const n=y(e,t,r);return"string"!=typeof n?N(e,s.INVALID_MESSAGE,void 0):n}return A.rich=y,A.markup=(e,t,r)=>y(e,t,r),A.raw=e=>{if(T)return c({error:l,key:e,namespace:g});const t=l;try{return I(m,t,e,g)}catch(t){return N(e,s.MISSING_MESSAGE,t.message)}},A.has=e=>{if(T)return!1;try{return I(m,l,e,g),!0}catch{return!1}},A}({...n,messagesOrError:o})}function S(e,t){return e===t?void 0:e.slice((t+".").length)}const T=3600,N=86400,y=7*N,A=2628e3,M=7884e3,F=365*N,R={second:1,seconds:1,minute:60,minutes:60,hour:T,hours:T,day:N,days:N,week:y,weeks:y,month:A,months:A,quarter:M,quarters:M,year:F,years:F};function d(e){const{_cache:t=m(),_formatters:r=g(t),formats:n,locale:o,onError:i=c,timeZone:u}=e;function f(e){return e?.timeZone||(u?e={...e,timeZone:u}:i(new a(s.ENVIRONMENT_FALLBACK,void 0))),e}function l(e,t,r,n,o){let u;try{u=function(e,t,r){let n;if("string"==typeof t){const r=t;if(n=e?.[r],!n){const e=new a(s.MISSING_FORMAT,void 0);throw i(e),e}}else n=t;return r&&(n={...n,...r}),n}(r,e,t)}catch{return o()}try{return n(u)}catch(e){return i(new a(s.FORMATTING_ERROR,e.message)),o()}}function I(e,t,a){return l(t,a,n?.dateTime,(t=>(t=f(t),r.getDateTimeFormat(o,t).format(e))),(()=>String(e)))}function E(){return e.now?e.now:(i(new a(s.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:I,number:function(e,t,a){return l(t,a,n?.number,(t=>r.getNumberFormat(o,t).format(e)),(()=>String(e)))},relativeTime:function(e,t){try{let n,a;const s={};t instanceof Date||"number"==typeof t?n=new Date(t):t&&(n=null!=t.now?new Date(t.now):E(),a=t.unit,s.style=t.style,s.numberingSystem=t.numberingSystem),n||(n=E());const i=(new Date(e).getTime()-n.getTime())/1e3;a||(a=function(e){const t=Math.abs(e);return t<60?"second":t<T?"minute":t<N?"hour":t<y?"day":t<A?"week":t<F?"month":"year"}(i)),s.numeric="second"===a?"auto":"always";const u=function(e,t){return Math.round(e/R[t])}(i,a);return r.getRelativeTimeFormat(o,s).format(u,a)}catch(t){return i(new a(s.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,t,a){const s=[],i=new Map;let u=0;for(const t of e){let e;"object"==typeof t?(e=String(u),i.set(e,t)):e=String(t),s.push(e),u++}return l(t,a,n?.list,(e=>{const t=r.getListFormat(o,e).formatToParts(s).map((e=>"literal"===e.type?e.value:i.get(e.value)||e.value));return i.size>0?t:t.join("")}),(()=>String(e)))},dateTimeRange:function(e,t,a,s){return l(a,s,n?.dateTime,(n=>(n=f(n),r.getDateTimeFormat(o,n).formatRange(e,t))),(()=>[I(e),I(t)].join(" – ")))}}}function h({formats:e,getMessageFallback:t,messages:r,onError:n,...o}){return{...o,formats:e||void 0,messages:r||void 0,onError:n||c,getMessageFallback:t||u}}export{a as I,s as a,g as b,d as c,m as d,E as e,u as f,c as g,h as i,S as r};