farincz
farincz

Reputation: 5173

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.

Upvotes: 1

Views: 1027

Answers (1)

brunnerh
brunnerh

Reputation: 185225

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;

Upvotes: 3

Related Questions