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: |
Replies: 5 comments 4 replies
|
Another potential solution would be to add a |
|
you posted here about the same problem already. opral/paraglide-js#134 converting to discussion as this is not an issue with sveltekit itself but rather a question about an unusual arcitecture. |
|
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 |
|
You can use |
|
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. |
Here is the solution:
+page.server.tshook.server.tsThanks @david-plugge and @jhubbardsf for the solution.