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

Bind i18n.t #1682

Merged
merged 4 commits into from
Nov 2, 2021
Merged

Bind i18n.t #1682

merged 4 commits into from
Nov 2, 2021

Conversation

joshkel
Copy link
Contributor

@joshkel joshkel commented Oct 26, 2021

I recently ran into #1287, and based on comments there, I saw that I'm not the only one. (In my case, I was trying to write a lower-level function that could take a t function either from useTranslation or from a I18n instance.) Since this does seem to be a somewhat common error, and since destructuring t or trying to use it as a callback can be useful, I believe it would make sense to bind the t function within the I18n constructor (similar to how React class components can bind their callbacks) so that this is always defined, regardless of how the t function is used.

While working on this PR, I fixed some typos, and I noticed that tests were failing, so I set a fixed timezone to help address that. (I can split these changes into separate PRs if you prefer.)

Thank you.

Checklist

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

This facilitates using t via destructuring or as a callback.  Several people have run into this; see #1287.
This fixes some test failures in some time zones (such as Eastern US).
@coveralls
Copy link

coveralls commented Oct 26, 2021

Coverage Status

Coverage increased (+0.03%) to 90.799% when pulling cff6f3f on joshkel:bind-t into cd721b7 on i18next:master.

@jamuhl
Copy link
Member

jamuhl commented Oct 26, 2021

I would take @adrai approach and bind all functions https://github.com/adrai/i18next-next/blob/main/src/i18next.js#L624

On the other hand (my personal opinion): I'm still in the opinion class functions should not be bound but called in the context of the used instance. But - If people love to deconstruct everything - I won't get in the way.

@adrai
Copy link
Member

adrai commented Oct 26, 2021

@joshkel please have a look at this: #1681 (comment)
image

@adrai
Copy link
Member

adrai commented Oct 28, 2021

@joshkel would you like to adapt your PR accordingly?

@joshkel
Copy link
Contributor Author

joshkel commented Nov 2, 2021

@adrai Done. I apologize for the delayed response.

@adrai adrai merged commit b428ec2 into i18next:master Nov 2, 2021
@adrai
Copy link
Member

adrai commented Nov 2, 2021

Thank you, it's included in v21.4.0

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

4 participants