saykit
v0.11.0
Published
Type-safe i18n library with compile-time macro transforms
Maintainers
Readme
saykit
Type-safe i18n library with compile-time macro transforms.
The core runtime for SayKit. Exports createCatalogue, which holds your locales and where each one's messages come from, createView, which binds one locale and formats messages using ICU MessageFormat, and createStore, which holds the current view and swaps it when you switch locale.
You author messages with the say`...` tagged template (and say.plural, say.ordinal, say.select), and format numbers, dates and times for the locale with say.number, say.date and say.time; a SayKit build-tool plugin rewrites them at build time into small runtime calls.
Install
pnpm add saykitYou will normally also want @saykit/config and a build-tool plugin (unplugin-saykit or babel-plugin-saykit).
Usage
import { createCatalogue } from 'saykit';
import en from './locales/en.po';
import fr from './locales/fr.po';
const catalogue = createCatalogue({ en, fr });
const say = catalogue.locale('en');
say`Hello, ${name}!`;
say.plural(count, { one: '1 item', other: `${count} items` });
say`Total: ${say.number(total, { style: '::currency/EUR' })}`;
say.date(new Date(), { style: 'long' });In a browser, where the locale can change, hold a store instead of a view:
import { createStore } from 'saykit';
const store = createStore(catalogue, 'en');
store.subscribe((say) => render(say));
await store.set('fr');
store.say`Hello, ${name}!`;Documentation
Full guide at saykit.js.org.
