import React, { ReactNode } from 'react'; import { TaggedChildren, TaggedElement, TranslatedChildren, TranslatedElement, VariableProps, } from '../types'; import { getVariableProps, isVariableElementProps, } from '../variables/_getVariableProps'; import { createRenderDefaultChildren } from './renderDefaultChildren.shared'; import { isVariable, libraryDefaultLocale } from 'generaltranslation/internal'; import { getPluralBranch } from '../plurals/getPluralBranch'; import { HTML_CONTENT_PROPS, HtmlContentPropValuesRecord, } from '@generaltranslation/format/types'; import { getGTTag } from './getGTTag'; import type { RenderVariable } from '../types'; // Shared implementation: the variable renderer is dependency-injected by a // factory so neither code path statically imports the other's variable // components. Callsites use the pre-instantiated renderPipeline / // renderPipeline.rsc modules instead of importing this directly. type RenderTranslatedChildrenArgs = { source: TaggedChildren; target: TranslatedChildren; locales: string[]; enableI18n: boolean; }; function createRenderTranslatedChildren({ renderVariable, }: { renderVariable: RenderVariable; }): (args: RenderTranslatedChildrenArgs) => ReactNode { const renderDefaultChildren = createRenderDefaultChildren({ renderVariable }); function renderTranslatedElement({ sourceElement, targetElement, locales = [libraryDefaultLocale], enableI18n, }: { sourceElement: TaggedElement; targetElement: TranslatedElement; locales: string[]; enableI18n: boolean; }): React.ReactNode { // Get props and generaltranslation const { props: sourceProps } = sourceElement; const sourceGT = sourceProps['data-_gt']; const transformation = sourceGT?.transformation; // Get translated props const unprocessedTargetGT = targetElement.d; const translatedProps: HtmlContentPropValuesRecord = {}; if (unprocessedTargetGT) { Object.entries(HTML_CONTENT_PROPS).forEach(([minifiedName, fullName]) => { if ( unprocessedTargetGT[minifiedName as keyof typeof HTML_CONTENT_PROPS] ) { translatedProps[fullName] = unprocessedTargetGT[ minifiedName as keyof typeof HTML_CONTENT_PROPS ] as string; } }); } // plural (choose a branch) if (transformation === 'plural') { const n = sourceElement.props.n; if (typeof n !== 'number') { return renderDefaultChildren({ children: sourceElement, defaultLocale: locales[0], enableI18n, }); } const sourceBranches = sourceGT.branches || {}; const resolvedSourceBranch = getPluralBranch(n, locales, sourceBranches); const sourceBranch = resolvedSourceBranch != null ? resolvedSourceBranch : sourceElement.props.children; const targetBranches = targetElement.d?.b || {}; const resolvedTargetBranch = getPluralBranch(n, locales, targetBranches); const targetBranch = resolvedTargetBranch != null ? resolvedTargetBranch : targetElement.c; return renderTranslatedChildren({ source: sourceBranch as TaggedChildren, target: targetBranch as TranslatedChildren, locales, enableI18n, }); } // branch (choose a branch) if (transformation === 'branch') { const { branch, children } = sourceProps; const branchKey = branch == null || branch === '' ? undefined : branch.toString(); const sourceBranches = sourceGT.branches || {}; const targetBranches = targetElement.d?.b || {}; const sourceBranch = branchKey && sourceBranches[branchKey] !== undefined ? sourceBranches[branchKey] : children; const targetBranch = branchKey && targetBranches[branchKey] !== undefined ? targetBranches[branchKey] : targetElement.c; return renderTranslatedChildren({ source: sourceBranch as TaggedChildren, target: targetBranch as TranslatedChildren, locales, enableI18n, }); } // fragment (create a valid fragment) if (transformation === 'fragment' && targetElement.c) { return React.createElement(React.Fragment, { key: sourceElement.props.key, children: renderTranslatedChildren({ source: sourceProps.children as TaggedChildren, target: targetElement.c, locales, enableI18n, }) as TaggedChildren, }); } // other if (sourceProps?.children && targetElement?.c) { return React.cloneElement(sourceElement, { ...sourceProps, ...translatedProps, 'data-_gt': undefined, children: renderTranslatedChildren({ source: sourceProps.children as TaggedChildren, target: targetElement.c, locales, enableI18n, }) as TaggedChildren, }); } // fallback return renderDefaultChildren({ children: sourceElement, defaultLocale: locales[0], enableI18n, }); } function renderTranslatedChildren({ source, target, locales = [libraryDefaultLocale], enableI18n, }: RenderTranslatedChildrenArgs): ReactNode { // Most straightforward case, return a valid React node if ((target === null || typeof target === 'undefined') && source) return renderDefaultChildren({ children: source, defaultLocale: locales[0], enableI18n, }); if (typeof target === 'string') return target; // Convert source to an array in case target has multiple children where source only has one if (Array.isArray(target) && !Array.isArray(source) && source) source = [source]; // Multiple children if (Array.isArray(source) && Array.isArray(target)) { // Track the variables const variables: Record = {}; const variablesOptions: Record = {}; const variableInjectionTypes: Record< string, VariableProps['injectionType'] > = {}; // Extract source elements // Extract variable props // Filter out variable elements const sourceElements: TaggedElement[] = source.filter( (sourceChild): sourceChild is TaggedElement => { if (React.isValidElement(sourceChild)) { if (isVariableElementProps(sourceChild.props)) { const { variableName, variableValue, variableOptions, injectionType, } = getVariableProps(sourceChild.props); variables[variableName] = variableValue; variablesOptions[variableName] = variableOptions; variableInjectionTypes[variableName] = injectionType; } else { return true; } } return false; } ); // TODO: pre-index these to avoid O(n/2) lookups const findMatchingSourceElement = ( targetElement: TranslatedElement ): TaggedElement | undefined => { return ( sourceElements.find((sourceChild): sourceChild is TaggedElement => { const generaltranslation = getGTTag(sourceChild); if (typeof generaltranslation?.id !== 'undefined') { const sourceId = generaltranslation.id; const targetId = targetElement.i; return sourceId === targetId; } return false; }) || sourceElements.shift() ); // assumes fixed order, not recommended }; // map target to source return target.map((targetChild, index) => { if (typeof targetChild === 'string') return ( {targetChild} ); // Render variable if (isVariable(targetChild)) { return ( {renderVariable({ variableType: targetChild.v || 'v', variableValue: variables[targetChild.k], variableOptions: variablesOptions[targetChild.k], locales, enableI18n, injectionType: variableInjectionTypes[targetChild.k] || 'manual', })} ); } // Render element (targetChild is a TranslatedElement) const matchingSourceElement = findMatchingSourceElement( targetChild as TranslatedElement ); if (!matchingSourceElement) return null; return ( {renderTranslatedElement({ sourceElement: matchingSourceElement, targetElement: targetChild, locales, enableI18n, })} ); }); } // Single child if (target && typeof target === 'object' && !Array.isArray(target)) { const targetType: 'variable' | 'element' = isVariable(target) ? 'variable' : 'element'; if (React.isValidElement(source)) { if (targetType === 'element') { return renderTranslatedElement({ sourceElement: source, targetElement: target as TranslatedElement, locales, enableI18n, }); } // Render variable if (isVariableElementProps(source.props)) { const { variableValue, variableOptions, variableType, injectionType, } = getVariableProps(source.props); return renderVariable({ variableType, variableValue, variableOptions, locales, enableI18n, injectionType, }); } } } // fallback return renderDefaultChildren({ children: source, defaultLocale: locales[0], enableI18n, }); } return renderTranslatedChildren; } export { createRenderTranslatedChildren }; export type { RenderTranslatedChildrenArgs };