Runtime
Catalogues, views and stores, locales, messages, loading, matching, switching
The runtime is three objects with one job each:
- A catalogue holds your locales and where each one's messages come from. It never formats and has no current locale.
- A view is one locale bound to its messages. You call it to format, it is immutable, and it is what application code holds.
- A store holds one view at a time and says when that changed. It is what switching locale in a browser looks like.
Framework integrations wrap these (SayProvider in React, SayPlugin in Carbon), but the API underneath is the same. Every method is in the API reference.
Creating a catalogue
import { createCatalogue } from 'saykit';
import en from './locales/en.js';
const catalogue = createCatalogue({
en, // bundled
fr: () => import('./locales/fr.js'), // loaded on demand
ja: () => import('./locales/ja.js'),
});The keys are your locales, and the first is the fallback. Each value is a compiled locale module, imported directly or behind a thunk that is left alone until the locale is loaded. See dynamic loading.
Getting a view
const say = catalogue.locale('fr');
say.locale; // 'fr'
say`Hello!`; // 'Bonjour !'Views are memoised, so catalogue.locale('fr') returns the same view every time. It is synchronous, so for a lazy locale that is not loaded yet, use load():
const say = await catalogue.load('fr');load() is synchronous when the locale is already loaded, shares one call between overlapping loads, and retries a thunk that rejected. A locale's messages are filled once and never replaced, so a view is safe to hold for as long as you like.
Matching guesses
match() maps guesses (browser languages, an Accept-Language header, a cookie, a URL segment) to a locale the catalogue has: exact match first, then language and script (zh-HK → zh-TW), then language (fr-CA → fr), then the first locale. Case doesn't matter.
catalogue.match(navigator.languages); // 'fr' for ['fr-CA', 'en-US']
catalogue.match(cookieLocale, headerLocales); // nullish guesses are skippedSee locale detection.
Switching locale
A view cannot switch, so switching is a store:
import { createStore } from 'saykit';
const store = createStore(catalogue, 'en');
store.subscribe((say) => render(say));
await store.set('fr');
store.say`Hello, ${name}!`; // formats in frset() loads the locale if needed. It does nothing for the current locale, keeps the current view if the load fails, and lets the last switch win when two overlap. Subscribers are told only about successful switches.
Read store.say each time you format. A const say = store.say held across a switch is the old
view.
A store mutates, so it belongs in a browser: one user, one locale at a time. On a server, look a view up per request instead:
export function sayFor(headers: Headers) {
const header = headers.get('accept-language') ?? '';
const guesses = header.split(',').map((tag) => tag.split(';')[0]!.trim());
return catalogue.load(catalogue.match(guesses));
}Catalogues and views are safe to share between requests: neither has anything to leak. For code that cannot be handed a view, createScope gives a say that resolves to the view established around each call.
Formatting
Macros compile to say.call(descriptor):
say.call({ id: 'abc123', _name: 'Ada' }); // → "Hello, Ada!"The message is already compiled to data at build time, so nothing is parsed at runtime. A view turns each message into a closure the first time it is called and caches it, building its Intl formatters once. A value that cannot be formatted, such as an invalid date, is written as given with a warning rather than throwing.
Views without a catalogue
createView(locale, messages) builds a view directly, for code that only ever has one locale. It is what SayProvider uses when a server hands it a locale and messages. With a catalogue, prefer catalogue.locale(), which memoises.
Next
- Locale detection: matching browser, header, cookie, and URL inputs
- Dynamic loading: lazy locale loading
- React integration:
SayProvider,useSay,withSay