Skip to content

@parlajs/svelte

Framework adapter

pnpm add @parlajs/svelte

Requires Svelte 5.36. Re-exports @parlajs/core, with rich text typed as data that <Rich> renders.

The showcase runs on it.

vite.config.ts:

import { parla } from '@parlajs/compiler/vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vite';
export default defineConfig({ plugins: [parla(), svelte()] });

svelte.config.js, for useParla only:

export default { compilerOptions: { experimental: { async: true } } };

The root component:

<script lang="ts">
import { createParla, setParla } from '@parlajs/svelte';
import App from './App.svelte';
import { catalog } from './parla/catalog.js';
setParla(createParla({ catalog }));
</script>
<svelte:boundary>
<App />
{#snippet pending()}…{/snippet}
{#snippet failed(error)}Failed{/snippet}
</svelte:boundary>
Export
createParla(options) Core’s, with rich text as Rich
setParla(parla) Puts the instance in context: call it in the root component
useParla(list) { t }, a promise to await in $derived. Needs experimental.async
useParlaLoaded(list) { t }, undefined until loaded
useLocale() { locale, locales, switchLocale }: read them from the object, not destructured
<Rich value> Renders rich text. Also the name of its type
Translations<typeof ns> The type of t, with Rich rich text

A failed download reaches the nearest boundary; a failed switch rejects switchLocale. The functions throw without setParla.

<script lang="ts">
import { useParla } from '@parlajs/svelte';
import { checkout } from './parla/namespaces/checkout.js';
import Terms from './Terms.svelte';
const { count }: { count: number } = $props();
const messages = useParla([checkout]);
const t = $derived(await messages.t); // waits in <svelte:boundary>
</script>
<h1>{t.checkout.items({ count })}</h1>
<Terms />

Any component can call useParla or take t as a prop. A namespace loads once per locale. To load before a page renders, await parla.load([…]) in its route loader.

Use useParla by default, and useParlaLoaded for a placeholder of your own, or without experimental.async:

<script lang="ts">
import { useParlaLoaded } from '@parlajs/svelte';
import { checkout } from './parla/namespaces/checkout.js';
const messages = useParlaLoaded([checkout]);
</script>
<span>{messages.t?.checkout.title() ?? '…'}</span>

switchLocale loads first, then switches: nothing waits again.

<script lang="ts">
import { type Locale, useLocale } from '@parlajs/svelte';
const locales = useLocale();
let pending = $state(false);
const change = (l: Locale) => {
pending = true;
locales
.switchLocale(l)
.catch(reportError)
.finally(() => (pending = false));
};
</script>
{#each locales.locales as l}
<button disabled={pending || l === locales.locale} onclick={() => change(l)}>{l}</button>
{/each}

Svelte has no element values, so a tag’s callback returns a snippet and its children, and <Rich> renders the message:

<script lang="ts">
import { Rich, useParla } from '@parlajs/svelte';
import { checkout } from './parla/namespaces/checkout.js';
const messages = useParla([checkout]);
const t = $derived(await messages.t);
</script>
{#snippet link(children: Rich)}<a href="/terms"><Rich value={children} /></a>{/snippet}
<p><Rich value={t.checkout.terms({ link: (children) => ({ snippet: link, children }) })} /></p>