You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Add an optional argument to useT to allow passing in a specific tx instance
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.
The text was updated successfully, but these errors were encountered:
What
I would like a mechanism to be able to specify the
tx
instance used to translate byuseT
so I can create reusable custom hooks containing translations hosted in a shared library (essentially the hook equivalent to whatTXProvider
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 thet
returned byuseT
was associated with thetx
instance for the application, not for the shared library. For components hosted in these shared libraries we can wrap them withTXProvider
to ensure they are internally accessing the correcttx
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:
useT
to allow passing in a specifictx
instancetranslateWithElements
to pass in atx
instance as the 3rd argument instead of a reference to the contextI've employed a workaround that is very similar to proposed solution (a clone of
useT
that just usestx.t
directly though as I can't accesstranslateWithElements
) and it seems to be working well. If the proposed solution seems reasonable I'm also happy to submit a PR.The text was updated successfully, but these errors were encountered: