@loveholidays/phrasebook
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -1,2 +0,2 @@ | ||
import N,{Fragment as E}from"react";import h,{createContext as y,useContext as x,useMemo as L}from"react";var u="";var F=(e,n)=>{if(typeof n=="number")try{return new Intl.NumberFormat(e).format(n)}catch{return n}return n},C=({locale:e,namespaces:n,key:t,args:o={}})=>{var d,g;let r=(d=o.ns)!=null?d:u,s=t.split("."),a=s[s.length-1],i=s.slice(0,-1).reduce((m,T)=>m==null?void 0:m[T],n[r]);if(typeof i!="object")throw new Error(`Missing translation: "${t}"`);let c;o.context?c=o.context:typeof o.count!="undefined"&&o.count!==1&&o.count!==-1&&(c="plural");let p=(g=i[c?`${a}_${c}`:a])!=null?g:i[a];if(typeof p!="string")throw new Error(`Missing translation: "${t}" with suffix: "${c}"`);return Object.entries(o).reduce((m,[T,A])=>m.replace(new RegExp(`{{\\s*${T}\\s*}}`,"g"),String(F(e,A))),p)};var f=y({locale:"",namespaces:{},t:()=>""}),D=(e={},n={},t)=>({...e,...n,...t&&{[u]:t}}),P=e=>({t:e?(n,t)=>x(f).t(n,{ns:e,...t}):x(f).t}),_=({locale:e,namespaces:n,translations:t,children:o})=>{let{namespaces:r}=x(f),s=L(()=>D(r,n,t),[r,n,t]);return h.createElement(f.Provider,{value:{locale:e,namespaces:s,t:(a,i)=>C({locale:e,namespaces:s,key:a,args:i})}},o)};var l={singleComponent:/^<(\d+)>$/,childrenComponent:/^<(\d+)>([^<]*)<\/\d+>$/,extractComponents:/(<\d+>[^<]*<\/\d+>|<\d+>)/},R=(e,n)=>e.map(t=>{if(l.singleComponent.test(t)){let[,o]=t.match(l.singleComponent),r=Number(o)-1;return n[r]}if(l.childrenComponent.test(t)){let[,o,r]=t.match(l.childrenComponent),s=Number(o)-1,a=n[s];return typeof a=="function"?a(r):a}return t}),$=({translationKey:e,namespace:n,params:t={},components:o=[]})=>{let{t:r}=P(n),a=r(e,t).split(l.extractComponents),i=R(a,o);return N.createElement(E,null,i.map((c,p)=>N.createElement(E,{key:p},c)))};export{$ as Translation,f as TranslationContext,_ as TranslationProvider,P as useTranslation}; | ||
import F,{Fragment as R}from"react";import h,{createContext as D,useContext as g,useMemo as _}from"react";var T="";var L=(e,n)=>{if(typeof n=="number")try{return new Intl.NumberFormat(e).format(n)}catch{return n}return n},P=({locale:e,namespaces:n,key:t,onError:o,args:r={}})=>{var E,d;let s=(E=r.ns)!=null?E:T,a=t.split("."),i=a[a.length-1],c=a.slice(0,-1).reduce((m,u)=>m==null?void 0:m[u],n[s]);if(typeof c!="object")throw new Error(`Missing translation: "${t}"`);let l;r.context?l=r.context:typeof r.count!="undefined"&&r.count!==1&&r.count!==-1&&(l="plural");let x=(d=c[l?`${i}_${l}`:i])!=null?d:c[i];if(typeof x!="string")throw new Error(`Missing translation: "${t}" with suffix: "${l}"`);let{context:M,ns:$,...y}=r;return Object.entries(y).reduce((m,[u,C])=>{let N=new RegExp(`{{\\s*${u}\\s*}}`,"g");o&&!N.test(m)&&o("REPLACE_ARGUMENT_NOT_FOUND",{key:t,argumentName:u,value:C});let b=String(L(e,C));return m.replace(N,b)},x)};var f=D({locale:"",namespaces:{},t:()=>""}),O=(e={},n={},t)=>({...e,...n,...t&&{[T]:t}}),A=e=>({t:e?(n,t)=>g(f).t(n,{ns:e,...t}):g(f).t}),z=({locale:e,namespaces:n,translations:t,onError:o,children:r})=>{let{namespaces:s}=g(f),a=_(()=>O(s,n,t),[s,n,t]);return h.createElement(f.Provider,{value:{locale:e,namespaces:a,t:(i,c)=>P({locale:e,namespaces:a,key:i,args:c,onError:o})}},r)};var p={singleComponent:/^<(\d+)>$/,childrenComponent:/^<(\d+)>([^<]*)<\/\d+>$/,extractComponents:/(<\d+>[^<]*<\/\d+>|<\d+>)/},U=(e,n)=>e.map(t=>{if(p.singleComponent.test(t)){let[,o]=t.match(p.singleComponent),r=Number(o)-1;return n[r]}if(p.childrenComponent.test(t)){let[,o,r]=t.match(p.childrenComponent),s=Number(o)-1,a=n[s];return typeof a=="function"?a(r):a}return t}),V=({translationKey:e,namespace:n,params:t={},components:o=[]})=>{let{t:r}=A(n),a=r(e,t).split(p.extractComponents),i=U(a,o);return F.createElement(R,null,i.map((c,l)=>F.createElement(R,{key:l},c)))};export{V as Translation,f as TranslationContext,z as TranslationProvider,A as useTranslation}; | ||
//# sourceMappingURL=index.js.map |
import { Locale, Namespaces, TranslationArguments } from './types'; | ||
import type { OnErrorCallback } from './TranslationProvider'; | ||
interface ProcessTranslationParams { | ||
@@ -6,5 +7,6 @@ locale: Locale; | ||
key: string; | ||
onError?: OnErrorCallback; | ||
args?: TranslationArguments; | ||
} | ||
export declare const processTranslation: ({ locale, namespaces, key, args, }: ProcessTranslationParams) => string; | ||
export declare const processTranslation: ({ locale, namespaces, key, onError, args, }: ProcessTranslationParams) => string; | ||
export {}; |
import React from 'react'; | ||
import type { Namespaces, Locale, TFunction, TranslationData } from './types'; | ||
import type { Namespaces, Locale, TFunction, TranslationData, TranslationArgumentValue } from './types'; | ||
export interface TranslationContextValue { | ||
@@ -9,2 +9,8 @@ locale: Locale; | ||
export declare const TranslationContext: React.Context<TranslationContextValue>; | ||
export interface ReplaceArgumentErrorParams { | ||
key: string; | ||
argumentName: string; | ||
value: TranslationArgumentValue; | ||
} | ||
export declare type OnErrorCallback = ((errorType: 'REPLACE_ARGUMENT_NOT_FOUND', params: ReplaceArgumentErrorParams) => void); | ||
interface TranslationProviderProps { | ||
@@ -14,2 +20,3 @@ locale: Locale; | ||
translations?: TranslationData; | ||
onError?: OnErrorCallback; | ||
} | ||
@@ -20,3 +27,12 @@ export interface UseTranslationReturnValue { | ||
export declare const useTranslation: (namespace?: string | undefined) => UseTranslationReturnValue; | ||
/** | ||
* TranslationProvider | ||
* Provider used to create the localisation context. | ||
* @param {object} props | ||
* @param {string} props.locale String is used for locale specific formatting. | ||
* @param {object} props.namespaces Namespaced translations where the keys are the names of the namespaces and the values are the translations for the given namespace. | ||
* @param {object} props.translations Translation for default locale. | ||
* @param {function} props.onError Callback could be used to track the error during translation processing. | ||
* */ | ||
export declare const TranslationProvider: React.FC<TranslationProviderProps>; | ||
export {}; |
{ | ||
"name": "@loveholidays/phrasebook", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A lightweight translation library for React/Preact projects with a similar interface to react-i18next", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
@@ -45,2 +45,5 @@ # Phrasebook | ||
translations={translations} | ||
onError={(errorType, data) => { | ||
const { key, argumentName, value } = data; | ||
}} | ||
> | ||
@@ -47,0 +50,0 @@ // ... |
Sorry, the diff of this file is not supported yet
31159
84
172