Pre-render with i18n: how to activate locale with dynamic message catalogs? #2666
-
|
Hi! Thank you so much for vike, it's amazing! and inside my root layout, I had to use this: Technically this shouldn't be correct, because this is a conditional without useEffect, but I didn't get any warnings because I passed messages only during pre-render But when I decided to localize head for SEO (e.g. og:description), I had to activate locale even earlier Which made it possible to localize head, but now that messages are passed not only in pre-render, in development mode I get this warning: Cannot update a component ( Which are used to make localized pages properly load in pre-render, because useEffect doesn't get called in pre-render, and this warning recommends using useEffect But in production, it doesn't log anything for some reason. So I wonder if it appears if passing messages only during prerender, or in all cases. Sorry for the long write-up, I would like to find a proper way to make i18n work during pre-render basically and want to see if my workaround is viable or there is a better way (: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 29 replies
-
|
I ain't familiar with Lingui so I can't say much about its error message. Is there something about Vike you think is missing that would help you resolve your issue? |
Beta Was this translation helpful? Give feedback.
-
|
I tried using @brillout Is this the expected behavior of I went back and reread the thread, and I noticed that MrNaif2018 actually mentioned this situation there, but it’s not very clear in the Vike documentation.
Are you using ps: My project is an SPA, where I only lazy-loaded the specific locale file needed by the user. I’m now trying out Vike, and later I’d also like to minify html, so I’m being stricter about this. If that’s the case, I’d rather avoid this approach. It would be ideal if |
Beta Was this translation helpful? Give feedback.



Okay, so for anyone who needs this: because we are in SSR, we need to render localized messages somehow both on server and client
On server, we load locale chunks and just translate (this is what gets pre-rendered)
On client, because i18n object can't be serialized to client directly, we pass locale messages via pageContext, and activate them on client. During client-side navigation, onBeforeRender is called, fetches only the new language's chunk, and it is being applied.
The main thing: as i18n object is global, you must disable parallel pre-rendering!
The alternative solution to this is to get a new i18n instance each time, but …