Getting started
Three layers: @parlajs/compiler builds the catalogs, @parlajs/core runs them, and
an adapter such as @parlajs/react brings core to a framework. Tooling, such as x18n, is optional.
React + Vite
Section titled “React + Vite”pnpm add @parlajs/reactpnpm add -D @parlajs/compilernpm install @parlajs/reactnpm install -D @parlajs/compilerOne catalog per locale and namespace, locales/en/checkout.json:
{ "title": "Checkout", "cart": { "items": ".input {$count :number}\n.match $count\none {{{$count} item}}\n* {{{$count} items}}" }, "terms": "Accept the {#link}terms{/link}"}parla.config.ts, next to package.json:
import { defineConfig } from '@parlajs/compiler';
export default defineConfig({ locales: ['en'], // add each language as its catalogs arrive sourceLocale: 'en', outDir: 'src/parla', namespaces: { checkout: { format: 'mf2', path: 'locales/{locale}/checkout.json' } },});vite.config.ts. The plugin rebuilds on every catalog change:
import { parla } from '@parlajs/compiler/vite';import react from '@vitejs/plugin-react';import { defineConfig } from 'vite';
export default defineConfig({ plugins: [parla(), react()] });The app:
import { createParla, matchLocale, ParlaProvider, useParla } from '@parlajs/react';import { Suspense } from 'react';import { createRoot } from 'react-dom/client';
import { catalog } from './parla/catalog.js';import { checkout } from './parla/namespaces/checkout.js';
const parla = createParla({ catalog, locale: matchLocale(navigator.languages, catalog.locales, catalog.sourceLocale),});
function Cart() { const { t } = useParla([checkout]); // suspends until loaded return <h1>{t.checkout.cart.items({ count: 3 })}</h1>;}
createRoot(document.querySelector('#root')!).render( <ParlaProvider parla={parla}> <Suspense fallback={null}> <Cart /> </Suspense> </ParlaProvider>,);Add src/parla/ to .gitignore: tools such as knip then need no build. To render without Suspense, see React.
The CLI, without Vite
Section titled “The CLI, without Vite”Any bundler with import() works. Run the compiler beside it:
parla build # once, before the bundlerparla build --watch # beside the bundler's dev serverparla check --strict # in CI: warnings fail too{ "scripts": { "build": "parla build && tsc && your-bundler build", "dev": "parla build --watch & your-bundler dev" }}Elsewhere, use the Solid, Svelte or Vue adapter, or
@parlajs/core.
What it writes
Section titled “What it writes”outDir/ |
|
|---|---|
catalog.js, .d.ts |
Locales and source locale; types the app’s locales |
namespaces/{ns}.js, .d.ts |
The namespace object to import, and its message types |
messages/{locale}/{ns}.js |
One chunk per locale × namespace, loaded by import() |
Layers
Section titled “Layers”catalogs JSON, or what a parseFile reads └▶ format adapter ICU, MF2 or yours → MF2 data model └▶ compiler build time → typed namespaces └▶ core runtime: locale, loading, Intl └▶ framework adapter React, Solid, Svelte, Vue or yours
tooling, beside them x18n reads catalogs and usages| Layer | Package | Does |
|---|---|---|
| Format adapter | @parlajs/format-icu, @parlajs/format-mf2 |
Parses a syntax into the MF2 data model |
| Core | @parlajs/compiler |
Builds catalogs into typed namespaces |
| Core | @parlajs/core |
Runtime: locale state, loading, Intl |
| Framework adapter | @parlajs/react, solid, svelte, vue |
useParla, useLocale |
| Tooling | @parlajs/x18n-plugin |
Reads catalogs and usages for x18n |