Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set lang attribute on <html> tag in SSR mode with SvetleKit

I know user’s preferred language in cookie and I am able to init locales with it (using server hook).
And I would like to also render correct lang attribute which matches selected locale.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are docs on specifically that.

If your content is available in multiple languages, you should set the lang attribute based on the language of the current page. You can do this with SvelteKit’s handle hook:

src/app.html

<html lang="%lang%">

src/hooks.server.ts

import type { RequestEvent, Handle } from '@sveltejs/kit';
export const handle = (({ event, resolve }) => {
  return resolve(event, {
    transformPageChunk: ({ html }) =>
      html.replace('%lang%', get_lang(event))
  });
}) satisfies Handle;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading