Support passing objects to <Trans components={{ ... }} />
See original GitHub issueHi there! First of all, thank you for all the work you put into i18next
! It is by far the best i18n lib we’ve come across, and it just keeps getting better and better 😃
Is your feature request related to a problem? Please describe.
I’m running into some issues with the numeric (<0>
) placeholders in the <Trans components={[...]} />
prop.
First of all, the translation tool we use has first-class support for html placeholders, meaning that during the translation process they automatically get detected and copied to the target translation. Without a mechanism like this you very quickly run into awkward scenarios with for example RTL input. However, this detection is built on top of an X(HT)ML parser, and the XML spec dictates that tags are only valid if they start with a letter. As a result the numeric <0>
placeholders are not picked up and the detection does not work in this case.
Secondly, I’m noticing that numeric placeholders sometimes convey too little context to translators. Given the following 2 strings:
<0>{{firstName}} {{lastName}}</0> added a new <1>comment</1> to article <2>{{articleTitle}}</2>
<profileLink>{{firstName}} {{lastName}}</profileLink> added a new <commentLink>comment</commentLink> to article <articleLink>{{articleTitle}}</articleLink>
I personally prefer the latter one. It’s a bit more verbose, but verbosity is important when involving external translators into a project as they need additional context to figure out what is what. In addition, it looks more familiar to html which they generally already have some notion of, + it’s a bit more resilient to change as it doesn’t rely on order.
Describe the solution you’d like
I was wondering if you’d be open to support a secondary object syntax for the components
prop, i.e.:
<Trans
i18nKey="myKey"
components={{
articleLink: <a href="..." />,
commentlink: <a href="..." />,
profileLink: <a href="..." />
}}
values={values}
/>
Which would enable the use of the “named placeholder” method above. I can have a stab at this myself, just will have to dig into the Trans
component a bit first 😃
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:30 (15 by maintainers)
@klis87 duplication can be avoided.
Also passing object to
components
will be easier to maintainThe very big advantage of this is also you need to pass
children
forTrans
currently, which I find really problematic. For instance thing like:Several problems with this:
dragOrPickFromDisk
should be all it needs plus object of components to be injectedCompare it with this:
Advantage is cleaner jsx, less duplication, plus it would be easy to use interpolations in translations, as u would not use indexes of components anymore, but keys