Skip to content

How to set the "lang" attribute programatically? In a +page.server.ts for example #12376

Closed Answered by Antoine-lb
Antoine-lb asked this question in Q&A
Discussion options

You must be logged in to vote

Here is the solution:
+page.server.ts

export const load = (async (event) => {
        // use event.locals to set the value to be passed to the hook
	event.locals.locale = 'fr';

	return {};
});

hook.server.ts

export async function handle({ event, resolve }) {
	return resolve(event, {
                // then you can read this local here bc SvelteKit will run this function after the +page.server.ts
		transformPageChunk: ({ html }) => html.replace('%lang%', event.locals.locale) 
	});
}

Thanks @david-plugge and @jhubbardsf for the solution.

Replies: 5 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Antoine-lb
Comment options

Comment options

You must be logged in to vote
1 reply
@Antoine-lb
Comment options

Comment options

You must be logged in to vote
2 replies
@Antoine-lb
Comment options

@jhubbardsf
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by Antoine-lb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #12374 on June 20, 2024 13:03.