-
-
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
Inform useTranslation hook of replaced i18n instance #1272
Comments
Not sure who came up with that idea of replacing the instance in the i18nProvider -> correct would be a call to i18n.changeLanguage which would replace the Changing the instance looks like the badest idea regarding changing the language -> the instance is the base of everything -> all the events are bound to it: https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L65 So, replacing the instance and just shallow rerender breaks basically everything... |
@isaachinman this may interest you |
In This pattern works fine without shallow routing, but breaks with shallow routing. At the moment I don't have any clever ideas about how to reconcile this. |
Ok, I see. How do you recommend updating the i18n store when routing to another page? In the case of Next.js I get the language data preloaded from On the other side, I wonder why it is not possible to check in the |
If, in shallow routing, the |
@isaachinman When switching the language and just stay on the same page I would expect the state of the page not to get lost. This is why I choose shallow routing in this scenario. |
@medihack what you might try is adding the i18n instance https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L9 to the array of side-effects https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L75 so a new i18n instance should rerun the bindings... |
@jamuhl Do you mean by wrapping the existing |
No, changing that in your node_modules folder and test -> if it solves your issue provide a PR with the needed changes |
Ok, I'll give it a try and report back. |
I guess this issue is exactly what I’ve been trying to investigate this Friday night 😅 I have a project with two locales ( Here’s the missing piece, which has fixed everything for me locally! React.useEffect(() => {
setT(getT())
}, [i18n]); I added it after Thanks for the clue @jamuhl! I hope that my experiment will help us with fixing this issue 🙌 |
Yes, I came to the same result of how to fix it (see below). Just about to write a test and then make a PR.
|
One little thing to add. It won't fix the shallow routing problem in next-i18next completely (but it makes it at least possible). The code there has another little problem, but I will discuss it over there. |
Fix allow to replace i18n in provider with useTranslation hook (#1272)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
🐛 Bug Report
I use the
I18nextProvider
to distribute the i18n instance in a Blitz.js app with SSR. When switching the language I simply replace the i18n instance in the provider (this is also how next-i18next works). This works when recreating every component, but breaks when just rerendering every component. It is becauset
ofuseTranslation
is cached.This also breaks next-i18next when using shallow routing. And it is not only a problem with shallow routing, but also with persistent layouts as those components in the layout (like app bars) are also not translated correctly after a page change (and replacing the i18n instance).
To Reproduce
Replace the i18n instance of a
I18nextProvider
and just force a rerender of all components that use theuseTranslation
hook. I also demonstrated this with a little code change in the example app of next-i18next (https://github.com/medihack/next-i18next).Expected behavior
The
useTranslation
hook should somehow check if its provided i18n instance was replaced and not just provide a cachedt
function.Your Environment
The text was updated successfully, but these errors were encountered: