How to set the "lang" attribute programatically? In a +page.server.ts for example #12376
-
Describe the problemHello 👋 This is because I only know what language the page is when I load the article (in the This problem is very annoying bc if not done right, on some Androids Google will just translate the site without asking (is a opt out) just based on this Describe the proposed solutionUsing Alternatives consideredRun Importancewould make my life easier Additional InformationOther people with the same problem: |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
Another potential solution would be to add a |
Beta Was this translation helpful? Give feedback.
-
you posted here about the same problem already. opral/inlang-paraglide-js#134 converting to discussion as this is not an issue with sveltekit itself but rather a question about an unusual arcitecture. |
Beta Was this translation helpful? Give feedback.
-
if you have no information available in the url directly about the language to be used, you can create a lookup table and access that in the handle hook. Remember to also translate the whole page to that language if you set lang at the html level, not just the article itself. If the page is always in one language, and the article in another, you can use embedded lang attribute
|
Beta Was this translation helpful? Give feedback.
-
You can use |
Beta Was this translation helpful? Give feedback.
-
Here is the solution: export const load = (async (event) => {
// use event.locals to set the value to be passed to the hook
event.locals.locale = 'fr';
return {};
});
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. |
Beta Was this translation helpful? Give feedback.
Here is the solution:
+page.server.ts
hook.server.ts
Thanks @david-plugge and @jhubbardsf for the solution.