I am using the router example so i have [lang] folder in the routes and using hooks.js and __layout accordingly to asses the correct language.
Let's say i have a logout endpoint, which is logging out the user and redirecting him to the root /<lang> route.
Of course i can use locale.get() in the server endpoint like this:
export async function GET({locals,request}) {
//logout user logic, setting cookies or similar....
return {
status: 302,
headers: {
location: "/"+locale.get()+"/", //<--------
},
};
}
However because stores are shared between request on the server this is problematic when using multiple requests and will actually not result in the correct locale.
A simple was to test this is to
login in Browser A with language A
login in Browser B with language B
then refresh Browser A and try to logout in Browser B
=>This will redirect to Language A in Browser B because the locale is still set to A.
Possible Solutions
There are ways to handle this with setting the context. Honestly i haven't tried it and it would be better to integrate it into the library here if it is feasible (unfortunately this would mean a heavy coupling to svelte)
What are your thoughts on that? Should we completely avoid using locale.get()?
1) In that case the language would need to be fetched from the path and if not set from the accept-language header, duplicating the code already there in the hooks.js file. In that case one could maybe export the locale from the hook.js file to the locals variable of sveltekit and use that instead. However if i need to use the t method to translate stuff, it would still use the wrong language.
2) Also it is possible to execute locale.set(langFromPath) everytime in the hooks.js file. I am not sure if that is a good idea or not, because it can still maybe lead to race conditions if request get processed in parallel.
Problem
I am using the router example so i have
[lang]
folder in the routes and using hooks.js and __layout accordingly to asses the correct language.Let's say i have a logout endpoint, which is logging out the user and redirecting him to the root
/<lang>
route.Of course i can use
locale.get()
in the server endpoint like this:However because stores are shared between request on the server this is problematic when using multiple requests and will actually not result in the correct locale. A simple was to test this is to
Possible Solutions
There are ways to handle this with setting the context. Honestly i haven't tried it and it would be better to integrate it into the library here if it is feasible (unfortunately this would mean a heavy coupling to svelte)
What are your thoughts on that? Should we completely avoid using
locale.get()
?1) In that case the language would need to be fetched from the path and if not set from the accept-language header, duplicating the code already there in the hooks.js file. In that case one could maybe export the locale from the hook.js file to the locals variable of sveltekit and use that instead. However if i need to use the
t
method to translate stuff, it would still use the wrong language.2) Also it is possible to execute
locale.set(langFromPath)
everytime in the hooks.js file. I am not sure if that is a good idea or not, because it can still maybe lead to race conditions if request get processed in parallel.