Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Reasonable way to avoid locale resetting on preload? #143

Closed
henrikvilhelmberglund opened this issue Sep 11, 2023 · 1 comment
Closed

Reasonable way to avoid locale resetting on preload? #143

henrikvilhelmberglund opened this issue Sep 11, 2023 · 1 comment

Comments

@henrikvilhelmberglund
Copy link

I have a few buttons for selecting the user language which change the $locale store to the locale string. I had an issue where $locale would get reset to whatever initLocale was in the +layout.js when hovering over links (preload) and when changing the route.

I fixed(?) it by using the following code:

const initLocale = get(locale) ? get(locale) : "en"; // get from cookie, user session, ...

await loadTranslations(initLocale, pathname); 

which seems to work as it uses the $locale store value if it exists, otherwise "en" as a default. (Should probably use cookies/localstorage for this later)

My main question is, is there a better way to do this? I'm very new to i18n.

@jarda-svoboda
Copy link
Member

Hi @henrikvilhelmberglund!
Your solution is quite tricky as the locale store is shared between users on server. Recommended is to use cookies to keep user’s preferred language.. see example here.

@sveltekit-i18n sveltekit-i18n locked and limited conversation to collaborators Sep 12, 2023
@jarda-svoboda jarda-svoboda converted this issue into discussion #145 Sep 12, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants