Skip to content
Discussion options

You must be logged in to vote

I put together this demo that makes use of form actions to change the page's language: https://www.sveltelab.dev/xya98neroqbjl4t

As long as you have server side rendering, this solutions works with and without JS.

Example

app.html

<html lang="%lang%">

+page.svelte

<form method="POST" use:enhance>
	<button formaction="?l=en">English</button>
	<button formaction="?l=zh">中文</button>
</form>

+page.server.js

import { redirect } from '@sveltejs/kit'

export const load = async ({ cookies, depends }) => {
	depends('site:lang')
	const lang = cookies.get('lang')
	console.log('loading lang', {lang})

	let title = 'Language switcher!'

	if (lang === 'zh') {
		title = '语言切换器'
	}
	return {title};
};

c…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Natr1x
Comment options

Answer selected by Natr1x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants