-
-
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
Another approach for SSR with multiple namespaces #711
Comments
That's not a suggestion - it's one of the things you can do. It's impossible to write a documentation for every possible combination of things existing out there. So all the SSR stuff is giving hints, directions how to approach the problem. The express-middleware has the preload set prominently: https://github.com/i18next/i18next-express-middleware#wire-up-i18next-to-request-object Suggestion is heading to next-i18next: https://github.com/isaachinman/next-i18next There is reportNS for what you like to achive i guess: https://github.com/i18next/react-i18next/blob/v9.x.x/src/I18nextProvider.js#L14 --> https://github.com/i18next/react-i18next/blob/v9.x.x/src/NamespacesConsumer.js#L44 (currently not documented --- and old v9 : same exists in v10 - but not yet had time to document that stuff). PR to docs is more than welcome |
@jamuhl thx for quick response, I will check whether I can connect all the dots. If yes, I could make a PR to docs, but I am wondering whether it wouldn't be better to wait until vs 10 is ready, which I guess is coming quite soon. |
I just checked undocumented Please let me know if version 10 is going to be backward compatible regarding |
v10 has a Provider: https://github.com/i18next/react-i18next/blob/master/src/I18nextProvider.js which only acts as a way to pass i18n instance down via context. All |
I might be missing something, but from what I can see, this solution will cause issues compared to |
@klis87 you're right...will fix this asap. Best might be the same as in the last version - passing a function via context. |
@jamuhl Yeah, I think |
might get to in on monday...will ping you as soon it's published |
in [email protected] it will append a https://github.com/i18next/react-i18next/blob/master/src/context.js#L29 to the i18n instance passed in (global one or the req.i18n via provider: https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L35) so you will be able to read them out like: https://github.com/i18next/react-i18next/blob/master/test/useTranslation.usedNamespaces.spec.js#L37 hope that works for you ?!? |
@jamuhl I don't think It will. From what I understand i18n is a singletone object, which is perfect for server side rendering - we can just once load a namespace and share it for multiple requests - it doesn't make sense to load them for each request as translations are static, we can preload everything once, components will have access to anything, and as a last SSR step, we can filter translations to pass to client as initial i18nStore only those translations that were actually used, so they won't be downloaded again by XHR backend Looking at this code, what would happen is that for the first In my opinion we have 2 solutions:
const usedNamespaces = {};
const provider =<I18nextProvider i18n={req.i18n} initialLanguage={language} usedNamespaces={usedNamespaces}>
// ... render components
// read usedNamespaces to see which namespaces were used for THIS request,
// this would work as we create new usedNamespaces for each request |
on server side it should not be the global singleton - if doing so i promise you will run into concurrency issues in production where one request overwrites the language of another... the express middleware creates a cloned instance on req.i18n (sharing the loaded namespaces but not the set language) -> that should be passed to I18nextProvider |
Indeed, lang is property of i18n instance too, so mixed languages would be guaranteed :) So I confirm it will work then! Thx for the quick fix and generally for awesome i18n library! I spent much time checking all alternatives and no one is even close to i18next! I will wait until v 10 is released and create a PR for SSR docs |
Thank you for taking the time - checking i18next and it's potential - most people just see other solutions are simpler - or comparing size of the library with their own solution (not providing more than a key lookup). |
but there are also big libraries like react-intl of Yahoo, much more popular - currently 9k + stars, despite the fact that they don't even have proper loader of translations, not to mention namespaces (at the time I checked it at least), which generally is also harder to use than i18next But what can you do, anything done by big company has marketing advantage. I wish you luck getting more and more popularity, you deserve it! |
ok 16.8 was released .... will close this now and publish v10.0.0 to npm 🔥 |
Is your feature request related to a problem? Please describe.
Current suggestion for SSR is in my opinion not complete and optimal, especially when having multiple namespaces. In https://react.i18next.com/misc/serverside-rendering#loadnamespaces-helper it is mentioned we could use
match
from React-router to know which namespaces are needed. This is really problematic because:redux-first-router
Describe the solution you'd like
I suggest a simpler solution for SSR:
withNamespaces
for a given React component on the server won't be actually used to load a given namespace for a given language (yes, it will load namespaces but this would be cached already), it would be used to know which namespaces are needed to be passed to the client ininitialI18nStore
How we could achieve it? For each request we could create an object like
set
or[]
(set would be better but maybe you would prefer array as set is not implemented in every browser) and pass it to provider like:Now,
usedNamespaces
could be added to i18next context, and in proper i18next hocs/components, likewithNamespaces
,usedNamespaces
set could be mutated with used namespace/namespaces in a given component, for exampleusedNamespaces.add('someNamespaceName')
.Then, after components are rendered, we could read
usedNamespaces
to see which namespaces were used for this specific request. Then, we can used this info to pass optimalinitialI18nStore
to the client.Describe alternatives you've considered
initialI18nStore
- defeats the purpose of using namespaces in the first placeAdditional context
N/A
The text was updated successfully, but these errors were encountered: