Skip to content

Language handling / redirection for sveltekit : Example implementations #1807

Discussion options

You must be logged in to vote

Yes! This is a very good way to implement language redirection!
What you could do is to pass the language that you detect here into your layout. That way there isn't the risk that the client & server get out of sync.

To do this, set event.locals.lang to the userLanguage you detect here, and in your root +layout.js file pass it to the frontend:

// src/routes/[[lang]]/+layout.js

export function load({ locals }) {
    return { lang: locals.lang}
}

And then in your layout:

<script>
    import { setLanguageTag } from "../paraglide/runtime.js"
    export let data;
    
    $: setLanguageTag(data.lang)
</script>


{#key data.lang}
    <slot />
{/key}

Doing it this way ensures that there is neve…

Replies: 4 comments 6 replies

Comment options

You must be logged in to vote
1 reply
@MarArMar
Comment options

Comment options

You must be logged in to vote
1 reply
@MarArMar
Comment options

Answer selected by MarArMar
Comment options

You must be logged in to vote
3 replies
@psntr
Comment options

@eikaramba
Comment options

@psntr
Comment options

Comment options

You must be logged in to vote
1 reply
@eikaramba
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants