Add ability to use elements in Translate interpolation options#11204
Merged
ThieryMichel merged 2 commits intonextfrom Mar 27, 2026
Merged
Add ability to use elements in Translate interpolation options#11204ThieryMichel merged 2 commits intonextfrom
ThieryMichel merged 2 commits intonextfrom
Conversation
ThieryMichel
requested changes
Mar 26, 2026
Comment on lines
+10
to
+44
| const elementMap: Record<string, ReactElement> = {}; | ||
| const sanitizedOptions: Record<string, any> = {}; | ||
| let placeholderIndex = 0; | ||
|
|
||
| if (options) { | ||
| for (const [key, value] of Object.entries(options)) { | ||
| if (isValidElement(value)) { | ||
| const placeholder = `TRANSLATION_PLACEHOLDER_${placeholderIndex++}`; | ||
| elementMap[placeholder] = value; | ||
| sanitizedOptions[key] = | ||
| `${SPLIT_MARKER}${placeholder}${SPLIT_MARKER}`; | ||
| } else { | ||
| sanitizedOptions[key] = value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const translateOptions = | ||
| typeof children === 'string' | ||
| ? { _: children, ...sanitizedOptions } | ||
| : sanitizedOptions; | ||
|
|
||
| const translatedMessage = translate(i18nKey, translateOptions); | ||
|
|
||
| if (!translatedMessage) { | ||
| return children; | ||
| } | ||
|
|
||
| // If no elements were extracted, return plain string | ||
| if (placeholderIndex === 0) { | ||
| return <>{translatedMessage}</>; | ||
| } | ||
| return children; | ||
|
|
||
| // Split the translated string and replace placeholders with React elements | ||
| const parts = translatedMessage.split(SPLIT_MARKER); |
Contributor
There was a problem hiding this comment.
How about adding useMemo around all those computations.
Member
Author
There was a problem hiding this comment.
When calling <Translate key="foo" options={{ name: <NameField /> }} />, the options prop receives a new object on every render, so useMemo will always recompute. The memo is effectively useless for the most common usage pattern.
Besides, we don't know if this computation is costly. Premature optimization?
WiXSL
approved these changes
Mar 26, 2026
ThieryMichel
approved these changes
Mar 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
<Translate>supports string interpolation:But it does not allow to use a React element in interpolatino:
Solution
Replace element options with placeholders, then pass the options to the translation provider, then replace the placeholders back with elements.
How To Test
http://localhost:9010/?path=/story/ra-i18n-polyglot--translate-with-react-element&args=locale:fr
Additional Checks
masterfor a bugfix or a documentation fix, ornextfor a feature