-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrapping interpolated values in tags #93
Comments
This is how Airbnb tends to do it: polyglot.extend({
hello_name: 'Hola, ${name_start}${name}${name_end}. How are you?',
});
polyglot.t('hello_name', { name: 'DeNiro', name_start: '<strong>', name_end: '</strong>' }); |
@dcworldwide it works, but then it produces HTML, which has to be unsafely added to the DOM. |
Yep. As @ljharb says. It works, but far from ideal. Mixing html with translations would not be a good situation. It's simple enough to drop
|
This doesn't seem to work. I'm getting the literal string tags displayed. (Is it because I'm using it in React?) |
Yes. In react, you’d have to use dangerouslySetInnerHTML to make that work. |
At Airbnb, we had a polyglot.extend({
hello_name: 'Hola, ${tag_start}${name}${tag_end}. How are you?',
}); <T
k="hello_name"
name="DeNiro"
tag={<strong />
/> That essentially sliced up the string into an array of |
Sadly i haven’t had time to recreate that yet as an open source component. |
Answering the original question: I advocate keeping markup out of the translation files since these might be generated or created by people or tools that do not know html well. Therefore I'd recommend using -- With the react discussion since #170 you could also provide a custom replace implementation (see example: https://codesandbox.io/s/admiring-framework-lb367l?file=/src/App.js) to return valid react fragments from polyglot. |
So the newly released With that you can create a replace that is able to handle cases like See https://codesandbox.io/s/admiring-framework-lb367l?file=/src/App.js:1209-1257 for details. |
What is the recommended method to wrap an interpolated value in tags. For example, if I have
and would like to bold the name (wrap it in
<strong>
tags). Is there a recommended way to do this?The text was updated successfully, but these errors were encountered: