@tolgee/react 5.29.6-prerelease.4e3062df.0 → 5.30.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.js","sources":["../src/useTolgeeSSR.ts","../src/TolgeeProvider.tsx","../src/GlobalContextPlugin.tsx","../src/useTolgeeContext.ts","../src/hooks.ts","../src/useTranslateInternal.ts","../src/useTranslate.ts","../src/tagsTools.tsx","../src/TBase.tsx","../src/T.tsx","../src/useTolgee.ts"],"sourcesContent":["import {\n getTranslateProps,\n TolgeeInstance,\n TolgeeStaticData,\n isSSR,\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 * If no language data and static data are provided no action is taken\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 enabled = Boolean(language || staticData);\n\n const [noWrappingTolgee] = useState(() =>\n getTolgeeWithDeactivatedWrapper(tolgeeInstance)\n );\n\n const [initialRender, setInitialRender] = useState(enabled);\n\n useEffect(() => {\n setInitialRender(false);\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 if (enabled) {\n tolgeeInstance.setEmitterActive(false);\n tolgeeInstance.addStaticData(staticData);\n tolgeeInstance.changeLanguage(language!);\n tolgeeInstance.setEmitterActive(true);\n }\n }, [language, staticData, tolgeeInstance]);\n\n useState(() => {\n if (enabled && isSSR()) {\n // running this function only on first render\n if (!tolgeeInstance.isLoaded()) {\n // warning user, that static data provided are not sufficient\n // for proper SSR render\n const missingRecords = tolgeeInstance\n .getRequiredRecords(language)\n .map(({ namespace, language }) =>\n namespace ? `${namespace}:${language}` : language\n )\n .filter((key) => !staticData?.[key]);\n\n // eslint-disable-next-line no-console\n console.warn(\n `Tolgee: Missing records in \"staticData\" for proper SSR functionality: ${missingRecords.map((key) => `\"${key}\"`).join(', ')}`\n );\n }\n }\n });\n\n return initialRender ? noWrappingTolgee : tolgeeInstance;\n}\n","import React, { Suspense, useEffect, useState } from 'react';\nimport { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';\nimport { ReactOptions, TolgeeReactContext } from './types';\nimport { useTolgeeSSR } from './useTolgeeSSR';\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\nlet LAST_TOLGEE_INSTANCE: TolgeeInstance | undefined = undefined;\n\nexport interface TolgeeProviderProps {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n /**\n * Hard set language to this value, use together with `staticData`\n */\n language?: string;\n /**\n * If provided, static data will be hard set to Tolgee cache for initial render\n */\n staticData?: TolgeeStaticData;\n}\n\nexport const TolgeeProvider: React.FC<TolgeeProviderProps> = ({\n tolgee,\n options,\n children,\n fallback,\n staticData,\n language,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n // prevent restarting tolgee unnecesarly\n // however if the instance change on hot-reloading\n // we want to restart\n useEffect(() => {\n if (LAST_TOLGEE_INSTANCE?.run !== tolgee.run) {\n if (LAST_TOLGEE_INSTANCE) {\n LAST_TOLGEE_INSTANCE.stop();\n }\n LAST_TOLGEE_INSTANCE = tolgee;\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 }\n }, [tolgee]);\n\n const tolgeeSSR = useTolgeeSSR(tolgee, language, staticData);\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: tolgeeSSR, 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: tolgeeSSR, 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 subscription.subscribeNs(namespaces);\n subscriptionQueue.current.forEach((ns) => {\n subscription!.subscribeNs(ns);\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [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 { 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","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 type { 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"],"names":["getTranslateProps","useState","useEffect","useMemo","isSSR","React","Suspense","useContext","useCallback","getFallback","getFallbackArray","useRef"],"mappings":";;;;;;;;;;IAQA,SAAS,+BAA+B,CACtC,MAAsB,EAAA;IAEtB,IAAA,OAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,EAAA,EACK,MAAM,CAAA,EAAA,EACT,CAAC,CAAC,GAAG,IAAI,EAAA;;IAEP,YAAA,MAAM,KAAK,GAAGA,qBAAiB,CAAC,GAAG,IAAI,CAAC,CAAC;gBACzC,OAAO,MAAM,CAAC,CAAC,CAAM,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,EAAA,EAAA,KAAK,KAAE,MAAM,EAAE,IAAI,EAAA,CAAA,CAAG,CAAC;IAC9C,SAAC,EACD,CAAA,CAAA;IACJ,CAAC;IAED;;;;;;;;;;;;IAYG;aACa,YAAY,CAC1B,cAA8B,EAC9B,QAAiB,EACjB,UAAyC,EAAA;QAEzC,MAAM,OAAO,GAAG,OAAO,CAAC,QAAQ,IAAI,UAAU,CAAC,CAAC;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E,GAAG,WAAW,EAAE,CAAC;QAEnCH,eAAS,CAAC,MAAK;YACb,MAAM,SAAS,GAAG,MAAM,KAAN,IAAA,IAAA,MAAM,uBAAN,MAAM,CAAE,GAAG,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC7D,QAAA,OAAO,MAAK;IACV,YAAA,SAAS,aAAT,SAAS,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAT,SAAS,CAAE,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;IAC3D,SAAC,CAAC;IACJ,KAAC,EAAE,CAAC,MAAM,KAAA,IAAA,IAAN,MAAM,KAAN,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,MAAM,CAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAExB,IAAA,OAAO,MAAM,CAAC;IAChB;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"tolgee-react.umd.js","sources":["../src/useTolgeeSSR.ts","../src/TolgeeProvider.tsx","../src/GlobalContextPlugin.tsx","../src/useTolgeeContext.ts","../src/hooks.ts","../src/useTranslateInternal.ts","../src/useTranslate.ts","../src/tagsTools.tsx","../src/TBase.tsx","../src/T.tsx","../src/useTolgee.ts"],"sourcesContent":["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 * @param enabled if set to false, no action is taken\n */\nexport function useTolgeeSSR(\n tolgeeInstance: TolgeeInstance,\n language?: string,\n staticData?: TolgeeStaticData | undefined,\n enabled = true\n) {\n const [noWrappingTolgee] = useState(() =>\n getTolgeeWithDeactivatedWrapper(tolgeeInstance)\n );\n\n const [initialRender, setInitialRender] = useState(enabled);\n\n useEffect(() => {\n setInitialRender(false);\n }, []);\n\n useMemo(() => {\n if (enabled) {\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 tolgeeInstance.setEmitterActive(false);\n tolgeeInstance.addStaticData(staticData);\n tolgeeInstance.changeLanguage(language!);\n tolgeeInstance.setEmitterActive(true);\n }\n }, [language, staticData, tolgeeInstance]);\n\n useState(() => {\n // running this function only on first render\n if (!tolgeeInstance.isLoaded() && enabled) {\n // warning user, that static data provided are not sufficient\n // for proper SSR render\n const missingRecords = tolgeeInstance\n .getRequiredRecords(language)\n .map(({ namespace, language }) =>\n namespace ? `${namespace}:${language}` : language\n )\n .filter((key) => !staticData?.[key]);\n\n // eslint-disable-next-line no-console\n console.warn(\n `Tolgee: Missing records in \"staticData\" for proper SSR functionality: ${missingRecords.map((key) => `\"${key}\"`).join(', ')}`\n );\n }\n });\n\n return initialRender ? noWrappingTolgee : tolgeeInstance;\n}\n","import React, { Suspense, useEffect, useState } from 'react';\nimport { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';\nimport { ReactOptions, TolgeeReactContext } from './types';\nimport { useTolgeeSSR } from './useTolgeeSSR';\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\nlet LAST_TOLGEE_INSTANCE: TolgeeInstance | undefined = undefined;\n\nexport type SSROptions = {\n /**\n * Hard set language to this value, use together with `staticData`\n */\n language?: string;\n /**\n * If provided, static data will be hard set to Tolgee cache for initial render\n */\n staticData?: TolgeeStaticData;\n};\n\nexport interface TolgeeProviderProps {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n /**\n * use this option if you use SSR\n *\n * You can pass staticData and language\n * which will be set to tolgee instance for the initial render\n *\n * Don't switch between ssr and non-ssr dynamically\n */\n ssr?: SSROptions | boolean;\n}\n\nexport const TolgeeProvider: React.FC<TolgeeProviderProps> = ({\n tolgee,\n options,\n children,\n fallback,\n ssr,\n}) => {\n // prevent restarting tolgee unnecesarly\n // however if the instance change on hot-reloading\n // we want to restart\n useEffect(() => {\n if (LAST_TOLGEE_INSTANCE?.run !== tolgee.run) {\n if (LAST_TOLGEE_INSTANCE) {\n LAST_TOLGEE_INSTANCE.stop();\n }\n LAST_TOLGEE_INSTANCE = tolgee;\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 }\n }, [tolgee]);\n\n let tolgeeSSR = tolgee;\n\n const { language, staticData } = (\n typeof ssr !== 'object' ? {} : ssr\n ) as SSROptions;\n tolgeeSSR = useTolgeeSSR(tolgee, language, staticData, Boolean(ssr));\n\n const [loading, setLoading] = useState(!tolgeeSSR.isLoaded());\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: tolgeeSSR, 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: tolgeeSSR, 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 subscription.subscribeNs(namespaces);\n subscriptionQueue.current.forEach((ns) => {\n subscription!.subscribeNs(ns);\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [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 { 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","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 type { 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"],"names":["getTranslatePro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,KAAA,CAAA,GAAA,MAAM,CAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAExB,IAAA,OAAO,MAAM,CAAC;IAChB;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,2 +1,2 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@tolgee/web")):"function"==typeof define&&define.amd?define(["exports","react","@tolgee/web"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@tolgee/react"]={},e.React,e["@tolgee/web"])}(this,(function(e,t,n){"use strict";function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=s(t);function o(e,s,r){const o=Boolean(s||r),[a]=t.useState((()=>{return t=e,Object.assign(Object.assign({},t),{t(...e){const s=n.getTranslateProps(...e);return t.t(Object.assign(Object.assign({},s),{noWrap:!0}))}});var t})),[l,u]=t.useState(o);return t.useEffect((()=>{u(!1)}),[]),t.useMemo((()=>{o&&(e.setEmitterActive(!1),e.addStaticData(r),e.changeLanguage(s),e.setEmitterActive(!0))}),[s,r,e]),t.useState((()=>{if(o&&n.isSSR()&&!e.isLoaded()){const t=e.getRequiredRecords(s).map((({namespace:e,language:t})=>e?`${e}:${t}`:t)).filter((e=>!(null==r?void 0:r[e])));console.warn(`Tolgee: Missing records in "staticData" for proper SSR functionality: ${t.map((e=>`"${e}"`)).join(", ")}`)}})),l?a:e}const a={useSuspense:!0};let l;const u=()=>(l||(l=r.default.createContext(void 0)),l);let c;let i;const d=()=>{const e=u(),n=t.useContext(e)||i;if(!n)throw new Error("Couldn't find tolgee instance, did you forgot to use `TolgeeProvider`?");return n},f=()=>{const[e,n]=t.useState(0);return{instance:e,rerender:t.useCallback((()=>{n((e=>e+1))}),[n])}},g=(e,s)=>{const{tolgee:r,options:o}=d(),a=n.getFallback(e),l=n.getFallbackArray(a).join(":"),u=Object.assign(Object.assign({},o),s),{rerender:c,instance:i}=f(),g=t.useRef(),b=t.useRef([]);b.current=[];const p=r.isLoaded(a);t.useEffect((()=>{const e=r.onNsUpdate(c);return g.current=e,e.subscribeNs(a),b.current.forEach((t=>{e.subscribeNs(t)})),()=>{e.unsubscribe()}}),[l,r]),t.useEffect((()=>(r.addActiveNs(a),()=>r.removeActiveNs(a))),[l,r]);const v=t.useCallback((e=>{var t;const n=null!==(t=e.ns)&&void 0!==t?t:null==a?void 0:a[0];return(e=>{var t;b.current.push(e),null===(t=g.current)||void 0===t||t.subscribeNs(e)})(n),r.t(Object.assign(Object.assign({},e),{ns:n}))}),[r,i]);if(u.useSuspense&&!p)throw r.addActiveNs(a,!0);return{t:v,isLoading:!p}},b=e=>{if(!e)return;const t={};return Object.entries(e||{}).forEach((([e,n])=>{if("function"==typeof n)t[e]=e=>n(p(e));else if(r.default.isValidElement(n)){const s=n;t[e]=e=>void 0===s.props.children&&(null==e?void 0:e.length)?r.default.cloneElement(s,{},p(e)):r.default.cloneElement(s)}else t[e]=n})),t},p=e=>Array.isArray(e)?r.default.Children.toArray(e):e,v=e=>{const t=e.keyName||e.children;void 0===t&&console.error("T component: keyName not defined");const n=e.defaultValue||(e.keyName?e.children:void 0),s=p(e.t({key:t,params:b(e.params),defaultValue:n,noWrap:e.noWrap,ns:e.ns,language:e.language}));return r.default.createElement(r.default.Fragment,null,s)};e.GlobalContextPlugin=e=>t=>(i={tolgee:t,options:Object.assign(Object.assign({},a),e)},t),e.T=e=>{const{t:t}=g();return r.default.createElement(v,Object.assign({t:t},e))},e.TBase=v,e.TolgeeProvider=({tolgee:e,options:n,children:s,fallback:l,staticData:i,language:d})=>{const[f,g]=t.useState(!e.isLoaded());t.useEffect((()=>{(null==c?void 0:c.run)!==e.run&&(c&&c.stop(),c=e,e.run().catch((e=>{console.error(e)})).finally((()=>{g(!1)})))}),[e]);const b=o(e,d,i),p=Object.assign(Object.assign({},a),n),v=u();return p.useSuspense?r.default.createElement(v.Provider,{value:{tolgee:b,options:p}},f?l:r.default.createElement(t.Suspense,{fallback:l||null},s)):r.default.createElement(v.Provider,{value:{tolgee:b,options:p}},f?l:s)},e.getProviderInstance=u,e.useTolgee=e=>{const{tolgee:n}=d(),{rerender:s}=f();return t.useEffect((()=>{const t=null==e?void 0:e.map((e=>n.on(e,s)));return()=>{null==t||t.forEach((e=>e.unsubscribe()))}}),[null==e?void 0:e.join(":")]),n},e.useTolgeeSSR=o,e.useTranslate=(e,s)=>{const{t:r,isLoading:o}=g(e,s);return{t:t.useCallback(((...e)=>{const t=n.getTranslateProps(...e);return r(t)}),[r]),isLoading:o}},Object.keys(n).forEach((function(t){"default"===t||e.hasOwnProperty(t)||Object.defineProperty(e,t,{enumerable:!0,get:function(){return n[t]}})})),Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@tolgee/web")):"function"==typeof define&&define.amd?define(["exports","react","@tolgee/web"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@tolgee/react"]={},e.React,e["@tolgee/web"])}(this,(function(e,t,n){"use strict";function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=s(t);function o(e,s,r,o=!0){const[a]=t.useState((()=>{return t=e,Object.assign(Object.assign({},t),{t(...e){const s=n.getTranslateProps(...e);return t.t(Object.assign(Object.assign({},s),{noWrap:!0}))}});var t})),[l,u]=t.useState(o);return t.useEffect((()=>{u(!1)}),[]),t.useMemo((()=>{o&&(e.setEmitterActive(!1),e.addStaticData(r),e.changeLanguage(s),e.setEmitterActive(!0))}),[s,r,e]),t.useState((()=>{if(!e.isLoaded()&&o){const t=e.getRequiredRecords(s).map((({namespace:e,language:t})=>e?`${e}:${t}`:t)).filter((e=>!(null==r?void 0:r[e])));console.warn(`Tolgee: Missing records in "staticData" for proper SSR functionality: ${t.map((e=>`"${e}"`)).join(", ")}`)}})),l?a:e}const a={useSuspense:!0};let l;const u=()=>(l||(l=r.default.createContext(void 0)),l);let c;let i;const d=()=>{const e=u(),n=t.useContext(e)||i;if(!n)throw new Error("Couldn't find tolgee instance, did you forgot to use `TolgeeProvider`?");return n},f=()=>{const[e,n]=t.useState(0);return{instance:e,rerender:t.useCallback((()=>{n((e=>e+1))}),[n])}},g=(e,s)=>{const{tolgee:r,options:o}=d(),a=n.getFallback(e),l=n.getFallbackArray(a).join(":"),u=Object.assign(Object.assign({},o),s),{rerender:c,instance:i}=f(),g=t.useRef(),b=t.useRef([]);b.current=[];const p=r.isLoaded(a);t.useEffect((()=>{const e=r.onNsUpdate(c);return g.current=e,e.subscribeNs(a),b.current.forEach((t=>{e.subscribeNs(t)})),()=>{e.unsubscribe()}}),[l,r]),t.useEffect((()=>(r.addActiveNs(a),()=>r.removeActiveNs(a))),[l,r]);const v=t.useCallback((e=>{var t;const n=null!==(t=e.ns)&&void 0!==t?t:null==a?void 0:a[0];return(e=>{var t;b.current.push(e),null===(t=g.current)||void 0===t||t.subscribeNs(e)})(n),r.t(Object.assign(Object.assign({},e),{ns:n}))}),[r,i]);if(u.useSuspense&&!p)throw r.addActiveNs(a,!0);return{t:v,isLoading:!p}},b=e=>{if(!e)return;const t={};return Object.entries(e||{}).forEach((([e,n])=>{if("function"==typeof n)t[e]=e=>n(p(e));else if(r.default.isValidElement(n)){const s=n;t[e]=e=>void 0===s.props.children&&(null==e?void 0:e.length)?r.default.cloneElement(s,{},p(e)):r.default.cloneElement(s)}else t[e]=n})),t},p=e=>Array.isArray(e)?r.default.Children.toArray(e):e,v=e=>{const t=e.keyName||e.children;void 0===t&&console.error("T component: keyName not defined");const n=e.defaultValue||(e.keyName?e.children:void 0),s=p(e.t({key:t,params:b(e.params),defaultValue:n,noWrap:e.noWrap,ns:e.ns,language:e.language}));return r.default.createElement(r.default.Fragment,null,s)};e.GlobalContextPlugin=e=>t=>(i={tolgee:t,options:Object.assign(Object.assign({},a),e)},t),e.T=e=>{const{t:t}=g();return r.default.createElement(v,Object.assign({t:t},e))},e.TBase=v,e.TolgeeProvider=({tolgee:e,options:n,children:s,fallback:l,ssr:i})=>{t.useEffect((()=>{(null==c?void 0:c.run)!==e.run&&(c&&c.stop(),c=e,e.run().catch((e=>{console.error(e)})).finally((()=>{p(!1)})))}),[e]);let d=e;const{language:f,staticData:g}="object"!=typeof i?{}:i;d=o(e,f,g,Boolean(i));const[b,p]=t.useState(!d.isLoaded()),v=Object.assign(Object.assign({},a),n),m=u();return v.useSuspense?r.default.createElement(m.Provider,{value:{tolgee:d,options:v}},b?l:r.default.createElement(t.Suspense,{fallback:l||null},s)):r.default.createElement(m.Provider,{value:{tolgee:d,options:v}},b?l:s)},e.getProviderInstance=u,e.useTolgee=e=>{const{tolgee:n}=d(),{rerender:s}=f();return t.useEffect((()=>{const t=null==e?void 0:e.map((e=>n.on(e,s)));return()=>{null==t||t.forEach((e=>e.unsubscribe()))}}),[null==e?void 0:e.join(":")]),n},e.useTolgeeSSR=o,e.useTranslate=(e,s)=>{const{t:r,isLoading:o}=g(e,s);return{t:t.useCallback(((...e)=>{const t=n.getTranslateProps(...e);return r(t)}),[r]),isLoading:o}},Object.keys(n).forEach((function(t){"default"===t||e.hasOwnProperty(t)||Object.defineProperty(e,t,{enumerable:!0,get:function(){return n[t]}})})),Object.defineProperty(e,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=tolgee-react.umd.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"tolgee-react.umd.min.js","sources":["../src/useTolgeeSSR.ts","../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/useTranslate.ts"],"sourcesContent":["import {\n getTranslateProps,\n TolgeeInstance,\n TolgeeStaticData,\n isSSR,\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 * If no language data and static data are provided no action is taken\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 enabled = Boolean(language || staticData);\n\n const [noWrappingTolgee] = useState(() =>\n getTolgeeWithDeactivatedWrapper(tolgeeInstance)\n );\n\n const [initialRender, setInitialRender] = useState(enabled);\n\n useEffect(() => {\n setInitialRender(false);\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 if (enabled) {\n tolgeeInstance.setEmitterActive(false);\n tolgeeInstance.addStaticData(staticData);\n tolgeeInstance.changeLanguage(language!);\n tolgeeInstance.setEmitterActive(true);\n }\n }, [language, staticData, tolgeeInstance]);\n\n useState(() => {\n if (enabled && isSSR()) {\n // running this function only on first render\n if (!tolgeeInstance.isLoaded()) {\n // warning user, that static data provided are not sufficient\n // for proper SSR render\n const missingRecords = tolgeeInstance\n .getRequiredRecords(language)\n .map(({ namespace, language }) =>\n namespace ? `${namespace}:${language}` : language\n )\n .filter((key) => !staticData?.[key]);\n\n // eslint-disable-next-line no-console\n console.warn(\n `Tolgee: Missing records in \"staticData\" for proper SSR functionality: ${missingRecords.map((key) => `\"${key}\"`).join(', ')}`\n );\n }\n }\n });\n\n return initialRender ? noWrappingTolgee : tolgeeInstance;\n}\n","import React, { Suspense, useEffect, useState } from 'react';\nimport { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';\nimport { ReactOptions, TolgeeReactContext } from './types';\nimport { useTolgeeSSR } from './useTolgeeSSR';\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\nlet LAST_TOLGEE_INSTANCE: TolgeeInstance | undefined = undefined;\n\nexport interface TolgeeProviderProps {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n /**\n * Hard set language to this value, use together with `staticData`\n */\n language?: string;\n /**\n * If provided, static data will be hard set to Tolgee cache for initial render\n */\n staticData?: TolgeeStaticData;\n}\n\nexport const TolgeeProvider: React.FC<TolgeeProviderProps> = ({\n tolgee,\n options,\n children,\n fallback,\n staticData,\n language,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n // prevent restarting tolgee unnecesarly\n // however if the instance change on hot-reloading\n // we want to restart\n useEffect(() => {\n if (LAST_TOLGEE_INSTANCE?.run !== tolgee.run) {\n if (LAST_TOLGEE_INSTANCE) {\n LAST_TOLGEE_INSTANCE.stop();\n }\n LAST_TOLGEE_INSTANCE = tolgee;\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 }\n }, [tolgee]);\n\n const tolgeeSSR = useTolgeeSSR(tolgee, language, staticData);\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: tolgeeSSR, 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: tolgeeSSR, 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 subscription.subscribeNs(namespaces);\n subscriptionQueue.current.forEach((ns) => {\n subscription!.subscribeNs(ns);\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [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 type { 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 { 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":["useTolgeeSSR","tolgeeInstance","language","staticData","enabled","Boolean","noWrappingTolgee","useState","getTolgeeWithDeactivatedWrapper","tolgee","Object","assign","t","args","props","getTranslateProps","noWrap","initialRender","setInitialRender","useEffect","useMemo","setEmitterActive","addStaticData","changeLanguage","isSSR","isLoaded","missingRecords","getRequiredRecords","map","namespace","filter","key","console","warn","join","DEFAULT_REACT_OPTIONS","useSuspense","ProviderInstance","getProviderInstance","React","createContext","undefined","LAST_TOLGEE_INSTANCE","globalContext","useTolgeeContext","TolgeeProviderContext","context","useContext","Error","useRerender","instance","setCounter","rerender","useCallback","num","useTranslateInternal","ns","options","defaultOptions","namespaces","getFallback","namespacesJoined","getFallbackArray","currentOptions","subscriptionRef","useRef","subscriptionQueue","current","subscription","onNsUpdate","subscribeNs","forEach","unsubscribe","addActiveNs","removeActiveNs","fallbackNs","_a","push","subscribeToNs","isLoading","wrapTagHandlers","params","result","entries","value","chunk","addReactKeys","isValidElement","el","children","length","cloneElement","val","Array","isArray","Children","toArray","TBase","keyName","error","defaultValue","translation","createElement","Fragment","fallback","loading","setLoading","run","stop","catch","e","finally","tolgeeSSR","optionsWithDefault","Provider","Suspense","events","listeners","on","listener","tInternal"],"mappings":"+aAkCgBA,EACdC,EACAC,EACAC,GAEA,MAAMC,EAAUC,QAAQH,GAAYC,IAE7BG,GAAoBC,EAAAA,UAAS,KAClCC,OAjCFC,EAiCkCR,EA/BlCS,OAAAC,OAAAD,OAAAC,OAAA,GACKF,GAAM,CACT,CAAAG,IAAKC,GAEH,MAAMC,EAAQC,EAAAA,qBAAqBF,GACnC,OAAOJ,EAAOG,EAAOF,OAAAC,OAAAD,OAAAC,OAAA,CAAA,EAAAG,IAAOE,QAAQ,IACrC,IATL,IACEP,CAiCiD,KAG1CQ,EAAeC,GAAoBX,EAAQA,SAACH,GAuCnD,OArCAe,EAAAA,WAAU,KACRD,GAAiB,EAAM,GACtB,IAEHE,EAAAA,SAAQ,KAIFhB,IACFH,EAAeoB,kBAAiB,GAChCpB,EAAeqB,cAAcnB,GAC7BF,E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D,EAAsBqE,SAAQ,CAC7B9B,MAAO,CAAE3E,OAAQuG,EAAWvD,QAASwD,IAEpCR,EAAO,EAGNlE,EAAAA,QAAA+D,cAACa,WAAS,CAAAX,SAAUA,GAAY,MAAOf,IAO7ClD,EAAAA,QAAA+D,cAACzD,EAAsBqE,SAAQ,CAC7B9B,MAAO,CAAE3E,OAAQuG,EAAWvD,QAASwD,IAEpCR,EAAUD,EAAWf,EAExB,sCQ1FsB2B,IACxB,MAAM3G,OAAEA,GAAWmC,KAEbQ,SAAEA,GAAaH,IASrB,OAPA9B,EAAAA,WAAU,KACR,MAAMkG,EAAYD,eAAAA,EAAQxF,KAAKkF,GAAMrG,EAAO6G,GAAGR,EAAG1D,KAClD,MAAO,KACLiE,SAAAA,EAAW9C,SAASgD,GAAaA,EAAS/C,eAAc,CACzD,GACA,CAAC4C,aAAA,EAAAA,EAAQlF,KAAK,OAEVzB,CAAM,kCCDa,CAC1B+C,EACAC,KAEA,MAAQ7C,EAAG4G,EAASzC,UAAEA,GAAcxB,EAAqBC,EAAIC,GAW7D,MAAO,CAAE7C,EATCyC,EAAAA,aACR,IAAI4B,KAEF,MAAMnE,EAAQC,EAAAA,qBAAqBkE,GACnC,OAAOuC,EAAU1G,EAAM,GAEzB,CAAC0G,IAGSzC,YAAW"}
1
+ {"version":3,"file":"tolgee-react.umd.min.js","sources":["../src/useTolgeeSSR.ts","../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/useTranslate.ts"],"sourcesContent":["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 * @param enabled if set to false, no action is taken\n */\nexport function useTolgeeSSR(\n tolgeeInstance: TolgeeInstance,\n language?: string,\n staticData?: TolgeeStaticData | undefined,\n enabled = true\n) {\n const [noWrappingTolgee] = useState(() =>\n getTolgeeWithDeactivatedWrapper(tolgeeInstance)\n );\n\n const [initialRender, setInitialRender] = useState(enabled);\n\n useEffect(() => {\n setInitialRender(false);\n }, []);\n\n useMemo(() => {\n if (enabled) {\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 tolgeeInstance.setEmitterActive(false);\n tolgeeInstance.addStaticData(staticData);\n tolgeeInstance.changeLanguage(language!);\n tolgeeInstance.setEmitterActive(true);\n }\n }, [language, staticData, tolgeeInstance]);\n\n useState(() => {\n // running this function only on first render\n if (!tolgeeInstance.isLoaded() && enabled) {\n // warning user, that static data provided are not sufficient\n // for proper SSR render\n const missingRecords = tolgeeInstance\n .getRequiredRecords(language)\n .map(({ namespace, language }) =>\n namespace ? `${namespace}:${language}` : language\n )\n .filter((key) => !staticData?.[key]);\n\n // eslint-disable-next-line no-console\n console.warn(\n `Tolgee: Missing records in \"staticData\" for proper SSR functionality: ${missingRecords.map((key) => `\"${key}\"`).join(', ')}`\n );\n }\n });\n\n return initialRender ? noWrappingTolgee : tolgeeInstance;\n}\n","import React, { Suspense, useEffect, useState } from 'react';\nimport { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';\nimport { ReactOptions, TolgeeReactContext } from './types';\nimport { useTolgeeSSR } from './useTolgeeSSR';\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\nlet LAST_TOLGEE_INSTANCE: TolgeeInstance | undefined = undefined;\n\nexport type SSROptions = {\n /**\n * Hard set language to this value, use together with `staticData`\n */\n language?: string;\n /**\n * If provided, static data will be hard set to Tolgee cache for initial render\n */\n staticData?: TolgeeStaticData;\n};\n\nexport interface TolgeeProviderProps {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n /**\n * use this option if you use SSR\n *\n * You can pass staticData and language\n * which will be set to tolgee instance for the initial render\n *\n * Don't switch between ssr and non-ssr dynamically\n */\n ssr?: SSROptions | boolean;\n}\n\nexport const TolgeeProvider: React.FC<TolgeeProviderProps> = ({\n tolgee,\n options,\n children,\n fallback,\n ssr,\n}) => {\n // prevent restarting tolgee unnecesarly\n // however if the instance change on hot-reloading\n // we want to restart\n useEffect(() => {\n if (LAST_TOLGEE_INSTANCE?.run !== tolgee.run) {\n if (LAST_TOLGEE_INSTANCE) {\n LAST_TOLGEE_INSTANCE.stop();\n }\n LAST_TOLGEE_INSTANCE = tolgee;\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 }\n }, [tolgee]);\n\n let tolgeeSSR = tolgee;\n\n const { language, staticData } = (\n typeof ssr !== 'object' ? {} : ssr\n ) as SSROptions;\n tolgeeSSR = useTolgeeSSR(tolgee, language, staticData, Boolean(ssr));\n\n const [loading, setLoading] = useState(!tolgeeSSR.isLoaded());\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: tolgeeSSR, 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: tolgeeSSR, 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 subscription.subscribeNs(namespaces);\n subscriptionQueue.current.forEach((ns) => {\n subscription!.subscribeNs(ns);\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [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 type { 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 { 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":["useTolgeeSSR","tolgeeInstance","language","staticData","enabled","noWrappingTolgee","useState","getTolgeeWithDeactivatedWrapper","tolgee","Object","assign","t","args","props","getTranslateProps","noWrap","initialRender","setInitialRender","useEffect","useMemo","setEmitterActive","addStaticData","changeLanguage","isLoaded","missingRecords","getRequiredRecords","map","namespace","filter","key","console","warn","join","DEFAULT_REACT_OPTIONS","useSuspense","ProviderInstance","getProviderInstance","React","createContext","undefined","LAST_TOLGEE_INSTANCE","globalContext","useTolgeeContext","TolgeeProviderContext","context","useContext","Error","useRerender","instance","setCounter","rerender","useCallback","num","useTranslateInternal","ns","options","defaultOptions","namespaces","getFallback","namespacesJoined","getFallbackArray","currentOptions","subscriptionRef","useRef","subscriptionQueue","current","subscription","onNsUpdate","subscribeNs","forEach","unsubscribe","addActiveNs","removeActiveNs","fallbackNs","_a","push","subscribeToNs","isLoading","wrapTagHandlers","params","result","entries","value","chunk","addReactKeys","isValidElement","el","children","length","cloneElement","val","Array","isArray","Children","toArray","TBase","keyName","error","defaultValue","translation","createElement","Fragment","fallback","ssr","run","stop","catch","e","finally","setLoading","tolgeeSSR","Boolean","loading","optionsWithDefault","Provider","Suspense","events","listeners","on","listener","tInternal"],"mappings":"saAgCM,SAAUA,EACdC,EACAC,EACAC,EACAC,GAAU,GAEV,MAAOC,GAAoBC,EAAAA,UAAS,KAClCC,OA/BFC,EA+BkCP,EA7BlCQ,OAAAC,OAAAD,OAAAC,OAAA,GACKF,GAAM,CACT,CAAAG,IAAKC,GAEH,MAAMC,EAAQC,EAAAA,qBAAqBF,GACnC,OAAOJ,EAAOG,EAAOF,OAAAC,OAAAD,OAAAC,OAAA,CAAA,EAAAG,IAAOE,QAAQ,IACrC,IATL,IACEP,CA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,EAAmB,CAAA,EAAKA,EAEjCO,EAAY9G,EAAaQ,EAAQN,EAAUC,EAAY4G,QAAQR,IAE/D,MAAOS,EAASH,GAAcvG,EAAQA,UAAEwG,EAAUvF,YAE5C0F,EAA0BxG,OAAAC,OAAAD,OAAAC,OAAA,GAAAuB,GAA0BsB,GAEpDZ,EAAwBP,IAE9B,OAAI6E,EAAmB/E,YAEnBG,UAAC+D,cAAAzD,EAAsBuE,SAAQ,CAC7BhC,MAAO,CAAE1E,OAAQsG,EAAWvD,QAAS0D,IAEpCD,EAAO,EAGN3E,EAAAA,QAAA+D,cAACe,WAAS,CAAAb,SAAUA,GAAY,MAAOf,IAO7ClD,EAAAA,QAAA+D,cAACzD,EAAsBuE,SAAQ,CAC7BhC,MAAO,CAAE1E,OAAQsG,EAAWvD,QAAS0D,IAEpCD,EAAUV,EAAWf,EAExB,sCQ1GsB6B,IACxB,MAAM5G,OAAEA,GAAWkC,KAEbQ,SAAEA,GAAaH,IASrB,OAPA7B,EAAAA,WAAU,KACR,MAAMmG,EAAYD,eAAAA,EAAQ1F,KAAKiF,GAAMnG,EAAO8G,GAAGX,EAAGzD,KAClD,MAAO,KACLmE,SAAAA,EAAWhD,SAASkD,GAAaA,EAASjD,eAAc,CACzD,GACA,CAAC8C,aAAA,EAAAA,EAAQpF,KAAK,OAEVxB,CAAM,kCCDa,CAC1B8C,EACAC,KAEA,MAAQ5C,EAAG6G,EAAS3C,UAAEA,GAAcxB,EAAqBC,EAAIC,GAW7D,MAAO,CAAE5C,EATCwC,EAAAA,aACR,IAAI4B,KAEF,MAAMlE,EAAQC,EAAAA,qBAAqBiE,GACnC,OAAOyC,EAAU3G,EAAM,GAEzB,CAAC2G,IAGS3C,YAAW"}
@@ -5,11 +5,10 @@ import { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';
5
5
  *
6
6
  * It also ensures that the first render is done without wrapping and so it avoids
7
7
  * "client different than server" issues.
8
- *
9
- * If no language data and static data are provided no action is taken
10
- *
8
+ * *
11
9
  * @param tolgeeInstance initialized Tolgee instance
12
10
  * @param language language that is obtained outside of Tolgee on the server and client
13
11
  * @param staticData static data for the language
12
+ * @param enabled if set to false, no action is taken
14
13
  */
15
- export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, language?: string, staticData?: TolgeeStaticData | undefined): TolgeeInstance;
14
+ export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, language?: string, staticData?: TolgeeStaticData | undefined, enabled?: boolean): TolgeeInstance;
@@ -3,11 +3,7 @@ import { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';
3
3
  import { ReactOptions, TolgeeReactContext } from './types';
4
4
  export declare const DEFAULT_REACT_OPTIONS: ReactOptions;
5
5
  export declare const getProviderInstance: () => React.Context<TolgeeReactContext | undefined>;
6
- export interface TolgeeProviderProps {
7
- children?: React.ReactNode;
8
- tolgee: TolgeeInstance;
9
- options?: ReactOptions;
10
- fallback?: React.ReactNode;
6
+ export type SSROptions = {
11
7
  /**
12
8
  * Hard set language to this value, use together with `staticData`
13
9
  */
@@ -16,5 +12,20 @@ export interface TolgeeProviderProps {
16
12
  * If provided, static data will be hard set to Tolgee cache for initial render
17
13
  */
18
14
  staticData?: TolgeeStaticData;
15
+ };
16
+ export interface TolgeeProviderProps {
17
+ children?: React.ReactNode;
18
+ tolgee: TolgeeInstance;
19
+ options?: ReactOptions;
20
+ fallback?: React.ReactNode;
21
+ /**
22
+ * use this option if you use SSR
23
+ *
24
+ * You can pass staticData and language
25
+ * which will be set to tolgee instance for the initial render
26
+ *
27
+ * Don't switch between ssr and non-ssr dynamically
28
+ */
29
+ ssr?: SSROptions | boolean;
19
30
  }
20
31
  export declare const TolgeeProvider: React.FC<TolgeeProviderProps>;
@@ -5,11 +5,10 @@ import { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';
5
5
  *
6
6
  * It also ensures that the first render is done without wrapping and so it avoids
7
7
  * "client different than server" issues.
8
- *
9
- * If no language data and static data are provided no action is taken
10
- *
8
+ * *
11
9
  * @param tolgeeInstance initialized Tolgee instance
12
10
  * @param language language that is obtained outside of Tolgee on the server and client
13
11
  * @param staticData static data for the language
12
+ * @param enabled if set to false, no action is taken
14
13
  */
15
- export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, language?: string, staticData?: TolgeeStaticData | undefined): TolgeeInstance;
14
+ export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, language?: string, staticData?: TolgeeStaticData | undefined, enabled?: boolean): TolgeeInstance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolgee/react",
3
- "version": "5.29.6-prerelease.4e3062df.0",
3
+ "version": "5.30.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.29.6-prerelease.4e3062df.0"
41
+ "@tolgee/web": "5.30.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.29.6-prerelease.4e3062df.0",
50
- "@tolgee/testing": "5.29.6-prerelease.4e3062df.0",
49
+ "@tolgee/format-icu": "5.30.0",
50
+ "@tolgee/testing": "5.30.0",
51
51
  "@types/jest": "^27.0.2",
52
52
  "@types/node": "^17.0.8",
53
53
  "@types/react": "^18.2.42",
@@ -88,5 +88,5 @@
88
88
  "access": "public"
89
89
  },
90
90
  "sideEffects": false,
91
- "gitHead": "6b02326cfb0ed610b6b21d816e3c088b9f74d1f6"
91
+ "gitHead": "9f57cdae5c2cb861fd7d4f96787b1ce16964f114"
92
92
  }
@@ -21,11 +21,7 @@ export const getProviderInstance = () => {
21
21
 
22
22
  let LAST_TOLGEE_INSTANCE: TolgeeInstance | undefined = undefined;
23
23
 
24
- export interface TolgeeProviderProps {
25
- children?: React.ReactNode;
26
- tolgee: TolgeeInstance;
27
- options?: ReactOptions;
28
- fallback?: React.ReactNode;
24
+ export type SSROptions = {
29
25
  /**
30
26
  * Hard set language to this value, use together with `staticData`
31
27
  */
@@ -34,6 +30,22 @@ export interface TolgeeProviderProps {
34
30
  * If provided, static data will be hard set to Tolgee cache for initial render
35
31
  */
36
32
  staticData?: TolgeeStaticData;
33
+ };
34
+
35
+ export interface TolgeeProviderProps {
36
+ children?: React.ReactNode;
37
+ tolgee: TolgeeInstance;
38
+ options?: ReactOptions;
39
+ fallback?: React.ReactNode;
40
+ /**
41
+ * use this option if you use SSR
42
+ *
43
+ * You can pass staticData and language
44
+ * which will be set to tolgee instance for the initial render
45
+ *
46
+ * Don't switch between ssr and non-ssr dynamically
47
+ */
48
+ ssr?: SSROptions | boolean;
37
49
  }
38
50
 
39
51
  export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
@@ -41,11 +53,8 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
41
53
  options,
42
54
  children,
43
55
  fallback,
44
- staticData,
45
- language,
56
+ ssr,
46
57
  }) => {
47
- const [loading, setLoading] = useState(!tolgee.isLoaded());
48
-
49
58
  // prevent restarting tolgee unnecesarly
50
59
  // however if the instance change on hot-reloading
51
60
  // we want to restart
@@ -67,7 +76,14 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
67
76
  }
68
77
  }, [tolgee]);
69
78
 
70
- const tolgeeSSR = useTolgeeSSR(tolgee, language, staticData);
79
+ let tolgeeSSR = tolgee;
80
+
81
+ const { language, staticData } = (
82
+ typeof ssr !== 'object' ? {} : ssr
83
+ ) as SSROptions;
84
+ tolgeeSSR = useTolgeeSSR(tolgee, language, staticData, Boolean(ssr));
85
+
86
+ const [loading, setLoading] = useState(!tolgeeSSR.isLoaded());
71
87
 
72
88
  const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options };
73
89
 
@@ -2,7 +2,6 @@ import {
2
2
  getTranslateProps,
3
3
  TolgeeInstance,
4
4
  TolgeeStaticData,
5
- isSSR,
6
5
  } from '@tolgee/web';
7
6
  import { useEffect, useMemo, useState } from 'react';
8
7
 
@@ -25,20 +24,18 @@ function getTolgeeWithDeactivatedWrapper(
25
24
  *
26
25
  * It also ensures that the first render is done without wrapping and so it avoids
27
26
  * "client different than server" issues.
28
- *
29
- * If no language data and static data are provided no action is taken
30
- *
27
+ * *
31
28
  * @param tolgeeInstance initialized Tolgee instance
32
29
  * @param language language that is obtained outside of Tolgee on the server and client
33
30
  * @param staticData static data for the language
31
+ * @param enabled if set to false, no action is taken
34
32
  */
35
33
  export function useTolgeeSSR(
36
34
  tolgeeInstance: TolgeeInstance,
37
35
  language?: string,
38
- staticData?: TolgeeStaticData | undefined
36
+ staticData?: TolgeeStaticData | undefined,
37
+ enabled = true
39
38
  ) {
40
- const enabled = Boolean(language || staticData);
41
-
42
39
  const [noWrappingTolgee] = useState(() =>
43
40
  getTolgeeWithDeactivatedWrapper(tolgeeInstance)
44
41
  );
@@ -50,10 +47,10 @@ export function useTolgeeSSR(
50
47
  }, []);
51
48
 
52
49
  useMemo(() => {
53
- // we have to prepare tolgee before rendering children
54
- // so translations are available right away
55
- // events emitting must be off, to not trigger re-render while rendering
56
50
  if (enabled) {
51
+ // we have to prepare tolgee before rendering children
52
+ // so translations are available right away
53
+ // events emitting must be off, to not trigger re-render while rendering
57
54
  tolgeeInstance.setEmitterActive(false);
58
55
  tolgeeInstance.addStaticData(staticData);
59
56
  tolgeeInstance.changeLanguage(language!);
@@ -62,23 +59,21 @@ export function useTolgeeSSR(
62
59
  }, [language, staticData, tolgeeInstance]);
63
60
 
64
61
  useState(() => {
65
- if (enabled && isSSR()) {
66
- // running this function only on first render
67
- if (!tolgeeInstance.isLoaded()) {
68
- // warning user, that static data provided are not sufficient
69
- // for proper SSR render
70
- const missingRecords = tolgeeInstance
71
- .getRequiredRecords(language)
72
- .map(({ namespace, language }) =>
73
- namespace ? `${namespace}:${language}` : language
74
- )
75
- .filter((key) => !staticData?.[key]);
62
+ // running this function only on first render
63
+ if (!tolgeeInstance.isLoaded() && enabled) {
64
+ // warning user, that static data provided are not sufficient
65
+ // for proper SSR render
66
+ const missingRecords = tolgeeInstance
67
+ .getRequiredRecords(language)
68
+ .map(({ namespace, language }) =>
69
+ namespace ? `${namespace}:${language}` : language
70
+ )
71
+ .filter((key) => !staticData?.[key]);
76
72
 
77
- // eslint-disable-next-line no-console
78
- console.warn(
79
- `Tolgee: Missing records in "staticData" for proper SSR functionality: ${missingRecords.map((key) => `"${key}"`).join(', ')}`
80
- );
81
- }
73
+ // eslint-disable-next-line no-console
74
+ console.warn(
75
+ `Tolgee: Missing records in "staticData" for proper SSR functionality: ${missingRecords.map((key) => `"${key}"`).join(', ')}`
76
+ );
82
77
  }
83
78
  });
84
79