Skip to content
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

feat(trans): allow interpolation in tOptions if provided #1204

Merged

Conversation

tigerabrodi
Copy link
Contributor

@tigerabrodi tigerabrodi commented Nov 30, 2020

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • documentation is changed or added

@coveralls
Copy link

coveralls commented Nov 30, 2020

Coverage Status

Coverage remained the same at 95.692% when pulling d8043dc on tigerabrodi:allow-trans-tOptions-interpolation into 0d0f4d8 on i18next:master.

@tigerabrodi
Copy link
Contributor Author

tigerabrodi commented Nov 30, 2020

@jamuhl As written on Twitter this morning, I would work on this after work 😃, I am really excited 🥳 This would be my very first OSS contribution 🎉

@tigerabrodi
Copy link
Contributor Author

@jamuhl I have no clue why it is messing with my PR here https://codeclimate.com/github/i18next/react-i18next/pull/1204 🤔 , if anyone else has any clues, would love some assistance 🙌

@tigerabrodi
Copy link
Contributor Author

tigerabrodi commented Nov 30, 2020

Do I need to update the documentation in https://github.com/i18next/react-i18next-gitbook, if so, what exactly should I update related to tOptions in https://react.i18next.com/latest/trans-component#trans-props?

src/Trans.js Outdated Show resolved Hide resolved
@jamuhl jamuhl requested a review from adrai December 1, 2020 06:59
src/Trans.js Outdated Show resolved Hide resolved
src/Trans.js Outdated Show resolved Hide resolved
src/Trans.js Outdated
@@ -298,6 +298,7 @@ export function Trans({
...interpolationOverride,
defaultValue,
ns: namespaces,
...tOptions?.interpolation,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to change line 293 to add the passed in tOptions.interpolation like:

const interpolationOverride = values ? tOptions?.interpolation : { interpolation: { ...tOptions?.interpolation, prefix: '#$?', suffix: '?$#' } };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @jamuhl and @adrai, will make sure to do this change and also add a test, where tOptions.interpolation is passed to Trans 🙌.

Currently working, will do it after work, many thanks again, it means the world to me, my very first OSS contribution 😃 🥰.

@tigerabrodi
Copy link
Contributor Author

@jamuhl @adrai I did the changes as requested 💯, one issue though, it still seems not to be escaping HTML tags properly. Here is a screenshot when using console.log(wrapper.debug()):
Screenshot from 2020-12-01 17-25-09

I wonder if there is a way to console.log and see the value of tOptions passed into the Trans component 🤔?

Also what do you think of the changes so far? What should I do next?

Many thanks 🙏 💕

@adrai
Copy link
Member

adrai commented Dec 1, 2020

@tigerabrodi maybe you missed my first comment about optional chaining (?.): #1204 (comment)

regarding the test: I suspect react is already escaping your '<no>Hello' and not the interpolation values of i18next...

@tigerabrodi
Copy link
Contributor Author

@adrai I actually implemented the change @jamuhl suggested #1204 (comment), should I try again, but without optional chaining?

What would the next steps be, since this seems not to be working?
Is there a way to force interpolation to be prioritized over React's escaping HTML tags?

@tigerabrodi
Copy link
Contributor Author

@adrai I pushed a new commit without Optional chaining 👏

@adrai
Copy link
Member

adrai commented Dec 1, 2020

removal of optional chaining is ok, thx

but the unescape issue is a react problem 🤷‍♂️

I'm not a react expert, maybe someone knows more?

@JacobMGEvans
Copy link
Contributor

JacobMGEvans commented Dec 1, 2020

It could possibly be a test environment issue.
Possibilities:

  • JSDOM isn't handling it correctly
    • Could try updating Jest other related packages
  • Whatever the test utility library being used isn't rendering it properly
    • Could try updating the utility and its configs (i.e. Enzyme, adapters, etc...)
    • Could try adding another testing utility library (React Testing Library)
  • Misconfigured or missing configs for React in Jest
    • This would involve some fine combing

@kentcdodds Any other thoughts on this? #1204 (comment)

Copy link

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest switching to React Testing Library eventually, but maybe this is the issue?

const stringWithHTMLTags = '<no>Hello';
const TestElement = () => (
<Trans tOptions={{ interpolation: { escapeValue: true } }}>
<div> {stringWithHTMLTags} </div>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<div> {stringWithHTMLTags} </div>
<div>{stringWithHTMLTags}</div>

Maybe the extra spaces are the problem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't even notice the spaces! 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would try this first, thanks both of you 💕

@adrai
Copy link
Member

adrai commented Dec 1, 2020

  • Whatever the test utility library being used isn't rendering it properly
    I'd suggest switching to React Testing Library eventually, but maybe this is the issue?

probably it's an enzyme issue...

@tigerabrodi if it works for you in react, I think if this can't be fixed with enzyme, we can skip the test or if you like you can try to switch to a better react test library?

@tigerabrodi
Copy link
Contributor Author

tigerabrodi commented Dec 1, 2020

  • Whatever the test utility library being used isn't rendering it properly
    I'd suggest switching to React Testing Library eventually, but maybe this is the issue?

probably it's an enzyme issue...

@tigerabrodi if it works for you in react, I think if this can't be fixed with enzyme, we can skip the test or if you like you can try to switch to a better react test library?

@adrai You mean for that particular test use React Testing Library? Or convert the whole project to use React Testing Library, which I would actually be down to 😃 ?

My suggestion: Try out what @kentcdodds mentioned, if it still does not work, skip the test and merge the changes, and I would with joy and passion convert this project to use React Testing Library instead of Enzyme, which could take some time 👍.

Side question: @adrai I was wondering if this project plans on migrating fully to TypeScript? If so, I'd also be down to help in every way I can 🎉. We use it at work by the way, and you and @jamuhl are just really awesome, being able to contribute here just means the world to me 😃 💕.

@adrai
Copy link
Member

adrai commented Dec 1, 2020

@tigerabrodiI would say, if you really like to change all tests to "React Testing Library" try it 😉

about TypeScript, idk, maybe not, need to wait for @jamuhl answer tomorrow morning.

@tigerabrodi
Copy link
Contributor Author

tigerabrodi commented Dec 1, 2020

@tigerabrodiI would say, if you really like to change all tests to "React Testing Library" try it

about TypeScript, idk, maybe not, need to wait for @jamuhl answer tomorrow morning.

@adrai Is it okay to merge the changes I made in Trans component first and make a new PR with the conversion over to React Testing Library?

I would make a fixup commit and squash the commits, so we only have the changes in Trans component and nothing else 👍.

In a separate PR, I would start the conversion to React Testing Library 🎉. I plan on doing it with @JacobMGEvans, we really look forward to it 💕 🤩

PS. What @kentcdodds pointed out was not the issue 👍.

@marcosvega91
Copy link
Contributor

Good job @tigerabrodi :)

@tigerabrodi tigerabrodi force-pushed the allow-trans-tOptions-interpolation branch from 4d59b6a to d8043dc Compare December 1, 2020 19:19
@tigerabrodi
Copy link
Contributor Author

tigerabrodi commented Dec 1, 2020

@adrai I pushed the last commit 🙌

Do I still need to change anything or?

I am excited about @jamuhl 's feedback.

Once this gets merged, huge celebration haha 🍾, funny though, my sister has birthday on Friday, what a coincidence, two celebrations in a row 😂 😃.

In another PR, I would start the conversion to React Testing Library 🎉 💕

@adrai
Copy link
Member

adrai commented Dec 1, 2020

lgtm

@jamuhl
Copy link
Member

jamuhl commented Dec 2, 2020

Looks awesome 👍... a great first contribution...keep it up...we're all happy to get help.

Regarding the encoding. This comes from react which does a good job of guarding people against accidentally pass unencoded user input into the page (XSS attacks):

image

So it comes to two options a dev has to insert translations into a page:

a) using dangerouslysetinnerhtml https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
b) using the Trans component (https://react.i18next.com/latest/trans-component) with it's <Trans><div><no />hello</div></Trans> to string "<0><0 />hello</0>" conversion and interpolation of react children's or named replacement like in <Trans defaults="<no/> hello" components={{ no: <no /> }} />

A little hard to simply describe how the Trans component works / can be used -> which might be a cause of some pain: i18next/react-i18next-gitbook#15

So while this PR fixes an issue - I guess, it will not solve the initial problem you liked to solve. You might open an issue with the content you like to get translated and we might help out with some options on how to use the Trans component for it.

Regarding the switch to React Testing Library that would be the best Xmas present contribution to us, I could think of. If you like to do so - go for it.

Switching completely to typescript is something we considered - @adrai started work on a successor for the current i18next implementation - first in typescript but on the way, we decided to no go further with typescript as it felt more like a burden than a win (not sure it's a lack of our understanding of typescript or really just the price you pay for types, idk).

@jamuhl jamuhl merged commit a5a0167 into i18next:master Dec 2, 2020
@jamuhl
Copy link
Member

jamuhl commented Dec 2, 2020

Thank you for your contribution - looking forward to see more from you - well done.

This fix was published in react-i18next@11.7.4

@tigerabrodi
Copy link
Contributor Author

@jamuhl hahaha, wohooo 🍾 🎉, you are awesome, I will never forget this moment nor you 🙌 💕

@tigerabrodi
Copy link
Contributor Author

@jamuhl I think regarding TypeScript, that would not be necessary, but React Testing Library, wohooo 🎉, that'd be really awesome, my friends are also down to help me out @kentcdodds @JacobMGEvans and many more 💕

Thank you again @jamuhl, really, my first step into OSS, I admire it so much, I look up to so many, @kentcdodds himself is one of my greatest inspiration and role model, this really means the world to me, I look forward to my long journey here on github haha 😃 💯 🎉 🍾

@tigerabrodi
Copy link
Contributor Author

@jamuhl Regarding the issue we have at work, I would open an issue for that and try to explain it as thoroughly as possible, thank you again, and thank you @adrai also, you guys have no idea how much this means to me!!! 💕 Regarding converting to React Testing Library, that is the least I can do, someday we will all meet up in a React conf hopefully hahaha. This really means so much to me, more than every single birthday that I have had and will have 😃 🎉

@tigerabrodi
Copy link
Contributor Author

tigerabrodi commented Dec 2, 2020

@jamuhl Regarding the conversion over to React Testing Library, friends from @kentcdodds Discord also wanna help out 🥳. I plan on opening the PR on Friday, and from there magic will happen 😃 💯, I look forward to it, coding with friends is always awesome 😄.

Thanks to you and @adrai, my first big step into OSS 💕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants