A suite of packages to ease your translation needs.
@onigoetz/messageformat
a MessageFormat parsing and rendering library@onigoetz/make-plural
a lighter fork of make-plural
meant for browser usage@onigoetz/intl-formatters
default formatters if you don’t already have formatters for dates and numbers, uses the standard Intl
APIimport { parse, createRenderer } from "@onigoetz/messageformat";
import { dateFormatter, numberFormatter, pluralGenerator } from "@onigoetz/intl-formatters";
// Parse the MessageFormat to a renderable format
const parsed = parse("{test, plural, offset:3 one{one test} other {# test} }");
// Create a localized renderer
const render = createRenderer(
"en",
(locale: T, type) => pluralGenerator(locale, { type }),
(locale: T, options, value: number) => numberFormatter(locale, options)(value),
(locale: T, options, value: Date) => dateFormatter(locale, options)(value)
);
render(parsed, { test: 4 }); // => "one test"
render(parsed, { test: 7 }); // => "4 test"
This library is meant for applications starting with medium scale, where you might have multiple libraries and frameworks inside. Since these libraries don’t make any assumption about your stack, you can integrate them in any kind of application.
Most importantly, if you have an environment where pre-compiling translations isn’t possible, for example because your translation build process is separate from your app build process or you have a modular application / microfrontend.
This library is very interesting as a lightweight runtime because of its small footprint and performant parsing.
This suite of packages certainly wouldn’t exist without the previous work in the field.
This package forked make-plural
at version 4 to make it smaller.
Took inspiration for the MessageFormat parser from @ffz/icu-msgparser
for its small size and @phensley/messageformat
for its parsing speed.