HenryLie / svelte-i18n-lingui

Use lingui for i18n in Svelte/Sveltekit projects
https://www.npmjs.com/package/svelte-i18n-lingui
MIT License
23 stars 2 forks source link

SSR example? #9

Closed bfontaine closed 10 months ago

bfontaine commented 11 months ago

Hello, Thank you for this library. Would you happen to have an example with SSR? I struggle to make it work with my app because of the shared store situation (see https://github.com/cibernox/svelte-intl-precompile/issues/39).

HenryLie commented 10 months ago

Hey @bfontaine, sorry for the late reply. I don't have much experience with SSR, but here is my understanding of the issue:

Therefore, as long as we don't access the store in any load function, the lib should be working just fine with SSR enabled? I tried building a very simple SSR example in this repo in the beta (next) branch. You can check out the branch and run npm run dev to try it out, the next branch has some api difference to the release version but the same approach should be applicable to the release version as well .

In the sample code, a server load function runs on the very first page request since it's on the root layout, gets the browser's preferred language and returns this value to the component. The component then reads this from the page data, sets the preferred language to the store, then shows the page to the user with the preferred language. Does this match your use case?

bfontaine commented 10 months ago

Hello, sorry for my late reply as well. Thank you, it makes sense. The thing I’m missing is that I can’t mark text for translation outside of components, in the server-side code, like I would do using lazy strings in Flask-Babel.