Loading
Both take a list, even of one, and return t keyed by name.
const t = await parla.load([checkout, common]); // a route loader, the server, a testconst maybe = parla.loaded([checkout, common]); // t, or undefined if not loaded yet; never downloadsparla.load(list) |
|---|
| Loads each namespace’s chunk for the current locale, in parallel |
| The same list, in any order, returns the same promise |
| Loaded namespaces resolve at once |
| Throws at once for a namespace built without one of the app’s locales |
Throws for two namespaces of one name: t holds one per name |
| A failed download rejects, and is retried after a second |
In React, useParla and useParlaLoaded wrap these: see React.
Without Suspense
Section titled “Without Suspense”loaded(list) returns t, or undefined until the namespaces load for the current locale. Render
from it, and call load when it is undefined:
function render(): void { const t = parla.loaded([checkout]); if (!t) { void parla.load([checkout]).then(render); return; } document.title = t.checkout.title();}t is the proof of loading
Section titled “t is the proof of loading”t has only the namespaces you loaded: no ready flags, no getters that throw.
const t = await parla.load([checkout]);t.checkout.title(); // okt.common.save(); // type error: common was not loadedWhat may not be there is not in t: an element such as flag ? a : b, or an array of unknown length.
A list kept in a constant needs as const.
Translations<typeof ns>
Section titled “Translations<typeof ns>”The type of t for some namespaces, a union for several; a wider t satisfies it.
import type { Translations } from '@parlajs/core';
function total(t: Translations<typeof checkout>, amount: number): string { return t.checkout.total({ amount });}
const t = await parla.load([checkout, common]);total(t, 39.8); // a wider t fits
type Page = Translations<typeof checkout | typeof common>; // t.checkout and t.commonA function generic over its namespace takes Loaded<[N]>, since N may be a union:
async function titled<N extends typeof checkout | typeof common>( ns: N, title: (t: Loaded<[N]>) => string,): Promise<string> { return title(await parla.load([ns]));}
await titled(checkout, (t) => t.checkout.title());Rich text makes it Translations<typeof ns, Node>: see Rich text.
Every message is a function
Section titled “Every message is a function”| Message | Type |
|---|---|
| No params, no markup | Text, that is () => string |
{$name}, no annotation |
(params: { name: string | number }) => string |
{$count :number} |
(params: { count: number }) => string |
{$when :date} |
(params: { when: Date | number }) => string |
| With markup | Returns the instance’s node: string in core |
A :string select |
Params per branch, see Selects |
Groups nest, up to 100 levels: "cart": { "items": "…" } is t.checkout.cart.items(…).