Allow React components as dynamic data arguments #100
Merged
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.
From #76.
Repeating the summary for posterity.
Before
templater()
would return a string with all the dynamic content filled in:getLocalizedElement
would check if the result oftemplater()
has any HTML tags in it:After
templater()
returns a string if the dynamic content does not contain any values satisfyingReact.isValidElement()
. Otherwise, it returns an array of string/React components:getLocalizedElement
checks if the result oftemplater
is a string or not. If it is, it does the same as before. If it is not (i.e. we are passing React components as dynamic content, and it is an array), first check that no string element in the array satisfieshasHtmlTags()
(see below). If that check passes, spread the array intoReact.createElement
:Known issues
HTML in translations: This approach does not support having HTML in the translation. The reason is that the templater will split the stringified HTML into
[ '<span>', <strong>Google</strong>, '</span>' ]
and I'm not sure how to re-form this into HTML. UsingdangerouslySetInnerHTML
will lose the<strong>Google</strong>
(because it is not a string), but is the current way of parsing the string HTML.React Native: Any translations with React components in data are returned as
React.createElement('span', ...)
. This won't work in RN. I'm less familiar with RN so wonder if replacingspan
withFragment
would work.