@loveholidays/phrasebook
Advanced tools
Comparing version 2.1.3 to 2.2.0
@@ -1,2 +0,2 @@ | ||
import h,{Fragment as y}from"react";import _,{createContext as D,useContext as A,useMemo as $}from"react";var f="";var L=(e,t)=>{if(typeof t=="number")try{return new Intl.NumberFormat(e).format(t)}catch{return t}return t},x="count",N=({locale:e,namespaces:t,key:n,onError:a,args:r={}})=>{var C,P;let c=(C=r.ns)!=null?C:f,o=n.split("."),l=o[o.length-1],m=o.slice(0,-1).reduce((s,p)=>s==null?void 0:s[p],t[c]);if(typeof m!="object")throw new Error(`Missing translation: "${n}"`);let i=r.context;typeof r[x]!="undefined"&&r[x]!==1&&r[x]!==-1&&(i=i?`${i}_plural`:"plural");let g=(P=m[i?`${l}_${i}`:l])!=null?P:m[l];if(typeof g!="string")throw new Error(`Missing translation: "${n}" with suffix: "${i}"`);let{context:S,ns:U,...d}=r;if(a){let s=g.match(/{{\s*\w*\s*}}/g);s==null||s.forEach(p=>{let T=p.replace(/({{\s*|\s*}})/g,"");d[T]===void 0&&a("REPLACE_ARGUMENT_NOT_PASSED",{key:n,argumentName:T})})}return Object.entries(d).reduce((s,[p,T])=>{let F=new RegExp(`{{\\s*${p}\\s*}}`,"g"),b=String(L(e,T));return s.replace(F,b)},g)};var E=D({locale:"",namespaces:{},t:()=>""}),w=(e={},t={},n)=>({...e,...t,...n&&{[f]:n}}),R=e=>{let{t}=A(E);return{t:e?(n,a)=>t(n,{ns:e,...a}):t}},I=({locale:e,namespaces:t,translations:n,onError:a,children:r})=>{let{namespaces:c}=A(E),o=$(()=>w(c,t,n),[c,t,n]);return _.createElement(E.Provider,{value:{locale:e,namespaces:o,t:(l,m)=>N({locale:e,namespaces:o,key:l,args:m,onError:a})}},r)};var u={singleComponent:/^<(\d+)>$/,childrenComponent:/^<(\d+)>([^<]*)<\/\d+>$/,extractComponents:/(<\d+>[^<]*<\/\d+>|<\d+>)/},M=(e,t)=>e.map(n=>{if(u.singleComponent.test(n)){let[,a]=n.match(u.singleComponent),r=Number(a)-1;return t[r]}if(u.childrenComponent.test(n)){let[,a,r]=n.match(u.childrenComponent),c=Number(a)-1,o=t[c];return typeof o=="function"?o(r):o}return n}),O=({translationKey:e,namespace:t,params:n={},components:a=[]})=>{let{t:r}=R(t),o=r(e,n).split(u.extractComponents),l=M(o,a);return h.createElement(y,null,l.map((m,i)=>h.createElement(y,{key:i},m)))};export{O as Translation,E as TranslationContext,I as TranslationProvider,R as useTranslation}; | ||
import h,{Fragment as L}from"react";import _,{createContext as D,useContext as N,useMemo as v}from"react";var f="";var b=(e,t)=>{if(typeof t=="number")try{return new Intl.NumberFormat(e).format(t)}catch{return t}return t},g="count",A=({locale:e,namespaces:t,key:n,onError:o,args:r={}})=>{var C,P;let l=(C=r.ns)!=null?C:f,a=n.split("."),m=a[a.length-1],s=a.slice(0,-1).reduce((c,p)=>c==null?void 0:c[p],t[l]);if(typeof s!="object")throw new Error(`Missing translation: "${n}"`);let i=r.context;typeof r[g]!="undefined"&&r[g]!==1&&r[g]!==-1&&(i=i?`${i}_plural`:"plural");let d=(P=s[i?`${m}_${i}`:m])!=null?P:s[m];if(typeof d!="string")throw new Error(`Missing translation: "${n}" with suffix: "${i}"`);let{context:O,ns:S,...E}=r;if(o){let c=d.match(/{{\s*\w*\s*}}/g);c==null||c.forEach(p=>{let T=p.replace(/({{\s*|\s*}})/g,"");E[T]===void 0&&o("REPLACE_ARGUMENT_NOT_PASSED",{key:n,argumentName:T})})}return Object.entries(E).reduce((c,[p,T])=>{let y=new RegExp(`{{\\s*${p}\\s*}}`,"g"),F=String(b(e,T));return c.replace(y,F)},d)};var x=D({locale:"",namespaces:{},t:()=>""}),$=(e={},t={},n)=>({...e,...t,...n&&{[f]:n}}),R=e=>{let{t}=N(x);return{t:e?(n,o)=>t(n,{ns:e,...o}):t}},I=({locale:e,namespaces:t,translations:n,onError:o,children:r})=>{let{namespaces:l}=N(x),a=v(()=>$(l,t,n),[l,t,n]);return _.createElement(x.Provider,{value:{locale:e,namespaces:a,t:(m,s)=>{let i=s!=null&&s.overrideLocale?s.overrideLocale:e;return A({namespaces:a,locale:i,key:m,args:s,onError:o})}}},r)};var u={singleComponent:/^<(\d+)>$/,childrenComponent:/^<(\d+)>([^<]*)<\/\d+>$/,extractComponents:/(<\d+>[^<]*<\/\d+>|<\d+>)/},w=(e,t)=>e.map(n=>{if(u.singleComponent.test(n)){let[,o]=n.match(u.singleComponent),r=Number(o)-1;return t[r]}if(u.childrenComponent.test(n)){let[,o,r]=n.match(u.childrenComponent),l=Number(o)-1,a=t[l];return typeof a=="function"?a(r):a}return n}),M=({translationKey:e,namespace:t,params:n={},components:o=[]})=>{let{t:r}=R(t),a=r(e,n).split(u.extractComponents),m=w(a,o);return h.createElement(L,null,m.map((s,i)=>h.createElement(L,{key:i},s)))};export{M as Translation,x as TranslationContext,I as TranslationProvider,R as useTranslation}; | ||
//# sourceMappingURL=index.js.map |
@@ -9,2 +9,3 @@ export interface TranslationData { | ||
context?: string; | ||
overrideLocale?: Locale; | ||
[name: string]: TranslationArgumentValue; | ||
@@ -11,0 +12,0 @@ }; |
{ | ||
"name": "@loveholidays/phrasebook", | ||
"version": "2.1.3", | ||
"version": "2.2.0", | ||
"description": "A lightweight translation library for React/Preact projects with a similar interface to react-i18next", | ||
@@ -21,3 +21,4 @@ "sideEffects": false, | ||
"lint:info": "eslint --print-config .eslintrc.js", | ||
"changeset": "changeset" | ||
"changeset": "changeset", | ||
"yalc:publish": "npm run build && npm pack && npx -y yalc publish --push" | ||
}, | ||
@@ -24,0 +25,0 @@ "repository": { |
@@ -34,2 +34,30 @@ # Phrasebook | ||
## Using a dev version of Phrasebook in a dev app | ||
We can use yalc for this, which creates a local store that we can publish and pull dependencies from. | ||
To publish Phrasebook to the local store: | ||
```bash | ||
npx --yes yalc publish | ||
``` | ||
In your target repo (eg. sunrise), update the dependency to local Phrasebook: | ||
```bash | ||
npx yalc add @loveholidays/phrasebook | ||
``` | ||
If you make a change in your local Phrasebook repo, update the local store and the target repo: | ||
```bash | ||
npx yalc publish --push | ||
``` | ||
To reset the dependency of Phraseboook, in your target repo run: | ||
```bash | ||
npx yalc remove @loveholidays/phrasebook | ||
``` | ||
## Usage | ||
@@ -36,0 +64,0 @@ |
Sorry, the diff of this file is not supported yet
32752
85
200