Namespaces
Each namespace in parla.config.ts compiles to <outDir>/namespaces/<name>.js. Import it and pass
it:
import { checkout } from './parla/namespaces/checkout.js';import { common } from './parla/namespaces/common.js';
const t = await parla.load([checkout, common]);t.checkout.title();t.common.save();It is { name, load }, one chunk loader per locale; its .d.ts types every message.
| Because a namespace is an import | |
|---|---|
| The bundler ships what you import | Nothing lists every namespace, so none ships by accident |
| The types follow | tsc reads only the .d.ts of imported namespaces |
| Tooling follows | Tools such as x18n read which namespaces a file uses |
| A name does not type-check | parla.load(['checkout']) is an error |
Import each from its own module, not a barrel, so the bundler splits exactly those chunks.
The name is the module, the export and the key in t. Make it an identifier, and map a file name
that differs:
namespaces: { scopeAudit: { format: 'icu', path: 'locales/{locale}/scope_audit.json' } },- A name that is no identifier (
foo-bar) exports asfooBarwhiletkeepsfoo-bar; the build warns. - Two names that differ only in case, which some disks write to one file, fail the build; so do two locales.
- Alias a clash with a local name as
<name>Namespace:import { user as userNamespace }. - Rename one that clashes often rather than prefixing them all.
By name, from data
Section titled “By name, from data”Keep a map:
const byName = { checkout, common };await parla.load([byName[route.namespace]]);A missed lookup passes undefined, and load throws a TypeError.
Keys from data
Section titled “Keys from data”A group is an object of functions, so a key typed as one of its own needs nothing:
declare const status: 'open' | 'closed';t.orders.status[status]();For any string, narrow it first:
function hasKey<T extends object>(group: T, key: string): key is string & keyof T { return Object.hasOwn(group, key);}
const { status } = t.orders;const label = hasKey(status, code) ? status[code]() : code;Keys are strings: pass a number as String(n).
The call needs the params of every message in the group. For a path, cart.items, narrow level by level:
TypeScript cannot check the params of a message it does not know. x18n counts the whole group
as used.
All of them, in a test
Section titled “All of them, in a test”Glob them with Vite:
const all = Object.values( import.meta.glob<Record<string, Namespace>>('./parla/namespaces/*.js', { eager: true }),).flatMap((mod) => Object.values(mod));