Last active
October 18, 2023 17:28
-
-
Save DanikVitek/7d0478646a4bdb61f1014cd5174d5c6a to your computer and use it in GitHub Desktop.
CSR ParaglideJS SvelteKit adapter w/ cookies-based persistence, SE optimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { AvailableLanguageTag } from '@inlang/paraglide-js/website'; | |
import type { LayoutServerLoad } from './$types'; | |
export const load: LayoutServerLoad = ({ cookies }) => { | |
const languageTagCookie = cookies.get('languageTag') as AvailableLanguageTag; | |
return { languageTagCookie }; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import '../app.css'; | |
/** @type {import('./$types').LayoutServerData} */ | |
export let data; | |
</script> | |
<ParaglideJsProviderCSR languageTagCookie={data.languageTagCookie}> | |
<!-- ... --> | |
</ParaglideJsProviderCSR> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html data-theme="dark" %lang%> | |
<head> | |
<meta charset="utf-8" /> | |
<link rel="icon" href="%sveltekit.assets%/favicon.svg" /> | |
<meta name="viewport" content="width=device-width" /> | |
%sveltekit.head% | |
</head> | |
<body data-sveltekit-preload-data="hover"> | |
<div style="display: contents">%sveltekit.body%</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
availableLanguageTags, | |
sourceLanguageTag, | |
type AvailableLanguageTag, | |
} from '@inlang/paraglide-js/website'; | |
import type { Handle } from '@sveltejs/kit'; | |
export const handle: Handle = async ({ event, resolve }) => { | |
const { | |
cookies, | |
request: { headers }, | |
} = event; | |
let languageTagCookie = cookies.get('languageTag') as AvailableLanguageTag | undefined; | |
if (languageTagCookie === undefined || !availableLanguageTags.includes(languageTagCookie)) { | |
const acceptLanguageHeader = headers.get('accept-language'); | |
const preferredLanguageTag = | |
acceptLanguageHeader !== null | |
? availableLanguageTags.find((tag) => | |
acceptLanguageHeader.includes(tag.split('-')[0]) | |
) | |
: undefined; | |
cookies.set('languageTag', preferredLanguageTag || sourceLanguageTag, { httpOnly: false }); | |
languageTagCookie = preferredLanguageTag || sourceLanguageTag; | |
} | |
return resolve(event, { | |
transformPageChunk: ({ html }) => html.replace('%lang%', `lang="${languageTagCookie}"`), | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { browser } from '$app/environment'; | |
import { languageTag, onSetLanguageTag, setLanguageTag } from '@inlang/paraglide-js/website'; | |
/** @type {import('@inlang/paraglide-js/website').AvailableLanguageTag} */ | |
export let languageTagCookie; | |
setLanguageTag(languageTagCookie); | |
// initialize a reactive language tag | |
$: _languageTag = languageTag(); | |
// the server should not listen for language tag changes | |
if (browser) { | |
onSetLanguageTag((newLanguageTag) => { | |
// set the reactive language tag | |
_languageTag = newLanguageTag; | |
document.cookie = `languageTag=${newLanguageTag}`; | |
document.getElementsByTagName('html')[0].setAttribute('lang', newLanguageTag); | |
}); | |
} | |
</script> | |
<svelte:head> | |
<meta http-equiv="content-language" content={_languageTag} /> | |
</svelte:head> | |
<!-- trigger a re-render if the language tag changes --> | |
{#key _languageTag} | |
<slot /> | |
{/key} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment