Closed pago93 closed 2 months ago
As mentioned in the docs, the languageTag isn't set on the server before rendering starts. This is to avoid cross-talk between different concurrent requests.
To use the language on the server (outside of .svelte
files or code called from .svelte
files) you need to get it from the locals. You can access the current language on event.locals.paraglide.lang
.
Calling languageTag()
on the server should probably throw an error with an explanation. I'll see if I can do that.
You can get a message in a specific message by passing it into the message options m.my_message({}, { languageTag: "en"})
The load function runs on the Browser (see ssr = false). The log output is from the browser. Maybe not the best example for this... I see if I can get a repro up later.
Repro is here: https://github.com/pago93/ParagildeJSLanguageTag I created a new SvelteKit app, added paraglide as per documentation and modified the root page to demonstrate it.
For http://localhost:5173/de/
I get the following:
Code for reference: https://github.com/pago93/ParagildeJSLanguageTag/blob/8def2fe07ee453b020019a1e6d5a87aa5aa689eb/src/routes/%2Bpage.svelte#L13
Browser console:
Maybe I am doing something wrong here, but I expected languageTag()
to give me the correct language because the code is executed in the browser and not on the server.
Thanks! Will be working on this today.
I expected
languageTag()
to give me the correct language because the code is executed in the browser
That's how it's supposed to work, you aren't doing anything wrong here
I have a working version that eagerly sets the language as the first thing when running reroute
on the client.
This will still not set the language in module-scope
languageTag() //won't work
export function load() {
if(client) languageTag() //will work
}
That being said, using the language in module scope is a bad idea anyway since it won't be possible to react to language changes.
Actually, scratch everything I've said. We can't use the languageTag in load functions on the client either.
The reason is SvelteKit's preloading behavior. load
functions, including the ones on the client, are run if a link is hovered. This causes an obvious issue when hovering a link to a page in a different language.
Let's say the current languages is en
and you hover a link to /de/ueber-uns
. This is going to run the load function for /de/ueber-uns
. This leaves us in the following Catch 22:
load
function context. languageTag()
in the load function would return the wrong value. Unfortunately the restriction on not using languageTag()
in load functions needs to stay.
Calling
languageTag()
from$lib/paraglide/runtime.js
with@inlang/paraglide-js-adapter-sveltekit
does always return the default language for me.In my SvelteKit app with
@sveltejs/adapter-static
(don't know if relevant) I can't get the correct language via the built in functionality. A workaround I found is calling 'getLanguageFromUrl(url)' with the currenturl
. The example below gets me the following output for routehttps://my-site/de/x
:Code for reference (
+page.ts
forhttps://my-site/de/x
):i18n.resolveRoute
also doesn't get the correct route without the language tag.I get the same beviour for example in
onMount()
.