|
| 1 | +import React from "react"; |
| 2 | +import { Text, TextStyle } from "react-native"; |
| 3 | +import { useAutoTranslate } from "../context/TranslationContext"; |
| 4 | + |
| 5 | +/** |
| 6 | + * FormattedText is a component that handles nested text formatting during translation. |
| 7 | + * It preserves styling and structure of nested Text components while allowing the content |
| 8 | + * to be translated. |
| 9 | + * |
| 10 | + * @example |
| 11 | + * ```tsx |
| 12 | + * <FormattedText> |
| 13 | + * Hello, <Text style={{ color: 'red' }}>world</Text>! |
| 14 | + * </FormattedText> |
| 15 | + * ``` |
| 16 | + */ |
| 17 | +interface FormattedTextProps { |
| 18 | + children: React.ReactNode; |
| 19 | + style?: TextStyle; |
| 20 | + /** |
| 21 | + * Whether to persist the text for review in the dashboard. |
| 22 | + * @default true |
| 23 | + */ |
| 24 | + persist?: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +export const FormattedText: React.FC<FormattedTextProps> = ({ |
| 28 | + children, |
| 29 | + style, |
| 30 | + persist = true, |
| 31 | +}) => { |
| 32 | + const { t } = useAutoTranslate(); |
| 33 | + |
| 34 | + /** |
| 35 | + * Extracts text content and styled nodes from the children prop. |
| 36 | + * Converts nested Text components into a template format (e.g., <0>styled text</0>) |
| 37 | + * while preserving the original styled nodes for later restoration. |
| 38 | + * |
| 39 | + * @param nodes - The React nodes to process (typically the children prop) |
| 40 | + * @returns An object containing the template text and an array of styled nodes |
| 41 | + */ |
| 42 | + const extractTextAndStyles = ( |
| 43 | + nodes: React.ReactNode |
| 44 | + ): { |
| 45 | + text: string; |
| 46 | + styles: Array<{ node: React.ReactElement; text: string }>; |
| 47 | + } => { |
| 48 | + const styles: Array<{ node: React.ReactElement; text: string }> = []; |
| 49 | + let text = ""; |
| 50 | + |
| 51 | + const processNode = (node: React.ReactNode) => { |
| 52 | + if (typeof node === "string") { |
| 53 | + text += node; |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (React.isValidElement(node)) { |
| 58 | + const children = node.props.children; |
| 59 | + if (typeof children === "string") { |
| 60 | + text += `<${styles.length}>${children}</${styles.length}>`; |
| 61 | + styles.push({ node, text: children }); |
| 62 | + } else if (Array.isArray(children)) { |
| 63 | + children.forEach(processNode); |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + processNode(nodes); |
| 69 | + return { text, styles }; |
| 70 | + }; |
| 71 | + |
| 72 | + /** |
| 73 | + * Restores the styled nodes in the translated text by replacing template markers |
| 74 | + * with the original styled components, but with translated content. |
| 75 | + * |
| 76 | + * @param translatedText - The translated text containing template markers |
| 77 | + * @param styles - Array of original styled nodes and their text content |
| 78 | + * @returns An array of React nodes with restored styling and translated content |
| 79 | + */ |
| 80 | + const restoreStyledText = ( |
| 81 | + translatedText: string, |
| 82 | + styles: Array<{ node: React.ReactElement; text: string }> |
| 83 | + ): React.ReactNode[] => { |
| 84 | + const parts = translatedText.split(/(<\d+>.*?<\/\d+>)/g); |
| 85 | + return parts.map((part, index) => { |
| 86 | + const match = part.match(/<(\d+)>(.*?)<\/\1>/); |
| 87 | + if (match) { |
| 88 | + const [, styleIndex, content] = match; |
| 89 | + const { node } = styles[parseInt(styleIndex)]; |
| 90 | + return React.cloneElement(node, { key: `styled-${index}` }, content); |
| 91 | + } |
| 92 | + return part; |
| 93 | + }); |
| 94 | + }; |
| 95 | + |
| 96 | + const { text, styles } = extractTextAndStyles(children); |
| 97 | + const translatedText = t(text, persist); |
| 98 | + |
| 99 | + return <Text style={style}>{restoreStyledText(translatedText, styles)}</Text>; |
| 100 | +}; |
0 commit comments