Skip to content

@parlajs/core

Core

pnpm add @parlajs/core, or through a framework adapter, which re-exports it.

import { createParla, matchLocale } from '@parlajs/core';
import { catalog } from './parla/catalog.js';
import { checkout } from './parla/namespaces/checkout.js';
const parla = createParla({ catalog, locale: 'it' });
const t = await parla.load([checkout]);
t.checkout.cart.items({ count: 3 }); // "3 articoli"
Option
catalog catalog.js, written by the build
locale The first locale; default the source locale
timeZone IANA zone dates format in, unless a message sets one; default the host’s
functions Required, with every name the config’s functions declares
markup Required, with every name the config’s markup declares
wrapRich Joins rich parts into one node; set by framework adapters
escape Escapes every param of a text instance, as in HTML emails
Member Type
locale Locale Current
locales readonly Locale[] The catalog’s
load(list) Promise<Loaded<L, Node>> See Loading
loaded(list) Loaded<L, Node> | undefined t if loaded for the current locale; never downloads
switchLocale(locale) Promise<void> Loads, then switches; see Switching locale
subscribe(listener) () => void, to unsubscribe Called after each switch

load’s promise carries status and value once settled, so React’s use() reads it without suspending.

Export
matchLocale(requested, supported, fallback) Best supported locale from a preference list
parseAcceptLanguage(header) An Accept-Language header’s 32 first tags by weight, for matchLocale
direction(locale) 'ltr' or 'rtl', for <html dir>
escapeHtml(text) Escapes &, <, >, " and ', for escape
Translations<typeof ns> The type of t for ns, or a union of them; Node is string
Loaded<L> The t that load returns for a list of namespaces
Namespace<Name, Kind, L> A namespace object: { name, load }
Catalog { locales, sourceLocale, fallbacks? }; Intl follows fallbacks
Locale The app’s locales, once a catalog is built
Text A message without params or markup: () => string
WrapRich<Node> Joins rich parts into one node; see Writing an adapter
MessageFunction ({ value, options, locale, locales }) => string, for functions
LocaleRegistry, FunctionRegistry, MarkupRegistry Augmented by generated code; never by hand

Load one copy of @parlajs/core: a library makes it a peerDependency. A second copy warns.