Fix concurrency issues due to i18n singleton
See original GitHub issueWe have a problem that is around since the examples of with-i18next
on next.js repository which we should try to fix because is really bad issue.
The problem is that we have one instance of i18next
and we pass it to the provider
<I18nextProvider
i18n={i18n}
initialLanguage={initialLanguage}
initialI18nStore={initialI18nStore}
>
What happens here is that if a request hits the server and change the language before next renders the html it will render with the wrong language.
I solved the problem by cloning the i18next
instance before passing to the provider
...
render() {
const clone = i18n.cloneInstance({
initImmediate: false,
lng: initialLanguage,
})
return (
...
<I18nextProvider
i18n={clone}
initialLanguage={initialLanguage}
initialI18nStore={initialI18nStore}
>
...
)
}
This also happens if you use the i18n
instance outside of react context ( and this problem I couldn’t think how to solve it yet ).
For example:
I have a file where I setup our apollo-client
instance, and there I have some middlewares which inject some headers like this:
import i18n from '../i18n' // <--- the file which exports the i18next instance
...
// Accept language link
const acceptLanguageLink = new ApolloLink((operation, forward) => {
operation.setContext(({ headers = {} }) => {
return {
headers: {
...headers,
'Accept-Language': i18n.language, // <--- Prone to concurrency issue
},
}
})
return forward(operation)
})
This issue is much more complex than looks like and I would love to think in a solution with you guys however I have some questions about the next-18next
setup which @capellini or @isaachinman could give me some insights.
I’ll start with:
- What is the context of
this
here: https://github.com/isaachinman/next-i18next/blob/master/src/hocs/app-with-translation.js#L12. I’m asking to know where thei18n
comes from.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (9 by maintainers)
https://github.com/isaachinman/next-i18next/issues/5#issuecomment-440384604
Released in v0.24.0.