@tolgee/react 5.6.1 → 5.7.0-prerelease.134377fa.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.
- package/dist/T.d.ts +16 -5
- package/dist/tolgee-react.cjs.js +5 -4
- package/dist/tolgee-react.cjs.js.map +1 -1
- package/dist/tolgee-react.cjs.min.js +1 -1
- package/dist/tolgee-react.cjs.min.js.map +1 -1
- package/dist/tolgee-react.esm.js +5 -4
- package/dist/tolgee-react.esm.js.map +1 -1
- package/dist/tolgee-react.esm.min.js +1 -1
- package/dist/tolgee-react.esm.min.js.map +1 -1
- package/dist/tolgee-react.esm.min.mjs +1 -1
- package/dist/tolgee-react.esm.min.mjs.map +1 -1
- package/dist/tolgee-react.esm.mjs +5 -4
- package/dist/tolgee-react.esm.mjs.map +1 -1
- package/dist/tolgee-react.umd.js +5 -4
- package/dist/tolgee-react.umd.js.map +1 -1
- package/dist/tolgee-react.umd.min.js +1 -1
- package/dist/tolgee-react.umd.min.js.map +1 -1
- package/dist/useTolgeeSSR.d.ts +1 -53
- package/dist/useTranslate.d.ts +5 -3
- package/lib/T.d.ts +16 -5
- package/lib/useTolgeeSSR.d.ts +1 -53
- package/lib/useTranslate.d.ts +5 -3
- package/package.json +5 -5
- package/src/T.tsx +22 -8
- package/src/__integration/T.spec.tsx +0 -32
- package/src/tagsTools.tsx +3 -3
- package/src/useTranslate.ts +13 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tolgee-react.esm.mjs","sources":["../src/TolgeeProvider.tsx","../src/GlobalContextPlugin.tsx","../src/useTolgeeContext.ts","../src/hooks.ts","../src/useTranslateInternal.ts","../src/useTranslate.ts","../src/tagsTools.tsx","../src/T.tsx","../src/useTolgee.ts","../src/useTolgeeSSR.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\ntype Props = {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n};\n\nexport const TolgeeProvider: React.FC<Props> = ({\n tolgee,\n options,\n children,\n fallback,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n useEffect(() => {\n tolgee.run().finally(() => {\n setLoading(false);\n });\n return () => {\n tolgee.stop();\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 { useCallback } from 'react';\nimport { TFnType, getTranslateProps, DefaultParamType } from '@tolgee/web';\n\nimport { useTranslateInternal } from './useTranslateInternal';\nimport { ReactOptions } from './types';\n\nexport const useTranslate = (\n ns?: string[] | string,\n options?: ReactOptions\n) => {\n const { t: tInternal, isLoading } = useTranslateInternal(ns, options);\n\n const t: TFnType<DefaultParamType, string> = 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 { NsType, TranslateParams } from '@tolgee/web';\nimport React, { FunctionComponent } from 'react';\nimport { addReactKeys, wrapTagHandlers } from './tagsTools';\n\nimport { ParamsTags } from './types';\nimport { useTranslateInternal } from './useTranslateInternal';\n\ntype TProps = {\n params?: TranslateParams<ParamsTags>;\n children?: string;\n noWrap?: boolean;\n keyName?: string;\n ns?: NsType;\n defaultValue?: string;\n};\n\nexport const T: FunctionComponent<TProps> = (props: TProps) => {\n const key = props.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 || (props.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 })\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.setEmmiterActive(false);\n tolgee.addStaticData(staticData);\n tolgee.changeLanguage(locale!);\n tolgee.setEmmiterActive(true);\n }, [locale, staticData, tolgee]);\n\n return tolgee;\n}\n"],"names":[],"mappings":";;;;AAIO,MAAM,qBAAqB,GAAiB;AACjD,IAAA,WAAW,EAAE,IAAI;CAClB,CAAC;AAEW,M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,SAAS,CAAC,MAAK;QACb,SAAS,CAAC,cAAc,CAAC,CAAC;KAC3B,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,MAAK;;;;AAIX,QAAA,MAAM,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAC/B,QAAA,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;AACjC,QAAA,MAAM,CAAC,cAAc,CAAC,MAAO,CAAC,CAAC;AAC/B,QAAA,MAAM,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;KAC/B,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;AAEjC,IAAA,OAAO,MAAM,CAAC;AAChB;;;;"}
|
|
1
|
+
{"version":3,"file":"tolgee-react.esm.mjs","sources":["../src/TolgeeProvider.tsx","../src/GlobalContextPlugin.tsx","../src/useTolgeeContext.ts","../src/hooks.ts","../src/useTranslateInternal.ts","../src/useTranslate.ts","../src/tagsTools.tsx","../src/T.tsx","../src/useTolgee.ts","../src/useTolgeeSSR.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\ntype Props = {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n};\n\nexport const TolgeeProvider: React.FC<Props> = ({\n tolgee,\n options,\n children,\n fallback,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n useEffect(() => {\n tolgee.run().finally(() => {\n setLoading(false);\n });\n return () => {\n tolgee.stop();\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 { 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\ntype 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\n ? React.cloneElement(el)\n : React.cloneElement(el, {}, addReactKeys(chunk));\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\ntype PropsWithKeyName = {\n params?: TranslateParams<ParamsTags>;\n children?: string;\n noWrap?: boolean;\n keyName: TranslationKey;\n ns?: NsType;\n defaultValue?: string;\n};\n\ntype PropsWithoutKeyName = {\n params?: TranslateParams<ParamsTags>;\n children: TranslationKey;\n noWrap?: boolean;\n ns?: NsType;\n defaultValue?: string;\n};\n\ninterface TInterface {\n (props: PropsWithKeyName): JSX.Element;\n (props: PropsWithoutKeyName): 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 })\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.setEmmiterActive(false);\n tolgee.addStaticData(staticData);\n tolgee.changeLanguage(locale!);\n tolgee.setEmmiterActive(true);\n }, [locale, staticData, tolgee]);\n\n return tolgee;\n}\n"],"names":[],"mappings":";;;;AAIO,MAAM,qBAAqB,GAAiB;AACjD,IAAA,WAAW,EAAE,IAAI;CAClB,CAAC;AAEW,MAAA,qBAAqB,GAAG,KAAK,CAAC,aAAa,CAEtD,SAAS,EAAE;AASN,MAAM,cAAc,GAAoB,CAAC,EAC9C,MAAM,EACN,OAAO,EACP,QAAQ,EACR,QAAQ,GACT,KAAI;AACH,IAAA,MAAM,CAAC,OAAO,EAAE,UA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O,CAAC,CAAC;AAC/B,QAAA,MAAM,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;KAC/B,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;AAEjC,IAAA,OAAO,MAAM,CAAC;AAChB;;;;"}
|
package/dist/tolgee-react.umd.js
CHANGED
|
@@ -126,9 +126,9 @@
|
|
|
126
126
|
else if (React__default["default"].isValidElement(value)) {
|
|
127
127
|
const el = value;
|
|
128
128
|
result[key] = (chunk) => {
|
|
129
|
-
return el.props.children
|
|
130
|
-
? React__default["default"].cloneElement(el
|
|
131
|
-
: React__default["default"].cloneElement(el);
|
|
129
|
+
return el.props.children !== undefined
|
|
130
|
+
? React__default["default"].cloneElement(el)
|
|
131
|
+
: React__default["default"].cloneElement(el, {}, addReactKeys(chunk));
|
|
132
132
|
};
|
|
133
133
|
}
|
|
134
134
|
else {
|
|
@@ -152,7 +152,8 @@
|
|
|
152
152
|
// eslint-disable-next-line no-console
|
|
153
153
|
console.error('T component: keyName not defined');
|
|
154
154
|
}
|
|
155
|
-
const defaultValue = props.defaultValue ||
|
|
155
|
+
const defaultValue = props.defaultValue ||
|
|
156
|
+
(props.keyName ? props.children : undefined);
|
|
156
157
|
const { t } = useTranslateInternal();
|
|
157
158
|
const translation = addReactKeys(t({
|
|
158
159
|
key: key,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tolgee-react.umd.js","sources":["../src/TolgeeProvider.tsx","../src/GlobalContextPlugin.tsx","../src/useTolgeeContext.ts","../src/hooks.ts","../src/useTranslateInternal.ts","../src/useTranslate.ts","../src/tagsTools.tsx","../src/T.tsx","../src/useTolgee.ts","../src/useTolgeeSSR.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\ntype Props = {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n};\n\nexport const TolgeeProvider: React.FC<Props> = ({\n tolgee,\n options,\n children,\n fallback,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n useEffect(() => {\n tolgee.run().finally(() => {\n setLoading(false);\n });\n return () => {\n tolgee.stop();\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 { useCallback } from 'react';\nimport { TFnType, getTranslateProps, DefaultParamType } from '@tolgee/web';\n\nimport { useTranslateInternal } from './useTranslateInternal';\nimport { ReactOptions } from './types';\n\nexport const useTranslate = (\n ns?: string[] | string,\n options?: ReactOptions\n) => {\n const { t: tInternal, isLoading } = useTranslateInternal(ns, options);\n\n const t: TFnType<DefaultParamType, string> = 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 { NsType, TranslateParams } from '@tolgee/web';\nimport React, { FunctionComponent } from 'react';\nimport { addReactKeys, wrapTagHandlers } from './tagsTools';\n\nimport { ParamsTags } from './types';\nimport { useTranslateInternal } from './useTranslateInternal';\n\ntype TProps = {\n params?: TranslateParams<ParamsTags>;\n children?: string;\n noWrap?: boolean;\n keyName?: string;\n ns?: NsType;\n defaultValue?: string;\n};\n\nexport const T: FunctionComponent<TProps> = (props: TProps) => {\n const key = props.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 || (props.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 })\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.setEmmiterActive(false);\n tolgee.addStaticData(staticData);\n tolgee.changeLanguage(locale!);\n tolgee.setEmmiterActive(true);\n }, [locale, staticData, tolgee]);\n\n return tolgee;\n}\n"],"names":["React","useState","useEffect","Suspense","useContext","useCallback","getFallback","getFallbackArray","useRef","getTranslateProps","useMemo"],"mappings":";;;;;;;;;;IAIO,MAAM,qBAAqB,GAAiB;IACjD,IAAA,WAAW,EAAE,IAAI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M,CAAC,MAAM,EAAE,SAAS,CAAC,GAAGT,cAAQ,CAAC,eAAe,CAAC,CAAC;QAEtDC,eAAS,CAAC,MAAK;YACb,SAAS,CAAC,cAAc,CAAC,CAAC;SAC3B,EAAE,EAAE,CAAC,CAAC;QAEPQ,aAAO,CAAC,MAAK;;;;IAIX,QAAA,MAAM,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAC/B,QAAA,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IACjC,QAAA,MAAM,CAAC,cAAc,CAAC,MAAO,CAAC,CAAC;IAC/B,QAAA,MAAM,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;SAC/B,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;IAEjC,IAAA,OAAO,MAAM,CAAC;IAChB;;;;;;;;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"tolgee-react.umd.js","sources":["../src/TolgeeProvider.tsx","../src/GlobalContextPlugin.tsx","../src/useTolgeeContext.ts","../src/hooks.ts","../src/useTranslateInternal.ts","../src/useTranslate.ts","../src/tagsTools.tsx","../src/T.tsx","../src/useTolgee.ts","../src/useTolgeeSSR.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\ntype Props = {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n};\n\nexport const TolgeeProvider: React.FC<Props> = ({\n tolgee,\n options,\n children,\n fallback,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n useEffect(() => {\n tolgee.run().finally(() => {\n setLoading(false);\n });\n return () => {\n tolgee.stop();\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 { 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\ntype 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\n ? React.cloneElement(el)\n : React.cloneElement(el, {}, addReactKeys(chunk));\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\ntype PropsWithKeyName = {\n params?: TranslateParams<ParamsTags>;\n children?: string;\n noWrap?: boolean;\n keyName: TranslationKey;\n ns?: NsType;\n defaultValue?: string;\n};\n\ntype PropsWithoutKeyName = {\n params?: TranslateParams<ParamsTags>;\n children: TranslationKey;\n noWrap?: boolean;\n ns?: NsType;\n defaultValue?: string;\n};\n\ninterface TInterface {\n (props: PropsWithKeyName): JSX.Element;\n (props: PropsWithoutKeyName): 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 })\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.setEmmiterActive(false);\n tolgee.addStaticData(staticData);\n tolgee.changeLanguage(locale!);\n tolgee.setEmmiterActive(true);\n }, [locale, staticData, tolgee]);\n\n return tolgee;\n}\n"],"names":["React","useState","useEffect","Suspense","useContext","useCallback","getFallback","getFallbackArray","useRef","getTranslateProps","useMemo"],"mappings":";;;;;;;;;;IAIO,MAAM,qBAAqB,GAAiB;IACjD,IAAA,WAAW,EAAE,IAAI;KAClB,CAAC;AAEW,UAAA,qBAAq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,cAAc,CAAC,CAAC;SAC3B,EAAE,EAAE,CAAC,CAAC;QAEPQ,aAAO,CAAC,MAAK;;;;IAIX,QAAA,MAAM,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAC/B,QAAA,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IACjC,QAAA,MAAM,CAAC,cAAc,CAAC,MAAO,CAAC,CAAC;IAC/B,QAAA,MAAM,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;SAC/B,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;IAEjC,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);const o={useSuspense:!0},a=r.default.createContext(void 0);let u;const
|
|
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);const o={useSuspense:!0},a=r.default.createContext(void 0);let u;const c=()=>{const e=t.useContext(a)||u;if(!e)throw new Error("Couldn't find tolgee instance, did you forgot to use `TolgeeProvider`?");return e},l=()=>{const[e,n]=t.useState(0);return{instance:e,rerender:t.useCallback((()=>{n((e=>e+1))}),[n])}},i=(e,s)=>{const{tolgee:r,options:o}=c(),a=n.getFallback(e),u=n.getFallbackArray(a).join(":"),i=Object.assign(Object.assign({},o),s),{rerender:d,instance:f}=l(),g=t.useRef(),b=t.useRef([]);b.current=[];const p=r.isLoaded(a);t.useEffect((()=>{const e=r.onNsUpdate(d);return g.current=e,p||e.subscribeNs(a),b.current.forEach((t=>{e.subscribeNs(t)})),()=>{e.unsubscribe()}}),[p,u,r]),t.useEffect((()=>(r.addActiveNs(a),()=>r.removeActiveNs(a))),[u,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,f]);if(i.useSuspense&&!p)throw r.addActiveNs(a,!0);return{t:v,isLoading:!p}},d=e=>{if(!e)return;const t={};return Object.entries(e||{}).forEach((([e,n])=>{if("function"==typeof n)t[e]=e=>n(f(e));else if(r.default.isValidElement(n)){const s=n;t[e]=e=>void 0!==s.props.children?r.default.cloneElement(s):r.default.cloneElement(s,{},f(e))}else t[e]=n})),t},f=e=>Array.isArray(e)?r.default.Children.toArray(e):e;e.GlobalContextPlugin=e=>t=>(u={tolgee:t,options:Object.assign(Object.assign({},o),e)},t),e.T=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),{t:s}=i(),o=f(s({key:t,params:d(e.params),defaultValue:n,noWrap:e.noWrap,ns:e.ns}));return r.default.createElement(r.default.Fragment,null,o)},e.TolgeeProvider=({tolgee:e,options:n,children:s,fallback:u})=>{const[c,l]=t.useState(!e.isLoaded());t.useEffect((()=>(e.run().finally((()=>{l(!1)})),()=>{e.stop()})),[e]);const i=Object.assign(Object.assign({},o),n);return i.useSuspense?r.default.createElement(a.Provider,{value:{tolgee:e,options:i}},c?u:r.default.createElement(t.Suspense,{fallback:u||null},s)):r.default.createElement(a.Provider,{value:{tolgee:e,options:i}},c?u:s)},e.TolgeeProviderContext=a,e.useTolgee=e=>{const{tolgee:n}=c(),{rerender:s}=l();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=function(e,s,r){const o=t.useMemo((()=>function(e){return Object.assign(Object.assign({},e),{t(...t){const s=n.getTranslateProps(...t);return e.t(Object.assign(Object.assign({},s),{noWrap:!0}))}})}(e)),[]),[a,u]=t.useState(o);return t.useEffect((()=>{u(e)}),[]),t.useMemo((()=>{a.setEmmiterActive(!1),a.addStaticData(r),a.changeLanguage(s),a.setEmmiterActive(!0)}),[s,r,a]),a},e.useTranslate=(e,s)=>{const{t:r,isLoading:o}=i(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/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\ntype Props = {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n};\n\nexport const TolgeeProvider: React.FC<Props> = ({\n tolgee,\n options,\n children,\n fallback,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n useEffect(() => {\n tolgee.run().finally(() => {\n setLoading(false);\n });\n return () => {\n tolgee.stop();\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 } from '@tolgee/web';\nimport React, { FunctionComponent } from 'react';\nimport { addReactKeys, wrapTagHandlers } from './tagsTools';\n\nimport { ParamsTags } from './types';\nimport { useTranslateInternal } from './useTranslateInternal';\n\ntype TProps = {\n params?: TranslateParams<ParamsTags>;\n children?: string;\n noWrap?: boolean;\n keyName?: string;\n ns?: NsType;\n defaultValue?: string;\n};\n\nexport const T: FunctionComponent<TProps> = (props: TProps) => {\n const key = props.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 || (props.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 })\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.setEmmiterActive(false);\n tolgee.addStaticData(staticData);\n tolgee.changeLanguage(locale!);\n tolgee.setEmmiterActive(true);\n }, [locale, staticData, tolgee]);\n\n return tolgee;\n}\n","import { useCallback } from 'react';\nimport { TFnType, getTranslateProps, DefaultParamType } from '@tolgee/web';\n\nimport { useTranslateInternal } from './useTranslateInternal';\nimport { ReactOptions } from './types';\n\nexport const useTranslate = (\n ns?: string[] | string,\n options?: ReactOptions\n) => {\n const { t: tInternal, isLoading } = useTranslateInternal(ns, options);\n\n const t: TFnType<DefaultParamType, string> = 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","createElement","Fragment","fallback","loading","setLoading","run","finally","stop","optionsWithDefault","Provider","Suspense","events","listeners","map","e","on","listener","tolgeeInstance","locale","staticData","initialInstance","useMemo","args","getTranslateProps","getTolgeeWithDeactivatedWrapper","setTolgee","setEmmiterActive","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,EAAa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aA,EAASlD,eAAc,CACzD,GACA,CAAC6C,aAAA,EAAAA,EAAQ3D,KAAK,OAEVN,CAAM,0BCIbuE,EACAC,EACAC,GAEA,MAAMC,EAAkBC,EAAAA,SACtB,IAnBJ,SACE3E,GAEA,OAAAQ,OAAAC,OAAAD,OAAAC,OAAA,GACKT,GAAM,CACTuB,KAAKqD,GAEH,MAAMpD,EAAQqD,EAAAA,qBAAqBD,GACnC,OAAO5E,EAAOuB,EAAOf,OAAAC,OAAAD,OAAAC,OAAA,CAAA,EAAAe,IAAO6B,QAAQ,IACrC,GAEL,CAQUyB,CAAgCP,IACtC,KAGKvE,EAAQ+E,GAAatF,EAAQA,SAACiF,GAgBrC,OAdA3D,EAAAA,WAAU,KACRgE,EAAUR,EAAe,GACxB,IAEHI,EAAAA,SAAQ,KAIN3E,EAAOgF,kBAAiB,GACxBhF,EAAOiF,cAAcR,GACrBzE,EAAOkF,eAAeV,GACtBxE,EAAOgF,kBAAiB,EAAK,GAC5B,CAACR,EAAQC,EAAYzE,IAEjBA,CACT,iBCzC4B,CAC1BF,EACAC,KAEA,MAAQwB,EAAG4D,EAAStD,UAAEA,GAAchC,EAAqBC,EAAIC,GAW7D,MAAO,CAAEwB,EAToC5B,EAAAA,aAC3C,IAAIoC,KAEF,MAAMP,EAAQqD,EAAAA,qBAAqB9C,GACnC,OAAOoD,EAAU3D,EAAM,GAEzB,CAAC2D,IAGStD,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/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\ntype Props = {\n children?: React.ReactNode;\n tolgee: TolgeeInstance;\n options?: ReactOptions;\n fallback?: React.ReactNode;\n};\n\nexport const TolgeeProvider: React.FC<Props> = ({\n tolgee,\n options,\n children,\n fallback,\n}) => {\n const [loading, setLoading] = useState(!tolgee.isLoaded());\n\n useEffect(() => {\n tolgee.run().finally(() => {\n setLoading(false);\n });\n return () => {\n tolgee.stop();\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\n ? React.cloneElement(el)\n : React.cloneElement(el, {}, addReactKeys(chunk));\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\ntype PropsWithKeyName = {\n params?: TranslateParams<ParamsTags>;\n children?: string;\n noWrap?: boolean;\n keyName: TranslationKey;\n ns?: NsType;\n defaultValue?: string;\n};\n\ntype PropsWithoutKeyName = {\n params?: TranslateParams<ParamsTags>;\n children: TranslationKey;\n noWrap?: boolean;\n ns?: NsType;\n defaultValue?: string;\n};\n\ninterface TInterface {\n (props: PropsWithKeyName): JSX.Element;\n (props: PropsWithoutKeyName): 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 })\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.setEmmiterActive(false);\n tolgee.addStaticData(staticData);\n tolgee.changeLanguage(locale!);\n tolgee.setEmmiterActive(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\ntype 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","cloneElement","val","Array","isArray","Children","toArray","keyName","console","error","defaultValue","translation","noWrap","createElement","Fragment","fallback","loading","setLoading","run","finally","stop","optionsWithDefault","Provider","Suspense","events","listeners","map","e","on","listener","tolgeeInstance","locale","staticData","initialInstance","useMemo","args","getTranslateProps","getTolgeeWithDeactivatedWrapper","setTolgee","setEmmiterActive","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,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AqB7C,GACnC,OAAOmD,EAAU1D,EAAM,GAEzB,CAAC0D,IAGSrD,YAAW"}
|
package/dist/useTolgeeSSR.d.ts
CHANGED
|
@@ -1,54 +1,2 @@
|
|
|
1
1
|
import { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';
|
|
2
|
-
export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, locale?: string, staticData?: TolgeeStaticData | undefined):
|
|
3
|
-
on: import("@tolgee/web").TolgeeOn<keyof import("@tolgee/web").EventType>;
|
|
4
|
-
onNsUpdate: (handler: import("@tolgee/web").Listener<undefined>) => import("@tolgee/web").SubscriptionSelective;
|
|
5
|
-
setEmmiterActive: (active: boolean) => void;
|
|
6
|
-
getLanguage: () => string | undefined;
|
|
7
|
-
getPendingLanguage: () => string | undefined;
|
|
8
|
-
changeLanguage: (language: string) => Promise<void>;
|
|
9
|
-
changeTranslation: (descriptor: import("@tolgee/web").CacheDescriptor, key: string, value: string) => {
|
|
10
|
-
revert: () => void;
|
|
11
|
-
};
|
|
12
|
-
addActiveNs: (ns: import("@tolgee/web").NsFallback, forget?: boolean | undefined) => Promise<void>;
|
|
13
|
-
removeActiveNs: (ns: import("@tolgee/web").NsFallback) => void;
|
|
14
|
-
loadRecords: (descriptors: import("@tolgee/web").CacheDescriptor[]) => Promise<import("@tolgee/web").TranslationsFlat[]>;
|
|
15
|
-
loadRecord: (descriptor: import("@tolgee/web").CacheDescriptor) => Promise<import("@tolgee/web").TranslationsFlat>;
|
|
16
|
-
addStaticData: (data: TolgeeStaticData | undefined) => void;
|
|
17
|
-
getRecord: (descriptor: import("@tolgee/web").CacheDescriptor) => import("@tolgee/web").TranslationsFlat | undefined;
|
|
18
|
-
getAllRecords: () => {
|
|
19
|
-
data: import("@tolgee/web").TranslationsFlat;
|
|
20
|
-
language: string;
|
|
21
|
-
namespace: string;
|
|
22
|
-
}[];
|
|
23
|
-
isLoaded: (ns?: import("@tolgee/web").NsFallback) => boolean;
|
|
24
|
-
isInitialLoading: () => boolean;
|
|
25
|
-
isLoading: (ns?: import("@tolgee/web").NsFallback) => boolean;
|
|
26
|
-
isFetching: (ns?: import("@tolgee/web").NsFallback) => boolean;
|
|
27
|
-
isRunning: () => boolean;
|
|
28
|
-
run: () => Promise<void | undefined>;
|
|
29
|
-
stop: () => void;
|
|
30
|
-
t: import("@tolgee/web").TFnType<import("@tolgee/web").DefaultParamType, string>;
|
|
31
|
-
highlight: import("@tolgee/web").HighlightInterface;
|
|
32
|
-
getInitialOptions: () => {
|
|
33
|
-
apiUrl?: string | undefined;
|
|
34
|
-
apiKey?: string | undefined;
|
|
35
|
-
projectId?: string | number | undefined;
|
|
36
|
-
language?: string | undefined;
|
|
37
|
-
defaultLanguage?: string | undefined;
|
|
38
|
-
availableLanguages?: string[] | undefined;
|
|
39
|
-
fallbackLanguage?: import("@tolgee/web").FallbackLanguageOption;
|
|
40
|
-
ns?: string[] | undefined;
|
|
41
|
-
fallbackNs?: import("@tolgee/web").FallbackGeneral;
|
|
42
|
-
defaultNs: string;
|
|
43
|
-
staticData?: TolgeeStaticData | undefined;
|
|
44
|
-
observerType: "text" | "invisible";
|
|
45
|
-
observerOptions: import("@tolgee/web").ObserverOptionsInternal;
|
|
46
|
-
onFormatError: import("@tolgee/web").OnFormatError;
|
|
47
|
-
};
|
|
48
|
-
isDev: () => boolean;
|
|
49
|
-
wrap: (params: import("@tolgee/web").WrapperWrapProps) => string | undefined;
|
|
50
|
-
unwrap: (text: string) => import("@tolgee/web").Unwrapped;
|
|
51
|
-
overrideCredentials(credentials: import("@tolgee/web").DevCredentials): void;
|
|
52
|
-
addPlugin(plugin: import("@tolgee/web").TolgeePlugin | undefined): void;
|
|
53
|
-
updateOptions(options?: import("@tolgee/web").TolgeeOptions | undefined): void;
|
|
54
|
-
}>;
|
|
2
|
+
export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, locale?: string, staticData?: TolgeeStaticData | undefined): TolgeeInstance;
|
package/dist/useTranslate.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { TFnType, DefaultParamType } from '@tolgee/web';
|
|
1
|
+
import { TFnType, DefaultParamType, TranslationKey } from '@tolgee/web';
|
|
2
2
|
import { ReactOptions } from './types';
|
|
3
|
-
|
|
4
|
-
t: TFnType<DefaultParamType, string>;
|
|
3
|
+
type UseTranslateResult = {
|
|
4
|
+
t: TFnType<DefaultParamType, string, TranslationKey>;
|
|
5
5
|
isLoading: boolean;
|
|
6
6
|
};
|
|
7
|
+
export declare const useTranslate: (ns?: string[] | string, options?: ReactOptions) => UseTranslateResult;
|
|
8
|
+
export {};
|
package/lib/T.d.ts
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { NsType, TranslateParams, TranslationKey } from '@tolgee/web';
|
|
3
3
|
import { ParamsTags } from './types';
|
|
4
|
-
type
|
|
4
|
+
type PropsWithKeyName = {
|
|
5
5
|
params?: TranslateParams<ParamsTags>;
|
|
6
6
|
children?: string;
|
|
7
7
|
noWrap?: boolean;
|
|
8
|
-
keyName
|
|
8
|
+
keyName: TranslationKey;
|
|
9
9
|
ns?: NsType;
|
|
10
10
|
defaultValue?: string;
|
|
11
11
|
};
|
|
12
|
-
|
|
12
|
+
type PropsWithoutKeyName = {
|
|
13
|
+
params?: TranslateParams<ParamsTags>;
|
|
14
|
+
children: TranslationKey;
|
|
15
|
+
noWrap?: boolean;
|
|
16
|
+
ns?: NsType;
|
|
17
|
+
defaultValue?: string;
|
|
18
|
+
};
|
|
19
|
+
interface TInterface {
|
|
20
|
+
(props: PropsWithKeyName): JSX.Element;
|
|
21
|
+
(props: PropsWithoutKeyName): JSX.Element;
|
|
22
|
+
}
|
|
23
|
+
export declare const T: TInterface;
|
|
13
24
|
export {};
|
package/lib/useTolgeeSSR.d.ts
CHANGED
|
@@ -1,54 +1,2 @@
|
|
|
1
1
|
import { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';
|
|
2
|
-
export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, locale?: string, staticData?: TolgeeStaticData | undefined):
|
|
3
|
-
on: import("@tolgee/web").TolgeeOn<keyof import("@tolgee/web").EventType>;
|
|
4
|
-
onNsUpdate: (handler: import("@tolgee/web").Listener<undefined>) => import("@tolgee/web").SubscriptionSelective;
|
|
5
|
-
setEmmiterActive: (active: boolean) => void;
|
|
6
|
-
getLanguage: () => string | undefined;
|
|
7
|
-
getPendingLanguage: () => string | undefined;
|
|
8
|
-
changeLanguage: (language: string) => Promise<void>;
|
|
9
|
-
changeTranslation: (descriptor: import("@tolgee/web").CacheDescriptor, key: string, value: string) => {
|
|
10
|
-
revert: () => void;
|
|
11
|
-
};
|
|
12
|
-
addActiveNs: (ns: import("@tolgee/web").NsFallback, forget?: boolean | undefined) => Promise<void>;
|
|
13
|
-
removeActiveNs: (ns: import("@tolgee/web").NsFallback) => void;
|
|
14
|
-
loadRecords: (descriptors: import("@tolgee/web").CacheDescriptor[]) => Promise<import("@tolgee/web").TranslationsFlat[]>;
|
|
15
|
-
loadRecord: (descriptor: import("@tolgee/web").CacheDescriptor) => Promise<import("@tolgee/web").TranslationsFlat>;
|
|
16
|
-
addStaticData: (data: TolgeeStaticData | undefined) => void;
|
|
17
|
-
getRecord: (descriptor: import("@tolgee/web").CacheDescriptor) => import("@tolgee/web").TranslationsFlat | undefined;
|
|
18
|
-
getAllRecords: () => {
|
|
19
|
-
data: import("@tolgee/web").TranslationsFlat;
|
|
20
|
-
language: string;
|
|
21
|
-
namespace: string;
|
|
22
|
-
}[];
|
|
23
|
-
isLoaded: (ns?: import("@tolgee/web").NsFallback) => boolean;
|
|
24
|
-
isInitialLoading: () => boolean;
|
|
25
|
-
isLoading: (ns?: import("@tolgee/web").NsFallback) => boolean;
|
|
26
|
-
isFetching: (ns?: import("@tolgee/web").NsFallback) => boolean;
|
|
27
|
-
isRunning: () => boolean;
|
|
28
|
-
run: () => Promise<void | undefined>;
|
|
29
|
-
stop: () => void;
|
|
30
|
-
t: import("@tolgee/web").TFnType<import("@tolgee/web").DefaultParamType, string>;
|
|
31
|
-
highlight: import("@tolgee/web").HighlightInterface;
|
|
32
|
-
getInitialOptions: () => {
|
|
33
|
-
apiUrl?: string | undefined;
|
|
34
|
-
apiKey?: string | undefined;
|
|
35
|
-
projectId?: string | number | undefined;
|
|
36
|
-
language?: string | undefined;
|
|
37
|
-
defaultLanguage?: string | undefined;
|
|
38
|
-
availableLanguages?: string[] | undefined;
|
|
39
|
-
fallbackLanguage?: import("@tolgee/web").FallbackLanguageOption;
|
|
40
|
-
ns?: string[] | undefined;
|
|
41
|
-
fallbackNs?: import("@tolgee/web").FallbackGeneral;
|
|
42
|
-
defaultNs: string;
|
|
43
|
-
staticData?: TolgeeStaticData | undefined;
|
|
44
|
-
observerType: "text" | "invisible";
|
|
45
|
-
observerOptions: import("@tolgee/web").ObserverOptionsInternal;
|
|
46
|
-
onFormatError: import("@tolgee/web").OnFormatError;
|
|
47
|
-
};
|
|
48
|
-
isDev: () => boolean;
|
|
49
|
-
wrap: (params: import("@tolgee/web").WrapperWrapProps) => string | undefined;
|
|
50
|
-
unwrap: (text: string) => import("@tolgee/web").Unwrapped;
|
|
51
|
-
overrideCredentials(credentials: import("@tolgee/web").DevCredentials): void;
|
|
52
|
-
addPlugin(plugin: import("@tolgee/web").TolgeePlugin | undefined): void;
|
|
53
|
-
updateOptions(options?: import("@tolgee/web").TolgeeOptions | undefined): void;
|
|
54
|
-
}>;
|
|
2
|
+
export declare function useTolgeeSSR(tolgeeInstance: TolgeeInstance, locale?: string, staticData?: TolgeeStaticData | undefined): TolgeeInstance;
|
package/lib/useTranslate.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { TFnType, DefaultParamType } from '@tolgee/web';
|
|
1
|
+
import { TFnType, DefaultParamType, TranslationKey } from '@tolgee/web';
|
|
2
2
|
import { ReactOptions } from './types';
|
|
3
|
-
|
|
4
|
-
t: TFnType<DefaultParamType, string>;
|
|
3
|
+
type UseTranslateResult = {
|
|
4
|
+
t: TFnType<DefaultParamType, string, TranslationKey>;
|
|
5
5
|
isLoading: boolean;
|
|
6
6
|
};
|
|
7
|
+
export declare const useTranslate: (ns?: string[] | string, options?: ReactOptions) => UseTranslateResult;
|
|
8
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tolgee/react",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.7.0-prerelease.134377fa.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.
|
|
41
|
+
"@tolgee/web": "5.7.0-prerelease.134377fa.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.
|
|
50
|
-
"@tolgee/testing": "5.
|
|
49
|
+
"@tolgee/format-icu": "5.7.0-prerelease.134377fa.0",
|
|
50
|
+
"@tolgee/testing": "5.7.0-prerelease.134377fa.0",
|
|
51
51
|
"@types/jest": "^27.0.2",
|
|
52
52
|
"@types/node": "^17.0.8",
|
|
53
53
|
"@types/react": "^18.0.28",
|
|
@@ -79,5 +79,5 @@
|
|
|
79
79
|
"access": "public"
|
|
80
80
|
},
|
|
81
81
|
"sideEffects": false,
|
|
82
|
-
"gitHead": "
|
|
82
|
+
"gitHead": "3a0ca2ce76eb4126b21779e133bb5484ef596dbe"
|
|
83
83
|
}
|
package/src/T.tsx
CHANGED
|
@@ -1,27 +1,41 @@
|
|
|
1
|
-
import { NsType, TranslateParams } from '@tolgee/web';
|
|
2
|
-
import React
|
|
1
|
+
import { NsType, TranslateParams, TranslationKey } from '@tolgee/web';
|
|
2
|
+
import React from 'react';
|
|
3
3
|
import { addReactKeys, wrapTagHandlers } from './tagsTools';
|
|
4
|
-
|
|
5
4
|
import { ParamsTags } from './types';
|
|
5
|
+
|
|
6
6
|
import { useTranslateInternal } from './useTranslateInternal';
|
|
7
7
|
|
|
8
|
-
type
|
|
8
|
+
type PropsWithKeyName = {
|
|
9
9
|
params?: TranslateParams<ParamsTags>;
|
|
10
10
|
children?: string;
|
|
11
11
|
noWrap?: boolean;
|
|
12
|
-
keyName
|
|
12
|
+
keyName: TranslationKey;
|
|
13
13
|
ns?: NsType;
|
|
14
14
|
defaultValue?: string;
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
type PropsWithoutKeyName = {
|
|
18
|
+
params?: TranslateParams<ParamsTags>;
|
|
19
|
+
children: TranslationKey;
|
|
20
|
+
noWrap?: boolean;
|
|
21
|
+
ns?: NsType;
|
|
22
|
+
defaultValue?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
interface TInterface {
|
|
26
|
+
(props: PropsWithKeyName): JSX.Element;
|
|
27
|
+
(props: PropsWithoutKeyName): JSX.Element;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const T: TInterface = (props) => {
|
|
31
|
+
const key = (props as PropsWithKeyName).keyName || props.children;
|
|
19
32
|
if (key === undefined) {
|
|
20
33
|
// eslint-disable-next-line no-console
|
|
21
34
|
console.error('T component: keyName not defined');
|
|
22
35
|
}
|
|
23
36
|
const defaultValue =
|
|
24
|
-
props.defaultValue ||
|
|
37
|
+
props.defaultValue ||
|
|
38
|
+
((props as PropsWithKeyName).keyName ? props.children : undefined);
|
|
25
39
|
|
|
26
40
|
const { t } = useTranslateInternal();
|
|
27
41
|
|
|
@@ -36,22 +36,6 @@ describe('T component integration', () => {
|
|
|
36
36
|
<div data-testid="with_tags">
|
|
37
37
|
<T keyName="with_tags" params={{ b: <b />, i: <i /> }} />
|
|
38
38
|
</div>
|
|
39
|
-
<div data-testid="with_empty_tag">
|
|
40
|
-
<T
|
|
41
|
-
keyName="with_empty_tag"
|
|
42
|
-
params={{ br: <br /> }}
|
|
43
|
-
defaultValue="Here is empty<br></br>tag"
|
|
44
|
-
/>
|
|
45
|
-
</div>
|
|
46
|
-
<div data-testid="with_children_conflict">
|
|
47
|
-
<T
|
|
48
|
-
keyName="with_children_conflict"
|
|
49
|
-
params={{
|
|
50
|
-
b: <b>should</b>,
|
|
51
|
-
}}
|
|
52
|
-
defaultValue="This <b>shouldn't</b> be here"
|
|
53
|
-
/>
|
|
54
|
-
</div>
|
|
55
39
|
<div data-testid="with_tag">
|
|
56
40
|
<T
|
|
57
41
|
keyName="with_tag"
|
|
@@ -130,22 +114,6 @@ describe('T component integration', () => {
|
|
|
130
114
|
expect(screen.queryByTestId('with_tags')).toHaveAttribute('_tolgee');
|
|
131
115
|
});
|
|
132
116
|
|
|
133
|
-
it('works with empty tag', () => {
|
|
134
|
-
expect(screen.queryByTestId('with_empty_tag')).toContainHTML(
|
|
135
|
-
'Here is empty<br />tag'
|
|
136
|
-
);
|
|
137
|
-
expect(screen.queryByTestId('with_empty_tag')).toHaveAttribute('_tolgee');
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it("won't pass children if the element already has children", () => {
|
|
141
|
-
expect(screen.queryByTestId('with_children_conflict')).toContainHTML(
|
|
142
|
-
'This <b>should</b> be here'
|
|
143
|
-
);
|
|
144
|
-
expect(screen.queryByTestId('with_children_conflict')).toHaveAttribute(
|
|
145
|
-
'_tolgee'
|
|
146
|
-
);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
117
|
it('works with tag as function', () => {
|
|
150
118
|
expect(screen.queryByTestId('with_tag')).toContainHTML(
|
|
151
119
|
'<b title="bold">bold</b>'
|