|
2405 | 2405 | transWrapTextNodes: '', |
2406 | 2406 | transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'], |
2407 | 2407 | useSuspense: true, |
2408 | | - unescape |
| 2408 | + unescape, |
| 2409 | + transDefaultProps: undefined |
2409 | 2410 | }; |
2410 | 2411 | const setDefaults = (options = {}) => { |
2411 | 2412 | defaultOptions = { |
|
2764 | 2765 | }; |
2765 | 2766 | let namespaces = ns || t.ns || i18n.options?.defaultNS; |
2766 | 2767 | namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation']; |
| 2768 | + const { |
| 2769 | + transDefaultProps |
| 2770 | + } = reactI18nextOptions; |
| 2771 | + const mergedTOptions = transDefaultProps?.tOptions ? { |
| 2772 | + ...transDefaultProps.tOptions, |
| 2773 | + ...tOptions |
| 2774 | + } : tOptions; |
| 2775 | + const mergedShouldUnescape = shouldUnescape ?? transDefaultProps?.shouldUnescape; |
| 2776 | + const mergedValues = transDefaultProps?.values ? { |
| 2777 | + ...transDefaultProps.values, |
| 2778 | + ...values |
| 2779 | + } : values; |
| 2780 | + const mergedComponents = transDefaultProps?.components ? { |
| 2781 | + ...transDefaultProps.components, |
| 2782 | + ...components |
| 2783 | + } : components; |
2767 | 2784 | const nodeAsString = nodesToString(children, reactI18nextOptions, i18n, i18nKey); |
2768 | | - const defaultValue = defaults || tOptions?.defaultValue || nodeAsString || reactI18nextOptions.transEmptyNodeValue || (typeof i18nKey === 'function' ? keysFromSelector(i18nKey) : i18nKey); |
| 2785 | + const defaultValue = defaults || mergedTOptions?.defaultValue || nodeAsString || reactI18nextOptions.transEmptyNodeValue || (typeof i18nKey === 'function' ? keysFromSelector(i18nKey) : i18nKey); |
2769 | 2786 | const { |
2770 | 2787 | hashTransKey |
2771 | 2788 | } = reactI18nextOptions; |
2772 | 2789 | const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue); |
2773 | 2790 | if (i18n.options?.interpolation?.defaultVariables) { |
2774 | | - values = values && Object.keys(values).length > 0 ? { |
2775 | | - ...values, |
| 2791 | + values = mergedValues && Object.keys(mergedValues).length > 0 ? { |
| 2792 | + ...mergedValues, |
2776 | 2793 | ...i18n.options.interpolation.defaultVariables |
2777 | 2794 | } : { |
2778 | 2795 | ...i18n.options.interpolation.defaultVariables |
2779 | 2796 | }; |
| 2797 | + } else { |
| 2798 | + values = mergedValues; |
2780 | 2799 | } |
2781 | 2800 | const valuesFromChildren = getValuesFromChildren(children); |
2782 | 2801 | if (valuesFromChildren && typeof valuesFromChildren.count === 'number' && count === undefined) { |
2783 | 2802 | count = valuesFromChildren.count; |
2784 | 2803 | } |
2785 | | - const interpolationOverride = values || count !== undefined && !i18n.options?.interpolation?.alwaysFormat || !children ? tOptions.interpolation : { |
| 2804 | + const interpolationOverride = values || count !== undefined && !i18n.options?.interpolation?.alwaysFormat || !children ? mergedTOptions.interpolation : { |
2786 | 2805 | interpolation: { |
2787 | | - ...tOptions.interpolation, |
| 2806 | + ...mergedTOptions.interpolation, |
2788 | 2807 | prefix: '#$?', |
2789 | 2808 | suffix: '?$#' |
2790 | 2809 | } |
2791 | 2810 | }; |
2792 | 2811 | const combinedTOpts = { |
2793 | | - ...tOptions, |
2794 | | - context: context || tOptions.context, |
| 2812 | + ...mergedTOptions, |
| 2813 | + context: context || mergedTOptions.context, |
2795 | 2814 | count, |
2796 | 2815 | ...values, |
2797 | 2816 | ...interpolationOverride, |
|
2800 | 2819 | }; |
2801 | 2820 | let translation = key ? t(key, combinedTOpts) : defaultValue; |
2802 | 2821 | if (translation === key && defaultValue) translation = defaultValue; |
2803 | | - const generatedComponents = generateComponents(components, translation, i18n, i18nKey); |
| 2822 | + const generatedComponents = generateComponents(mergedComponents, translation, i18n, i18nKey); |
2804 | 2823 | let indexedChildren = generatedComponents || children; |
2805 | 2824 | let componentsMap = null; |
2806 | 2825 | if (isComponentsMap(generatedComponents)) { |
2807 | 2826 | componentsMap = generatedComponents; |
2808 | 2827 | indexedChildren = children; |
2809 | 2828 | } |
2810 | | - const content = renderNodes(indexedChildren, componentsMap, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape); |
| 2829 | + const content = renderNodes(indexedChildren, componentsMap, translation, i18n, reactI18nextOptions, combinedTOpts, mergedShouldUnescape); |
2811 | 2830 | const useAsParent = parent ?? reactI18nextOptions.defaultTransParent; |
2812 | 2831 | return useAsParent ? React.createElement(useAsParent, additionalProps, content) : content; |
2813 | 2832 | } |
|
0 commit comments