Skip to content

Server

Node runs the generated chunks without a bundler, and nothing in @parlajs/core needs a browser.

Create one instance per locale at startup and preload what the server uses:

import { createParla, matchLocale, parseAcceptLanguage } from '@parlajs/core';
import { catalog } from './parla/catalog.js';
import { emails } from './parla/namespaces/emails.js';
import { errors } from './parla/namespaces/errors.js';
const instances = new Map(
await Promise.all(
catalog.locales.map(async (locale) => {
const parla = createParla({ catalog, locale, timeZone: 'Europe/Rome' });
await parla.load([emails, errors]);
return [locale, parla] as const;
}),
),
);
export function parlaFor(acceptLanguage: string | null | undefined) {
const requested = parseAcceptLanguage(acceptLanguage);
return instances.get(matchLocale(requested, catalog.locales, catalog.sourceLocale))!;
}
const parla = parlaFor(req.headers['accept-language']);
const t = await parla.load([errors]); // loaded at startup: resolves at once
res.setHeader('content-language', parla.locale);
res.end(t.errors.notFound({ path: req.url ?? '' }));

An instance per request also works, at ~1 µs: instances of the same catalog and options share what they loaded, for a catalog’s last eight sets of options, so a few time zones too. Define functions, markup and escape once, as they are compared by identity.

Dates format in the host’s zone, rarely the reader’s. createParla({ timeZone }) sets the zone; a message’s own timeZone option still wins. For a zone per reader, keep an instance per locale and zone.

Render with the request’s instance and put its locale in <html lang dir>. The client creates its instance in that locale and zone; otherwise hydration fails and re-renders in the source locale.

// server: createParla from @parlajs/react, so rich text is React nodes
renderToPipeableStream(
<html lang={parla.locale} dir={direction(parla.locale)}>
<body>
<ParlaProvider parla={parla}>
<App />
</ParlaProvider>
</body>
</html>,
);
// client
const parla = createParla({
catalog,
locale: matchLocale([document.documentElement.lang], catalog.locales, catalog.sourceLocale),
timeZone: 'Europe/Rome',
});
hydrateRoot(document, <Root parla={parla} />);

The client need not preload: while useParla suspends, React keeps the server’s HTML. renderToString does not wait, so its instance must be preloaded.

Under the react-server condition, @parlajs/react has createParla and the types, no hooks or provider. Load in the component:

export default async function Checkout({ parla }: { parla: Parla }) {
const t = await parla.load([checkout]);
return <h1>{t.checkout.title()}</h1>;
}

A string instance inserts params as they are. For HTML, pass escape: it escapes every param, like i18next’s escapeValue, and leaves translations and markup as written. React escapes on its own, so @parlajs/react takes no escape.

Translations are HTML here: write &lt; for a literal <, and trust or review whoever writes them.

import { createParla, escapeHtml } from '@parlajs/core';
const mail = createParla({ catalog, locale, escape: escapeHtml });
const t = await mail.load([emails]);
t.emails.welcome({ name: user.name, b: (c) => `<b>${c}</b>` }); // <b>&lt;i&gt;Ann…</b>