@tolgee/react 5.10.2 → 5.11.1-prerelease.cb4bc75b.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"tolgee-react.umd.min.js","sources":["../src/TolgeeProvider.tsx","../src/GlobalContextPlugin.tsx","../src/useTolgeeContext.ts","../src/hooks.ts","../src/useTranslateInternal.ts","../src/tagsTools.tsx","../src/T.tsx","../src/useTolgee.ts","../src/useTolgeeSSR.ts","../src/useTranslate.ts"],"sourcesContent":["import React, { Suspense, useEffect, useState } from 'react';\nimport { TolgeeInstance } from '@tolgee/web';\nimport { ReactOptions, TolgeeReactContext } from './types';\n\nexport const DEFAULT_REACT_OPTIONS: ReactOptions = {\n useSuspense: true,\n};\n\nexport const TolgeeProviderContext = React.createContext<\n TolgeeReactContext | undefined\n>(undefined);\n\nexport interface TolgeeProviderProps {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n}\n\nexport const TolgeeProvider: React.FC<TolgeeProviderProps> = ({\n tolgee,\n options,\n children,\n fallback,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n useEffect(() => {\n tolgee\n .run()\n .catch((e) => {\n // eslint-disable-next-line no-console\n console.error(e);\n })\n .finally(() => {\n setLoading(false);\n });\n }, [tolgee]);\n\n const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options };\n\n if (optionsWithDefault.useSuspense) {\n return (\n <TolgeeProviderContext.Provider\n value={{ tolgee, options: optionsWithDefault }}\n >\n {loading ? (\n fallback\n ) : (\n <Suspense fallback={fallback || null}>{children}</Suspense>\n )}\n </TolgeeProviderContext.Provider>\n );\n }\n\n return (\n <TolgeeProviderContext.Provider\n value={{ tolgee, options: optionsWithDefault }}\n >\n {loading ? fallback : children}\n </TolgeeProviderContext.Provider>\n );\n};\n","import type { TolgeePlugin } from '@tolgee/web';\nimport { DEFAULT_REACT_OPTIONS } from './TolgeeProvider';\nimport type { ReactOptions, TolgeeReactContext } from './types';\n\nlet globalContext: TolgeeReactContext | undefined;\n\nexport const GlobalContextPlugin =\n (options?: Partial<ReactOptions>): TolgeePlugin =>\n (tolgee) => {\n globalContext = {\n tolgee,\n options: { ...DEFAULT_REACT_OPTIONS, ...options },\n };\n return tolgee;\n };\n\nexport function getGlobalContext() {\n return globalContext;\n}\n","import { useContext } from 'react';\nimport { getGlobalContext } from './GlobalContextPlugin';\nimport { TolgeeProviderContext } from './TolgeeProvider';\n\nexport const useTolgeeContext = () => {\n const context = useContext(TolgeeProviderContext) || getGlobalContext();\n if (!context) {\n throw new Error(\n \"Couldn't find tolgee instance, did you forgot to use `TolgeeProvider`?\"\n );\n }\n return context;\n};\n","import { useCallback, useState } from 'react';\n\nexport const useRerender = () => {\n const [instance, setCounter] = useState(0);\n\n const rerender = useCallback(() => {\n setCounter((num) => num + 1);\n }, [setCounter]);\n return { instance, rerender };\n};\n","import { useCallback, useEffect, useRef } from 'react';\nimport {\n SubscriptionSelective,\n TranslateProps,\n NsFallback,\n getFallbackArray,\n getFallback,\n} from '@tolgee/web';\n\nimport { useTolgeeContext } from './useTolgeeContext';\nimport { ReactOptions } from './types';\nimport { useRerender } from './hooks';\n\nexport const useTranslateInternal = (\n ns?: NsFallback,\n options?: ReactOptions\n) => {\n const { tolgee, options: defaultOptions } = useTolgeeContext();\n const namespaces = getFallback(ns);\n const namespacesJoined = getFallbackArray(namespaces).join(':');\n\n const currentOptions = {\n ...defaultOptions,\n ...options,\n };\n\n // dummy state to enable re-rendering\n const { rerender, instance } = useRerender();\n\n const subscriptionRef = useRef<SubscriptionSelective>();\n\n const subscriptionQueue = useRef([] as NsFallback[]);\n subscriptionQueue.current = [];\n\n const subscribeToNs = (ns: NsFallback) => {\n subscriptionQueue.current.push(ns);\n subscriptionRef.current?.subscribeNs(ns);\n };\n\n const isLoaded = tolgee.isLoaded(namespaces);\n\n useEffect(() => {\n const subscription = tolgee.onNsUpdate(rerender);\n subscriptionRef.current = subscription;\n if (!isLoaded) {\n subscription.subscribeNs(namespaces);\n }\n subscriptionQueue.current.forEach((ns) => {\n subscription!.subscribeNs(ns);\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [isLoaded, namespacesJoined, tolgee]);\n\n useEffect(() => {\n tolgee.addActiveNs(namespaces);\n return () => tolgee.removeActiveNs(namespaces);\n }, [namespacesJoined, tolgee]);\n\n const t = useCallback(\n (props: TranslateProps<any>) => {\n const fallbackNs = props.ns ?? namespaces?.[0];\n subscribeToNs(fallbackNs);\n return tolgee.t({ ...props, ns: fallbackNs }) as any;\n },\n [tolgee, instance]\n );\n\n if (currentOptions.useSuspense && !isLoaded) {\n throw tolgee.addActiveNs(namespaces, true);\n }\n\n return { t, isLoading: !isLoaded };\n};\n","import { TranslateParams } from '@tolgee/web';\nimport React from 'react';\n\nimport { ParamsTags } from './types';\n\nexport const wrapTagHandlers = (\n params: TranslateParams<ParamsTags> | undefined\n) => {\n if (!params) {\n return undefined;\n }\n\n const result: any = {};\n\n Object.entries(params || {}).forEach(([key, value]) => {\n if (typeof value === 'function') {\n result[key] = (chunk: any) => {\n return value(addReactKeys(chunk));\n };\n } else if (React.isValidElement(value as any)) {\n const el = value as React.ReactElement;\n result[key] = (chunk: any) => {\n return el.props.children === undefined && chunk?.length\n ? React.cloneElement(el, {}, addReactKeys(chunk))\n : React.cloneElement(el);\n };\n } else {\n result[key] = value;\n }\n });\n\n return result;\n};\n\nexport const addReactKeys = (\n val: React.ReactNode | React.ReactNode[] | undefined\n) => {\n if (Array.isArray(val)) {\n return React.Children.toArray(val);\n } else {\n return val;\n }\n};\n","import { NsType, TranslateParams, TranslationKey } from '@tolgee/web';\nimport React from 'react';\nimport { addReactKeys, wrapTagHandlers } from './tagsTools';\nimport { ParamsTags } from './types';\n\nimport { useTranslateInternal } from './useTranslateInternal';\n\ninterface PropsBase {\n params?: TranslateParams<ParamsTags>;\n noWrap?: boolean;\n ns?: NsType;\n defaultValue?: string;\n language?: string;\n}\n\ninterface PropsWithKeyName extends PropsBase {\n children?: string;\n keyName: TranslationKey;\n}\n\ninterface PropsWithoutKeyName extends PropsBase {\n children: TranslationKey;\n}\n\nexport type TProps = PropsWithKeyName | PropsWithoutKeyName;\n\ninterface TInterface {\n (props: TProps): JSX.Element;\n}\n\nexport const T: TInterface = (props) => {\n const key = (props as PropsWithKeyName).keyName || props.children;\n if (key === undefined) {\n // eslint-disable-next-line no-console\n console.error('T component: keyName not defined');\n }\n const defaultValue =\n props.defaultValue ||\n ((props as PropsWithKeyName).keyName ? props.children : undefined);\n\n const { t } = useTranslateInternal();\n\n const translation = addReactKeys(\n t({\n key: key!,\n params: wrapTagHandlers(props.params),\n defaultValue,\n noWrap: props.noWrap,\n ns: props.ns,\n language: props.language,\n })\n );\n\n return <>{translation}</>;\n};\n","import { TolgeeEvent, TolgeeInstance } from '@tolgee/web';\nimport { useEffect } from 'react';\nimport { useRerender } from './hooks';\nimport { useTolgeeContext } from './useTolgeeContext';\n\nexport const useTolgee = (events?: TolgeeEvent[]): TolgeeInstance => {\n const { tolgee } = useTolgeeContext();\n\n const { rerender } = useRerender();\n\n useEffect(() => {\n const listeners = events?.map((e) => tolgee.on(e, rerender));\n return () => {\n listeners?.forEach((listener) => listener.unsubscribe());\n };\n }, [events?.join(':')]);\n\n return tolgee;\n};\n","import {\n getTranslateProps,\n TolgeeInstance,\n TolgeeStaticData,\n} from '@tolgee/web';\nimport { useEffect, useMemo, useState } from 'react';\n\nfunction getTolgeeWithDeactivatedWrapper(\n tolgee: TolgeeInstance\n): TolgeeInstance {\n return {\n ...tolgee,\n t(...args) {\n // @ts-ignore\n const props = getTranslateProps(...args);\n return tolgee.t({ ...props, noWrap: true });\n },\n };\n}\n\nexport function useTolgeeSSR(\n tolgeeInstance: TolgeeInstance,\n locale?: string,\n staticData?: TolgeeStaticData | undefined\n) {\n const initialInstance = useMemo(\n () => getTolgeeWithDeactivatedWrapper(tolgeeInstance),\n []\n );\n\n const [tolgee, setTolgee] = useState(initialInstance);\n\n useEffect(() => {\n setTolgee(tolgeeInstance);\n }, []);\n\n useMemo(() => {\n // we have to prepare tolgee before rendering children\n // so translations are available right away\n // events emitting must be off, to not trigger re-render while rendering\n tolgee.setEmitterActive(false);\n tolgee.addStaticData(staticData);\n tolgee.changeLanguage(locale!);\n tolgee.setEmitterActive(true);\n }, [locale, staticData, tolgee]);\n\n return tolgee;\n}\n","import { useCallback } from 'react';\nimport {\n TFnType,\n getTranslateProps,\n DefaultParamType,\n TranslationKey,\n} from '@tolgee/web';\n\nimport { useTranslateInternal } from './useTranslateInternal';\nimport { ReactOptions } from './types';\n\nexport interface UseTranslateResult {\n t: TFnType<DefaultParamType, string, TranslationKey>;\n isLoading: boolean;\n}\n\nexport const useTranslate = (\n ns?: string[] | string,\n options?: ReactOptions\n): UseTranslateResult => {\n const { t: tInternal, isLoading } = useTranslateInternal(ns, options);\n\n const t = useCallback(\n (...params: any) => {\n // @ts-ignore\n const props = getTranslateProps(...params);\n return tInternal(props);\n },\n [tInternal]\n );\n\n return { t, isLoading };\n};\n"],"names":["DEFAULT_REACT_OPTIONS","useSuspense","TolgeeProviderContext","React","createContext","undefined","globalContext","useTolgeeContext","context","useContext","Error","useRerender","instance","setCounter","useState","rerender","useCallback","num","useTranslateInternal","ns","options","tolgee","defaultOptions","namespaces","getFallback","namespacesJoined","getFallbackArray","join","currentOptions","Object","assign","subscriptionRef","useRef","subscriptionQueue","current","isLoaded","useEffect","subscription","onNsUpdate","subscribeNs","forEach","unsubscribe","addActiveNs","removeActiveNs","t","props","fallbackNs","_a","push","subscribeToNs","isLoading","wrapTagHandlers","params","result","entries","key","value","chunk","addReactKeys","isValidElement","el","children","length","cloneElement","val","Array","isArray","Children","toArray","keyName","console","error","defaultValue","translation","noWrap","language","createElement","Fragment","fallback","loading","setLoading","run","catch","e","finally","optionsWithDefault","Provider","Suspense","events","listeners","map","on","listener","tolgeeInstance","locale","staticData","initialInstance","useMemo","args","getTranslateProps","getTolgeeWithDeactivatedWrapper","setTolgee","setEmitterActive","addStaticData","changeLanguage","tInternal"],"mappings":"saAIO,MAAMA,EAAsC,CACjDC,aAAa,GAGFC,EAAwBC,EAAAA,QAAMC,mBAEzCC,GCNF,IAAIC,ECAG,MAAMC,EAAmB,KAC9B,MAAMC,EAAUC,EAAUA,WAACP,IDYpBI,ECXP,IAAKE,EACH,MAAM,IAAIE,MACR,0EAGJ,OAAOF,CAAO,ECTHG,EAAc,KACzB,MAAOC,EAAUC,GAAcC,EAAQA,SAAC,GAKxC,MAAO,CAAEF,WAAUG,SAHFC,EAAAA,aAAY,KAC3BH,GAAYI,GAAQA,EAAM,GAAE,GAC3B,CAACJ,IACyB,ECKlBK,EAAuB,CAClCC,EACAC,KAEA,MAAMC,OAAEA,EAAQD,QAASE,GAAmBf,IACtCgB,EAAaC,cAAYL,GACzBM,EAAmBC,EAAAA,iBAAiBH,GAAYI,KAAK,KAErDC,EACDC,OAAAC,OAAAD,OAAAC,OAAA,GAAAR,GACAF,IAICL,SAAEA,EAAQH,SAAEA,GAAaD,IAEzBoB,EAAkBC,EAAAA,SAElBC,EAAoBD,SAAO,IACjCC,EAAkBC,QAAU,GAE5B,MAKMC,EAAWd,EAAOc,SAASZ,GAEjCa,EAAAA,WAAU,KACR,MAAMC,EAAehB,EAAOiB,WAAWvB,GASvC,OARAgB,EAAgBG,QAAUG,EACrBF,GACHE,EAAaE,YAAYhB,GAE3BU,EAAkBC,QAAQM,SAASrB,IACjCkB,EAAcE,YAAYpB,EAAG,IAGxB,KACLkB,EAAaI,aAAa,CAC3B,GACA,CAACN,EAAUV,EAAkBJ,IAEhCe,EAAAA,WAAU,KACRf,EAAOqB,YAAYnB,GACZ,IAAMF,EAAOsB,eAAepB,KAClC,CAACE,EAAkBJ,IAEtB,MAAMuB,EAAI5B,eACP6B,UACC,MAAMC,EAAqB,QAARC,EAAAF,EAAM1B,UAAE,IAAA4B,EAAAA,EAAIxB,aAAA,EAAAA,E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,KAAKsD,GAEH,MAAMrD,EAAQsD,EAAAA,qBAAqBD,GACnC,OAAO7E,EAAOuB,EAAOf,OAAAC,OAAAD,OAAAC,OAAA,CAAA,EAAAe,IAAO6B,QAAQ,IACrC,GAEL,CAQU0B,CAAgCP,IACtC,KAGKxE,EAAQgF,GAAavF,EAAQA,SAACkF,GAgBrC,OAdA5D,EAAAA,WAAU,KACRiE,EAAUR,EAAe,GACxB,IAEHI,EAAAA,SAAQ,KAIN5E,EAAOiF,kBAAiB,GACxBjF,EAAOkF,cAAcR,GACrB1E,EAAOmF,eAAeV,GACtBzE,EAAOiF,kBAAiB,EAAK,GAC5B,CAACR,EAAQC,EAAY1E,IAEjBA,CACT,iBC/B4B,CAC1BF,EACAC,KAEA,MAAQwB,EAAG6D,EAASvD,UAAEA,GAAchC,EAAqBC,EAAIC,GAW7D,MAAO,CAAEwB,EATC5B,EAAAA,aACR,IAAIoC,KAEF,MAAMP,EAAQsD,EAAAA,qBAAqB/C,GACnC,OAAOqD,EAAU5D,EAAM,GAEzB,CAAC4D,IAGSvD,YAAW"}
1
+ {"version":3,"file":"tolgee-react.umd.min.js","sources":["../src/TolgeeProvider.tsx","../src/GlobalContextPlugin.tsx","../src/useTolgeeContext.ts","../src/hooks.ts","../src/useTranslateInternal.ts","../src/tagsTools.tsx","../src/TBase.tsx","../src/T.tsx","../src/useTolgee.ts","../src/useTolgeeSSR.ts","../src/useTranslate.ts"],"sourcesContent":["import React, { Suspense, useEffect, useState } from 'react';\nimport { TolgeeInstance } from '@tolgee/web';\nimport { ReactOptions, TolgeeReactContext } from './types';\n\nexport const DEFAULT_REACT_OPTIONS: ReactOptions = {\n useSuspense: true,\n};\n\nlet ProviderInstance: React.Context<TolgeeReactContext | undefined>;\n\nexport const getProviderInstance = () => {\n if (!ProviderInstance) {\n ProviderInstance = React.createContext<TolgeeReactContext | undefined>(\n undefined\n );\n }\n\n return ProviderInstance;\n};\n\nexport interface TolgeeProviderProps {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n}\n\nexport const TolgeeProvider: React.FC<TolgeeProviderProps> = ({\n tolgee,\n options,\n children,\n fallback,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n useEffect(() => {\n tolgee\n .run()\n .catch((e) => {\n // eslint-disable-next-line no-console\n console.error(e);\n })\n .finally(() => {\n setLoading(false);\n });\n }, [tolgee]);\n\n const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options };\n\n const TolgeeProviderContext = getProviderInstance();\n\n if (optionsWithDefault.useSuspense) {\n return (\n <TolgeeProviderContext.Provider\n value={{ tolgee, options: optionsWithDefault }}\n >\n {loading ? (\n fallback\n ) : (\n <Suspense fallback={fallback || null}>{children}</Suspense>\n )}\n </TolgeeProviderContext.Provider>\n );\n }\n\n return (\n <TolgeeProviderContext.Provider\n value={{ tolgee, options: optionsWithDefault }}\n >\n {loading ? fallback : children}\n </TolgeeProviderContext.Provider>\n );\n};\n","import type { TolgeePlugin } from '@tolgee/web';\nimport { DEFAULT_REACT_OPTIONS } from './TolgeeProvider';\nimport type { ReactOptions, TolgeeReactContext } from './types';\n\nlet globalContext: TolgeeReactContext | undefined;\n\nexport const GlobalContextPlugin =\n (options?: Partial<ReactOptions>): TolgeePlugin =>\n (tolgee) => {\n globalContext = {\n tolgee,\n options: { ...DEFAULT_REACT_OPTIONS, ...options },\n };\n return tolgee;\n };\n\nexport function getGlobalContext() {\n return globalContext;\n}\n","import { useContext } from 'react';\nimport { getGlobalContext } from './GlobalContextPlugin';\nimport { getProviderInstance } from './TolgeeProvider';\n\nexport const useTolgeeContext = () => {\n const TolgeeProviderContext = getProviderInstance();\n const context = useContext(TolgeeProviderContext) || getGlobalContext();\n if (!context) {\n throw new Error(\n \"Couldn't find tolgee instance, did you forgot to use `TolgeeProvider`?\"\n );\n }\n return context;\n};\n","import { useCallback, useState } from 'react';\n\nexport const useRerender = () => {\n const [instance, setCounter] = useState(0);\n\n const rerender = useCallback(() => {\n setCounter((num) => num + 1);\n }, [setCounter]);\n return { instance, rerender };\n};\n","import { useCallback, useEffect, useRef } from 'react';\nimport {\n SubscriptionSelective,\n TranslateProps,\n NsFallback,\n getFallbackArray,\n getFallback,\n} from '@tolgee/web';\n\nimport { useTolgeeContext } from './useTolgeeContext';\nimport { ReactOptions } from './types';\nimport { useRerender } from './hooks';\n\nexport const useTranslateInternal = (\n ns?: NsFallback,\n options?: ReactOptions\n) => {\n const { tolgee, options: defaultOptions } = useTolgeeContext();\n const namespaces = getFallback(ns);\n const namespacesJoined = getFallbackArray(namespaces).join(':');\n\n const currentOptions = {\n ...defaultOptions,\n ...options,\n };\n\n // dummy state to enable re-rendering\n const { rerender, instance } = useRerender();\n\n const subscriptionRef = useRef<SubscriptionSelective>();\n\n const subscriptionQueue = useRef([] as NsFallback[]);\n subscriptionQueue.current = [];\n\n const subscribeToNs = (ns: NsFallback) => {\n subscriptionQueue.current.push(ns);\n subscriptionRef.current?.subscribeNs(ns);\n };\n\n const isLoaded = tolgee.isLoaded(namespaces);\n\n useEffect(() => {\n const subscription = tolgee.onNsUpdate(rerender);\n subscriptionRef.current = subscription;\n if (!isLoaded) {\n subscription.subscribeNs(namespaces);\n }\n subscriptionQueue.current.forEach((ns) => {\n subscription!.subscribeNs(ns);\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [isLoaded, namespacesJoined, tolgee]);\n\n useEffect(() => {\n tolgee.addActiveNs(namespaces);\n return () => tolgee.removeActiveNs(namespaces);\n }, [namespacesJoined, tolgee]);\n\n const t = useCallback(\n (props: TranslateProps<any>) => {\n const fallbackNs = props.ns ?? namespaces?.[0];\n subscribeToNs(fallbackNs);\n return tolgee.t({ ...props, ns: fallbackNs }) as any;\n },\n [tolgee, instance]\n );\n\n if (currentOptions.useSuspense && !isLoaded) {\n throw tolgee.addActiveNs(namespaces, true);\n }\n\n return { t, isLoading: !isLoaded };\n};\n","import { TranslateParams } from '@tolgee/web';\nimport React from 'react';\n\nimport { ParamsTags } from './types';\n\nexport const wrapTagHandlers = (\n params: TranslateParams<ParamsTags> | undefined\n) => {\n if (!params) {\n return undefined;\n }\n\n const result: any = {};\n\n Object.entries(params || {}).forEach(([key, value]) => {\n if (typeof value === 'function') {\n result[key] = (chunk: any) => {\n return value(addReactKeys(chunk));\n };\n } else if (React.isValidElement(value as any)) {\n const el = value as React.ReactElement;\n result[key] = (chunk: any) => {\n return el.props.children === undefined && chunk?.length\n ? React.cloneElement(el, {}, addReactKeys(chunk))\n : React.cloneElement(el);\n };\n } else {\n result[key] = value;\n }\n });\n\n return result;\n};\n\nexport const addReactKeys = (\n val: React.ReactNode | React.ReactNode[] | undefined\n) => {\n if (Array.isArray(val)) {\n return React.Children.toArray(val);\n } else {\n return val;\n }\n};\n","import React from 'react';\nimport { addReactKeys, wrapTagHandlers } from './tagsTools';\nimport { PropsWithKeyName, TBaseInterface } from './types';\n\nexport const TBase: TBaseInterface = (props) => {\n const key = (props as PropsWithKeyName).keyName || props.children;\n if (key === undefined) {\n // eslint-disable-next-line no-console\n console.error('T component: keyName not defined');\n }\n const defaultValue =\n props.defaultValue ||\n ((props as PropsWithKeyName).keyName ? props.children : undefined);\n\n const translation = addReactKeys(\n props.t({\n key: key!,\n params: wrapTagHandlers(props.params),\n defaultValue,\n noWrap: props.noWrap,\n ns: props.ns,\n language: props.language,\n })\n );\n\n return <>{translation}</>;\n};\n","import React from 'react';\nimport { ParamsTags, TProps } from './types';\n\nimport { useTranslateInternal } from './useTranslateInternal';\nimport { TFnType } from '@tolgee/web';\nimport { TBase } from './TBase';\n\ninterface TInterface {\n (props: TProps): JSX.Element;\n}\n\nexport const T: TInterface = (props) => {\n const { t } = useTranslateInternal();\n\n return <TBase t={t as TFnType<ParamsTags>} {...props} />;\n};\n","import { TolgeeEvent, TolgeeInstance } from '@tolgee/web';\nimport { useEffect } from 'react';\nimport { useRerender } from './hooks';\nimport { useTolgeeContext } from './useTolgeeContext';\n\nexport const useTolgee = (events?: TolgeeEvent[]): TolgeeInstance => {\n const { tolgee } = useTolgeeContext();\n\n const { rerender } = useRerender();\n\n useEffect(() => {\n const listeners = events?.map((e) => tolgee.on(e, rerender));\n return () => {\n listeners?.forEach((listener) => listener.unsubscribe());\n };\n }, [events?.join(':')]);\n\n return tolgee;\n};\n","import {\n getTranslateProps,\n TolgeeInstance,\n TolgeeStaticData,\n} from '@tolgee/web';\nimport { useEffect, useMemo, useState } from 'react';\n\nfunction getTolgeeWithDeactivatedWrapper(\n tolgee: TolgeeInstance\n): TolgeeInstance {\n return {\n ...tolgee,\n t(...args) {\n // @ts-ignore\n const props = getTranslateProps(...args);\n return tolgee.t({ ...props, noWrap: true });\n },\n };\n}\n\n/**\n * Updates tolgee static data and language, to be ready right away for the first render\n * and therefore compatible with SSR.\n *\n * It also ensures that the first render is done without wrapping and so it avoids\n * \"client different than server\" issues.\n *\n * @param tolgeeInstance initialized Tolgee instance\n * @param language language that is obtained outside of Tolgee on the server and client\n * @param staticData static data for the language\n */\nexport function useTolgeeSSR(\n tolgeeInstance: TolgeeInstance,\n language?: string,\n staticData?: TolgeeStaticData | undefined\n) {\n const initialInstance = useMemo(\n () => getTolgeeWithDeactivatedWrapper(tolgeeInstance),\n []\n );\n\n const [tolgee, setTolgee] = useState(initialInstance);\n\n useEffect(() => {\n setTolgee(tolgeeInstance);\n }, []);\n\n useMemo(() => {\n // we have to prepare tolgee before rendering children\n // so translations are available right away\n // events emitting must be off, to not trigger re-render while rendering\n tolgee.setEmitterActive(false);\n tolgee.addStaticData(staticData);\n tolgee.changeLanguage(language!);\n tolgee.setEmitterActive(true);\n }, [language, staticData, tolgee]);\n\n return tolgee;\n}\n","import { useCallback } from 'react';\nimport {\n TFnType,\n getTranslateProps,\n DefaultParamType,\n TranslationKey,\n} from '@tolgee/web';\n\nimport { useTranslateInternal } from './useTranslateInternal';\nimport { ReactOptions } from './types';\n\nexport interface UseTranslateResult {\n t: TFnType<DefaultParamType, string, TranslationKey>;\n isLoading: boolean;\n}\n\nexport const useTranslate = (\n ns?: string[] | string,\n options?: ReactOptions\n): UseTranslateResult => {\n const { t: tInternal, isLoading } = useTranslateInternal(ns, options);\n\n const t = useCallback(\n (...params: any) => {\n // @ts-ignore\n const props = getTranslateProps(...params);\n return tInternal(props);\n },\n [tInternal]\n );\n\n return { t, isLoading };\n};\n"],"names":["DEFAULT_REACT_OPTIONS","useSuspense","ProviderInstance","getProviderInstance","React","createContext","undefined","globalContext","useTolgeeContext","TolgeeProviderContext","context","useContext","Error","useRerender","instance","setCounter","useState","rerender","useCallback","num","useTranslateInternal","ns","options","tolgee","defaultOptions","namespaces","getFallback","namespacesJoined","getFallbackArray","join","currentOptions","Object","assign","subscriptionRef","useRef","subscriptionQueue","current","isLoaded","useEffect","subscription","onNsUpdate","subscribeNs","forEach","unsubscribe","addActiveNs","removeActiveNs","t","props","fallbackNs","_a","push","subscribeToNs","isLoading","wrapTagHandlers","params","result","entries","key","value","chunk","addReactKeys","isValidElement","el","children","length","cloneElement","val","Array","isArray","Children","toArray","TBase","keyName","console","error","defaultValue","translation","noWrap","language","createElement","Fragment","fallback","loading","setLoading","run","catch","e","finally","optionsWithDefault","Provider","Suspense","events","listeners","map","on","listener","tolgeeInstance","staticData","initialInstance","useMemo","args","getTranslateProps","getTolgeeWithDeactivatedWrapper","setTolgee","setEmitterActive","addStaticData","changeLanguage","tInternal"],"mappings":"saAIO,MAAMA,EAAsC,CACjDC,aAAa,GAGf,IAAIC,EAES,MAAAC,EAAsB,KAC5BD,IACHA,EAAmBE,EAAK,QAACC,mBACvBC,IAIGJ,GCbT,IAAIK,ECAG,MAAMC,EAAmB,KAC9B,MAAMC,EAAwBN,IACxBO,EAAUC,EAAUA,WAACF,IDWpBF,ECVP,IAAKG,EACH,MAAM,IAAIE,MACR,0EAGJ,OAAOF,CAAO,ECVHG,EAAc,KACzB,MAAOC,EAAUC,GAAcC,EAAQA,SAAC,GAKxC,MAAO,CAAEF,WAAUG,SAHFC,EAAAA,aAAY,KAC3B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IAEHG,EAAAA,SAAQ,KAIN5E,EAAOiF,kBAAiB,GACxBjF,EAAOkF,cAAcR,GACrB1E,EAAOmF,eAAe5B,GACtBvD,EAAOiF,kBAAiB,EAAK,GAC5B,CAAC1B,EAAUmB,EAAY1E,IAEnBA,CACT,iBC1C4B,CAC1BF,EACAC,KAEA,MAAQwB,EAAG6D,EAASvD,UAAEA,GAAchC,EAAqBC,EAAIC,GAW7D,MAAO,CAAEwB,EATC5B,EAAAA,aACR,IAAIoC,KAEF,MAAMP,EAAQsD,EAAAA,qBAAqB/C,GACnC,OAAOqD,EAAU5D,EAAM,GAEzB,CAAC4D,IAGSvD,YAAW"}
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { DefaultParamType, TolgeeInstance } from '@tolgee/web';
2
1
  import React from 'react';
2
+ import type { DefaultParamType, NsType, TFnType, TolgeeInstance, TranslateParams, TranslationKey } from '@tolgee/web';
3
3
  export type ParamsTags = DefaultParamType | ((value: any) => JSX.Element | React.ReactElement | null) | React.ReactNode;
4
4
  export type ReactOptions = {
5
5
  useSuspense: boolean;
@@ -8,3 +8,24 @@ export type TolgeeReactContext = {
8
8
  tolgee: TolgeeInstance;
9
9
  options: ReactOptions;
10
10
  };
11
+ interface PropsBase {
12
+ params?: TranslateParams<ParamsTags>;
13
+ noWrap?: boolean;
14
+ ns?: NsType;
15
+ defaultValue?: string;
16
+ language?: string;
17
+ }
18
+ export interface PropsWithKeyName extends PropsBase {
19
+ children?: string;
20
+ keyName: TranslationKey;
21
+ }
22
+ export interface PropsWithoutKeyName extends PropsBase {
23
+ children: TranslationKey;
24
+ }
25
+ export type TProps = PropsWithKeyName | PropsWithoutKeyName;
26
+ export interface TBaseInterface {
27
+ (props: TProps & {
28
+ t: TFnType<ParamsTags>;
29
+ }): JSX.Element;
30
+ }
31
+ export {};
@@ -1,2 +1,13 @@
1
1
  import { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';
2
- export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, locale?: string, staticData?: TolgeeStaticData | undefined): TolgeeInstance;
2
+ /**
3
+ * Updates tolgee static data and language, to be ready right away for the first render
4
+ * and therefore compatible with SSR.
5
+ *
6
+ * It also ensures that the first render is done without wrapping and so it avoids
7
+ * "client different than server" issues.
8
+ *
9
+ * @param tolgeeInstance initialized Tolgee instance
10
+ * @param language language that is obtained outside of Tolgee on the server and client
11
+ * @param staticData static data for the language
12
+ */
13
+ export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, language?: string, staticData?: TolgeeStaticData | undefined): TolgeeInstance;
package/lib/T.d.ts CHANGED
@@ -1,21 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { NsType, TranslateParams, TranslationKey } from '@tolgee/web';
3
- import { ParamsTags } from './types';
4
- interface PropsBase {
5
- params?: TranslateParams<ParamsTags>;
6
- noWrap?: boolean;
7
- ns?: NsType;
8
- defaultValue?: string;
9
- language?: string;
10
- }
11
- interface PropsWithKeyName extends PropsBase {
12
- children?: string;
13
- keyName: TranslationKey;
14
- }
15
- interface PropsWithoutKeyName extends PropsBase {
16
- children: TranslationKey;
17
- }
18
- export type TProps = PropsWithKeyName | PropsWithoutKeyName;
2
+ import { TProps } from './types';
19
3
  interface TInterface {
20
4
  (props: TProps): JSX.Element;
21
5
  }
package/lib/TBase.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { TBaseInterface } from './types';
2
+ export declare const TBase: TBaseInterface;
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  import { TolgeeInstance } from '@tolgee/web';
3
3
  import { ReactOptions, TolgeeReactContext } from './types';
4
4
  export declare const DEFAULT_REACT_OPTIONS: ReactOptions;
5
- export declare const TolgeeProviderContext: React.Context<TolgeeReactContext | undefined>;
5
+ export declare const getProviderInstance: () => React.Context<TolgeeReactContext | undefined>;
6
6
  export interface TolgeeProviderProps {
7
7
  children?: React.ReactNode;
8
8
  tolgee: TolgeeInstance;
package/lib/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  export { useTranslate, UseTranslateResult } from './useTranslate';
2
- export { TolgeeProvider, TolgeeProviderProps, TolgeeProviderContext, } from './TolgeeProvider';
3
- export { T, TProps } from './T';
2
+ export { TolgeeProvider, TolgeeProviderProps, getProviderInstance, } from './TolgeeProvider';
3
+ export { T } from './T';
4
4
  export { useTolgee } from './useTolgee';
5
5
  export { GlobalContextPlugin } from './GlobalContextPlugin';
6
6
  export { useTolgeeSSR } from './useTolgeeSSR';
7
+ export { TBase } from './TBase';
7
8
  export * from './types';
8
9
  export * from '@tolgee/web';
package/lib/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { DefaultParamType, TolgeeInstance } from '@tolgee/web';
2
1
  import React from 'react';
2
+ import type { DefaultParamType, NsType, TFnType, TolgeeInstance, TranslateParams, TranslationKey } from '@tolgee/web';
3
3
  export type ParamsTags = DefaultParamType | ((value: any) => JSX.Element | React.ReactElement | null) | React.ReactNode;
4
4
  export type ReactOptions = {
5
5
  useSuspense: boolean;
@@ -8,3 +8,24 @@ export type TolgeeReactContext = {
8
8
  tolgee: TolgeeInstance;
9
9
  options: ReactOptions;
10
10
  };
11
+ interface PropsBase {
12
+ params?: TranslateParams<ParamsTags>;
13
+ noWrap?: boolean;
14
+ ns?: NsType;
15
+ defaultValue?: string;
16
+ language?: string;
17
+ }
18
+ export interface PropsWithKeyName extends PropsBase {
19
+ children?: string;
20
+ keyName: TranslationKey;
21
+ }
22
+ export interface PropsWithoutKeyName extends PropsBase {
23
+ children: TranslationKey;
24
+ }
25
+ export type TProps = PropsWithKeyName | PropsWithoutKeyName;
26
+ export interface TBaseInterface {
27
+ (props: TProps & {
28
+ t: TFnType<ParamsTags>;
29
+ }): JSX.Element;
30
+ }
31
+ export {};
@@ -1,2 +1,13 @@
1
1
  import { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';
2
- export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, locale?: string, staticData?: TolgeeStaticData | undefined): TolgeeInstance;
2
+ /**
3
+ * Updates tolgee static data and language, to be ready right away for the first render
4
+ * and therefore compatible with SSR.
5
+ *
6
+ * It also ensures that the first render is done without wrapping and so it avoids
7
+ * "client different than server" issues.
8
+ *
9
+ * @param tolgeeInstance initialized Tolgee instance
10
+ * @param language language that is obtained outside of Tolgee on the server and client
11
+ * @param staticData static data for the language
12
+ */
13
+ export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, language?: string, staticData?: TolgeeStaticData | undefined): TolgeeInstance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolgee/react",
3
- "version": "5.10.2",
3
+ "version": "5.11.1-prerelease.cb4bc75b.0",
4
4
  "description": "React implementation for Tolgee localization framework",
5
5
  "main": "./dist/tolgee-react.cjs.js",
6
6
  "module": "./dist/tolgee-react.esm.js",
@@ -38,7 +38,7 @@
38
38
  "react": "^16.14.0 || ^17.0.1 || ^18.1.0"
39
39
  },
40
40
  "dependencies": {
41
- "@tolgee/web": "5.10.2"
41
+ "@tolgee/web": "5.11.1-prerelease.cb4bc75b.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@rollup/plugin-node-resolve": "^14.1.0",
@@ -46,8 +46,8 @@
46
46
  "@testing-library/dom": "^8.7.2",
47
47
  "@testing-library/jest-dom": "^5.11.4",
48
48
  "@testing-library/react": "^14.0.0",
49
- "@tolgee/format-icu": "5.10.2",
50
- "@tolgee/testing": "5.10.2",
49
+ "@tolgee/format-icu": "5.11.1-prerelease.cb4bc75b.0",
50
+ "@tolgee/testing": "5.11.1-prerelease.cb4bc75b.0",
51
51
  "@types/jest": "^27.0.2",
52
52
  "@types/node": "^17.0.8",
53
53
  "@types/react": "^18.0.28",
@@ -72,7 +72,8 @@
72
72
  ".": {
73
73
  "types": "./lib/index.d.ts",
74
74
  "require": "./dist/tolgee-react.cjs.js",
75
- "import": "./dist/tolgee-react.esm.mjs"
75
+ "module": "./dist/tolgee-react.esm.js",
76
+ "import": "./dist/tolgee-react.esm.js"
76
77
  },
77
78
  "./package.json": "./package.json"
78
79
  },
@@ -80,5 +81,5 @@
80
81
  "access": "public"
81
82
  },
82
83
  "sideEffects": false,
83
- "gitHead": "38d4c0acf8a1bb14976fac3b9391afde580ee261"
84
+ "gitHead": "962acf97299b1ba290ca5d2030c358a9bc10026b"
84
85
  }
package/src/T.tsx CHANGED
@@ -1,55 +1,16 @@
1
- import { NsType, TranslateParams, TranslationKey } from '@tolgee/web';
2
1
  import React from 'react';
3
- import { addReactKeys, wrapTagHandlers } from './tagsTools';
4
- import { ParamsTags } from './types';
2
+ import { ParamsTags, TProps } from './types';
5
3
 
6
4
  import { useTranslateInternal } from './useTranslateInternal';
7
-
8
- interface PropsBase {
9
- params?: TranslateParams<ParamsTags>;
10
- noWrap?: boolean;
11
- ns?: NsType;
12
- defaultValue?: string;
13
- language?: string;
14
- }
15
-
16
- interface PropsWithKeyName extends PropsBase {
17
- children?: string;
18
- keyName: TranslationKey;
19
- }
20
-
21
- interface PropsWithoutKeyName extends PropsBase {
22
- children: TranslationKey;
23
- }
24
-
25
- export type TProps = PropsWithKeyName | PropsWithoutKeyName;
5
+ import { TFnType } from '@tolgee/web';
6
+ import { TBase } from './TBase';
26
7
 
27
8
  interface TInterface {
28
9
  (props: TProps): JSX.Element;
29
10
  }
30
11
 
31
12
  export const T: TInterface = (props) => {
32
- const key = (props as PropsWithKeyName).keyName || props.children;
33
- if (key === undefined) {
34
- // eslint-disable-next-line no-console
35
- console.error('T component: keyName not defined');
36
- }
37
- const defaultValue =
38
- props.defaultValue ||
39
- ((props as PropsWithKeyName).keyName ? props.children : undefined);
40
-
41
13
  const { t } = useTranslateInternal();
42
14
 
43
- const translation = addReactKeys(
44
- t({
45
- key: key!,
46
- params: wrapTagHandlers(props.params),
47
- defaultValue,
48
- noWrap: props.noWrap,
49
- ns: props.ns,
50
- language: props.language,
51
- })
52
- );
53
-
54
- return <>{translation}</>;
15
+ return <TBase t={t as TFnType<ParamsTags>} {...props} />;
55
16
  };
package/src/TBase.tsx ADDED
@@ -0,0 +1,27 @@
1
+ import React from 'react';
2
+ import { addReactKeys, wrapTagHandlers } from './tagsTools';
3
+ import { PropsWithKeyName, TBaseInterface } from './types';
4
+
5
+ export const TBase: TBaseInterface = (props) => {
6
+ const key = (props as PropsWithKeyName).keyName || props.children;
7
+ if (key === undefined) {
8
+ // eslint-disable-next-line no-console
9
+ console.error('T component: keyName not defined');
10
+ }
11
+ const defaultValue =
12
+ props.defaultValue ||
13
+ ((props as PropsWithKeyName).keyName ? props.children : undefined);
14
+
15
+ const translation = addReactKeys(
16
+ props.t({
17
+ key: key!,
18
+ params: wrapTagHandlers(props.params),
19
+ defaultValue,
20
+ noWrap: props.noWrap,
21
+ ns: props.ns,
22
+ language: props.language,
23
+ })
24
+ );
25
+
26
+ return <>{translation}</>;
27
+ };
@@ -6,9 +6,17 @@ export const DEFAULT_REACT_OPTIONS: ReactOptions = {
6
6
  useSuspense: true,
7
7
  };
8
8
 
9
- export const TolgeeProviderContext = React.createContext<
10
- TolgeeReactContext | undefined
11
- >(undefined);
9
+ let ProviderInstance: React.Context<TolgeeReactContext | undefined>;
10
+
11
+ export const getProviderInstance = () => {
12
+ if (!ProviderInstance) {
13
+ ProviderInstance = React.createContext<TolgeeReactContext | undefined>(
14
+ undefined
15
+ );
16
+ }
17
+
18
+ return ProviderInstance;
19
+ };
12
20
 
13
21
  export interface TolgeeProviderProps {
14
22
  children?: React.ReactNode;
@@ -39,6 +47,8 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
39
47
 
40
48
  const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options };
41
49
 
50
+ const TolgeeProviderContext = getProviderInstance();
51
+
42
52
  if (optionsWithDefault.useSuspense) {
43
53
  return (
44
54
  <TolgeeProviderContext.Provider
package/src/index.ts CHANGED
@@ -2,12 +2,13 @@ export { useTranslate, UseTranslateResult } from './useTranslate';
2
2
  export {
3
3
  TolgeeProvider,
4
4
  TolgeeProviderProps,
5
- TolgeeProviderContext,
5
+ getProviderInstance,
6
6
  } from './TolgeeProvider';
7
- export { T, TProps } from './T';
7
+ export { T } from './T';
8
8
  export { useTolgee } from './useTolgee';
9
9
  export { GlobalContextPlugin } from './GlobalContextPlugin';
10
10
  export { useTolgeeSSR } from './useTolgeeSSR';
11
+ export { TBase } from './TBase';
11
12
  export * from './types';
12
13
 
13
14
  export * from '@tolgee/web';
package/src/types.ts CHANGED
@@ -1,5 +1,12 @@
1
- import type { DefaultParamType, TolgeeInstance } from '@tolgee/web';
2
1
  import React from 'react';
2
+ import type {
3
+ DefaultParamType,
4
+ NsType,
5
+ TFnType,
6
+ TolgeeInstance,
7
+ TranslateParams,
8
+ TranslationKey,
9
+ } from '@tolgee/web';
3
10
 
4
11
  export type ParamsTags =
5
12
  | DefaultParamType
@@ -14,3 +21,26 @@ export type TolgeeReactContext = {
14
21
  tolgee: TolgeeInstance;
15
22
  options: ReactOptions;
16
23
  };
24
+
25
+ interface PropsBase {
26
+ params?: TranslateParams<ParamsTags>;
27
+ noWrap?: boolean;
28
+ ns?: NsType;
29
+ defaultValue?: string;
30
+ language?: string;
31
+ }
32
+
33
+ export interface PropsWithKeyName extends PropsBase {
34
+ children?: string;
35
+ keyName: TranslationKey;
36
+ }
37
+
38
+ export interface PropsWithoutKeyName extends PropsBase {
39
+ children: TranslationKey;
40
+ }
41
+
42
+ export type TProps = PropsWithKeyName | PropsWithoutKeyName;
43
+
44
+ export interface TBaseInterface {
45
+ (props: TProps & { t: TFnType<ParamsTags> }): JSX.Element;
46
+ }
@@ -1,8 +1,9 @@
1
1
  import { useContext } from 'react';
2
2
  import { getGlobalContext } from './GlobalContextPlugin';
3
- import { TolgeeProviderContext } from './TolgeeProvider';
3
+ import { getProviderInstance } from './TolgeeProvider';
4
4
 
5
5
  export const useTolgeeContext = () => {
6
+ const TolgeeProviderContext = getProviderInstance();
6
7
  const context = useContext(TolgeeProviderContext) || getGlobalContext();
7
8
  if (!context) {
8
9
  throw new Error(
@@ -18,9 +18,20 @@ function getTolgeeWithDeactivatedWrapper(
18
18
  };
19
19
  }
20
20
 
21
+ /**
22
+ * Updates tolgee static data and language, to be ready right away for the first render
23
+ * and therefore compatible with SSR.
24
+ *
25
+ * It also ensures that the first render is done without wrapping and so it avoids
26
+ * "client different than server" issues.
27
+ *
28
+ * @param tolgeeInstance initialized Tolgee instance
29
+ * @param language language that is obtained outside of Tolgee on the server and client
30
+ * @param staticData static data for the language
31
+ */
21
32
  export function useTolgeeSSR(
22
33
  tolgeeInstance: TolgeeInstance,
23
- locale?: string,
34
+ language?: string,
24
35
  staticData?: TolgeeStaticData | undefined
25
36
  ) {
26
37
  const initialInstance = useMemo(
@@ -40,9 +51,9 @@ export function useTolgeeSSR(
40
51
  // events emitting must be off, to not trigger re-render while rendering
41
52
  tolgee.setEmitterActive(false);
42
53
  tolgee.addStaticData(staticData);
43
- tolgee.changeLanguage(locale!);
54
+ tolgee.changeLanguage(language!);
44
55
  tolgee.setEmitterActive(true);
45
- }, [locale, staticData, tolgee]);
56
+ }, [language, staticData, tolgee]);
46
57
 
47
58
  return tolgee;
48
59
  }