Skip to content

Writing an adapter

Framework adapter

Built on @parlajs/core, as @parlajs/react is.

A framework needs Core gives
To re-render on a switch subscribe(listener) and locale
Suspense load(list): the same list returns the same promise, with status and value once settled
No Suspense loaded(list): t, or undefined until loaded
Its own node for rich text wrapRich(parts), typing Parla<Node> and Translations<NS, Node>
One shared locale state @parlajs/core as a dependency, re-exported: a second copy warns
import { createParla as createCore } from '@parlajs/core';
import type { Loaded, Namespace, Parla, ParlaOptions, WrapRich } from '@parlajs/core';
// Called only when a part is not a string; all-text messages stay strings.
const wrapRich: WrapRich<MyNode> = (parts) => fragment(parts);
export function createParla(options: ParlaOptions<MyNode>): Parla<MyNode> {
return createCore<MyNode>({ wrapRich, ...options });
}
// `t` for some namespaces, kept current across switches.
export function watch<const L extends ReadonlyArray<Namespace>>(
parla: Parla<MyNode>,
list: L,
set: (t: Loaded<L, MyNode> | undefined) => void,
): () => void {
const update = (): void => {
const t = parla.loaded(list);
set(t);
if (!t) void parla.load(list).then(update, reportError);
};
update();
return parla.subscribe(update);
}

A listener’s loaded already has the new locale’s t. To copy from: Solid, Svelte and Vue.