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

[React] Update useT to allow specifying the tx instance to use #182

Closed
unkillbob opened this issue Feb 23, 2023 · 3 comments
Closed

[React] Update useT to allow specifying the tx instance to use #182

unkillbob opened this issue Feb 23, 2023 · 3 comments

Comments

@unkillbob
Copy link

unkillbob commented Feb 23, 2023

What
I would like a mechanism to be able to specify the tx instance used to translate by useT so I can create reusable custom hooks containing translations hosted in a shared library (essentially the hook equivalent to what TXProvider enables with components).

Why
At our organisation there are some lists of translations (e.g. map of currency code to translated currency name) that are hosted in shared libraries to be used across multiple React applications.

Initially we tried creating some custom hooks in these shared libraries and using useT to do the translation within the hooks. However this didn't work as the t returned by useT was associated with the tx instance for the application, not for the shared library. For components hosted in these shared libraries we can wrap them with TXProvider to ensure they are internally accessing the correct tx instance when translating, however there doesn't appear to be an equivalent mechanism for hooks.

Potential Solution
One possible way to solve this would be:

  1. Add an optional argument to useT to allow passing in a specific tx instance
  2. Change translateWithElements to pass in a tx instance as the 3rd argument instead of a reference to the context

I've employed a workaround that is very similar to proposed solution (a clone of useT that just uses tx.t directly though as I can't access translateWithElements) and it seems to be working well. If the proposed solution seems reasonable I'm also happy to submit a PR.

@n1k0sv
Copy link

n1k0sv commented Jan 4, 2024

@unkillbob Apologies for taking so long to reply.

I've opened a related PR for your request, if you'd like to take a look: #205

The scope has increased to the other hooks as well.

@unkillbob
Copy link
Author

thanks @nbasili, I took a look and it looked good to me!

@n1k0sv
Copy link

n1k0sv commented Jan 26, 2024

Fix has been released on @transifex/[email protected]

@n1k0sv n1k0sv closed this as completed Jan 26, 2024
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

No branches or pull requests

2 participants