Hi all, we have a problem doing i18n with React. This is about template strings.
What we need:
<p>
By clicking SignUp, you accept our <a href="/terms">Terms of Service</a>
</p>
How I would do it (not in React):
i18n.t('By clicking SignUp, you accept our {{terms}}', {
terms: `<a href="/terms">${i18n.t('Terms of Service')}</a>`
})
Since React does not operate with strings/templates - this is not really possible to achieve.
Possible hacky, but not acceptable solution is
<p>
{i18n.t('By clicking SignUp, you accept our')}
<a href="/terms">{i18n.t('Terms of Service')}</a>
</p>
It is not acceptable in a long term since in other languages phrases are often arranged differently, so when you do translation you need to move <a>
around the sentence to not sound silly.
An investigation into tagged template literals.
Consider a pagination component where
More
andLess
anchors are separately translated.Where the substitutions are non-strings we want the interpolation to return an
Array
. So long as the substitutions all have akey
then React is happy.Referring to this implementation:
The required translation is not very descriptive.
We would be looking for those fields to be named. However I cannot see how that can be achieved conventionally with tagged template literals.
However we are lucky that we expect our substitutions to be React components. Each substitution must already have a
key
so ideally we would introspect this value and use it as the placeholder.Meaning we translate:
Magically that actually works. Here is the full solution: