-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Support raw HTML in translation values #189
Comments
right changing that line would work it out - but lead to dangerous insert of pure html - depending on interpolating of user inputs would lead to xss vulnerability. i won't open this door - but you can create a own component doing so - but i would suggest you don't use html in your translations - better use a markdown component and use markdown... |
Yeah, I share your concern about allowing use of raw HTML. However, user input can not be in translation values. So it's only a translator who can put harmful HTML code, but we are supposed to trust our translators and verify their work, aren't we? :) What do you think about adding a prop like |
why not just doing it like in the sample: https://github.com/i18next/react-i18next/blob/master/example/app/components/View.js#L19 interpolating a strong component? |
The BTW There's no way to use markdown out of the box, is there? |
|
no markdown built in...nope |
Yeah, that's clear. Another option as for now is But they both are weird :-) |
yes...why not using eg. react-remarkable and pass markdown from t function to that? |
Yes it's possible, but it's no better than the I sure can write my own components which do whatever I want (in fact I already did), but I guess it's not just me having this problem #80 , so it would be great if this package had a solution for it out of the box. And I'm ready to contribute, as soon as we come up with a consensus. Maybe this should be a new component, extended from the Interpolate? |
it will process user input - but you can configure remarkable to not allow html tags or just defined tags - so no risk there. So i think the solution with markdown or you might do a PR adding a prop |
closing for now...if you still want to provide a PR go for it... |
Yes I will, just too busy as for now. |
Hi!
Thank you! |
@01Kuzma it's the same question...like said...using a component setting inner html #189 (comment) or using markdown or using https://react.i18next.com/components/trans-component.html all options to solve this... |
trans component will NOT trigger render when language changed 😢 |
@jkiss not the concern of that Component...it's used too often in one Component -> to much bindings to much noisy updates...languageChange is a "pagelevel" concern |
First of all, what HTML formatting has to be used, except fot technical structure, should be up the translator, not up to the developer, so all markup belongs to the translation files, not to the code. E.g. how many paragraphs there should be, depends on the content, e.g. the marketing wants to publish. I don't think it is a good idea to have HTML-tags within a string, but it should be possible to add norml JSX in ttanslations. I suggest a syntax like this:
|
@mwaeckerlin that's completely a different usecase from what the Trans component does -> Trans component is for embedding rich react elements into a translation - not for rendering prosa for the prosa case (long text with formatting):
|
Normally in your code, you have both to be translated: simple tags and texts, such as form labels etc., but often prosa, i.e. help texts and explanations. And in my experience, it is very bad, if the programmer defines the structure, e.g. how many paragraphs there should be, not the translator. Because then, sales come in, support and marketing, and ask for completely different structures. Then, if that need code change, the result is often a hack and a mess. Previously. I've been using react-markup for specific messages. As you suggest, react-render-html is also an option, but both for me still looks like a dirty hack. The programmer, not the translator needs to decide which texts will be parsed. Also, having markup or HTML in one huge string is not readable and so really maintainable. IMHO having the ability to add JSX in the tanslation files, as alternative to simple strings, would be best solution. Then the translator (and communications department) has full control over the content, while the programmer just cares about the logic. |
There is already one option: https://react.i18next.com/latest/trans-component#using-for-less-than-br-greater-than-and-other-simple-html-elements-in-translations-v-10-4-0 but it has it's limitations...but might ok for your case |
Just not the way it works...again JSX is not a string...you do not have JSX on the client during runtime...you have functions |
@jamuhl, thank you for the link. I have seen this, but it does not fit my case, because the structure is given in the code, not in the translation file. To better understand my point, in the past, I've seen code like this (not really in JavaScript/React, but in PHP/Joomla, but basically it's the same problem):
Then the product owner asked for having only the main title and one paragraph. With this construct, that requires a code change. Changing texts was easy on our system, just edit the new text in an online form. But changing the code means full build, full test scenarios, approvement by the change board and a maintenance window. That's why I say, text and it's structure belong together and should be separated from code. So my current solution is this:
And then, where I use it, I just add:
So, as a programmer, I just define the area, where some text can be inserted, but the translators are then completely free on what and how they want to place there. But I wish, there were a better, native solution. I I would write a translation library, something like embedded JSX would be one of the key-features. How do others solve this flexibility for the ttranslators / texters issue? Is the above workaround often used? |
@mwaeckerlin fully agree...I like the idea...providing a PR I will immediately merge it and publish an update |
there’s also the option to postprocess, like: https://www.npmjs.com/package/i18next-markdown-jsx-plugin |
@jamuhl, than you for your motivation, @adrai, than you for your Idea. I started to prepare a patch, then analyzing the code brought me to this already existing solution: How to Use JSX in the Translation FileThere is a option So the solution is:
Then just add in your JSX Code:
No more need for Please add this to the documentation, if it is not already there (and I missed it). |
@mwaeckerlin this solution has one big drawback...it only works as long your translations are located in the source code and transpiled. As the content of |
When you want to enter JSX directly, then I see no other solution. JSX is a JavaScript object that does not exist in this format in plain JSON. What you could do is something like:
Then in Also my solution above is not that bad: Even though the file ending is
Or you can even split this in two files, one What do you think is the best way to go? What is your suggestion, @jamuhl? What could be a good practice? |
@mwaeckerlin like said...personally I would use:
But if it works for your case and team...it's ok 👍 |
Translator's ChoiceEverything at Once: JSX, HTML-Text, MarkupTo give your translator the transparent choice between: simple text, JSX, html string, markup string: file:
Then in
What do you think about this solution, @jamuhl? |
@mwaeckerlin like said...if it works for you - go for it... |
@jamuhl, isn't that what you suggested, or do I misunderstand your point? |
yes...I personally just don't use the handler for that but use |
It's what I used to use before, but less flexible for the translator. I'll now give my suggestion a try and see, how it works together with other tools. I'll keep you updated. |
Ok, @jamuhl, you were completely right: Scanners just ignore (and therefore overwrite) objects instead of strings. So I have a new solution, that works together with Moreover I decided to use yaml for my translation files, since yaml allows multi line strings. So my translation file looks e.g. like this:
Unfortunately, after running
But at least entering multiline mode handy when I edit it. Then I add a post processor before init, instead of the
|
Say we have the next translation resource:
Then
<div><Interpolate i18nKey='foo' /></div>
will result in the next output:<div><b>Bar!</b></div>
, while I expect to get this:<div><b>Bar!</b></div>
, so a raw HTML in translation strings should not be escaped.Is this an intended behavior, or is a bug?
I guess replacing this line with something like
child = React.createElement(parent, {dangerouslySetInnerHTML: {__html: match}});
will work this out.The text was updated successfully, but these errors were encountered: