Skip to content

@parlajs/solid

Framework adapter

pnpm add @parlajs/solid

Requires Solid 2. Re-exports @parlajs/core, with rich text typed as Solid’s Element.

The showcase runs on it.

vite.config.ts:

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

The app:

import { createParla, ParlaProvider } from '@parlajs/solid';
import { render } from '@solidjs/web';
import { Errored, Loading } from 'solid-js';
import { catalog } from './parla/catalog.js';
const parla = createParla({ catalog });
render(
() => (
<ParlaProvider parla={parla}>
<Errored fallback={<p>Failed</p>}>
<Loading fallback={<p>…</p>}>
<App />
</Loading>
</Errored>
</ParlaProvider>
),
root,
);
Export
createParla(options) Core’s, with rich text rendered as Solid elements
<ParlaProvider parla> Puts the instance in context
useParla(list) { t }, an accessor. Reading it waits until loaded
useLocale() { locale, locales, switchLocale }; the first two are accessors
Translations<typeof ns> The type of t(), with Element rich text

A failed download reaches the nearest <Errored>; a failed switch rejects switchLocale. The hooks throw outside <ParlaProvider>.

Reading t() waits in the nearest <Loading>; loaded namespaces never wait.

function Cart(props: { count: number }) {
const { t } = useParla([checkout]); // an accessor: reading it waits until loaded
return <h1>{t().checkout.items({ count: props.count })}</h1>;
}

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.

There is no useParlaLoaded: for a placeholder of its own, wrap the component in a <Loading>.

switchLocale loads first, then switches: nothing waits again.

function Switcher() {
const { locale, locales, switchLocale } = useLocale();
const [pending, setPending] = createSignal(false);
const change = (l: Locale) => {
setPending(true);
switchLocale(l)
.catch(reportError)
.finally(() => setPending(false));
};
return (
<For each={locales()}>
{(l) => (
<button disabled={pending() || l === locale()} onClick={() => change(l)}>
{l}
</button>
)}
</For>
);
}

Each tag’s callback returns an Element:

function Terms() {
const { t } = useParla([checkout]);
return <p>{t().checkout.terms({ link: (c) => <a href="/terms">{c}</a> })}</p>;
}

A server loads what it renders. Before hydrating, await parla.load([…]): Solid warns of a mismatch where a boundary hydrates without its namespaces.