Notificationi18n

i18n

Localize notification content with ICU MessageFormat and locale fallback

i18n

@visulima/notification/i18n localizes message content with ICU MessageFormat (plurals, select, number/date) and region → language → fallback resolution driven by the subscriber's locale. It is edge-safe and dependency-light.

It requires the intl-messageformat peer:

npm install @visulima/notification intl-messageformat

Usage

import { createTranslator } from "@visulima/notification/i18n";

const t = createTranslator({
    fallbackLocale: "en",
    messages: {
        en: { likes: "{count, plural, one {# like} other {# likes}}", greeting: "Hi {name}" },
        de_DE: { likes: "{count, plural, one {# Like} other {# Likes}}", greeting: "Hallo {name}" },
    },
});

t.translate("de_DE", "likes", { count: 3 }); // "3 Likes"
t.translate("en", "greeting", { name: "Ada" }); // "Hi Ada"
t.translate("fr", "likes", { count: 1 }); // no fr table → falls back to en → "1 like"

Locale resolution

translate(locale, key, values?) resolves in order: the exact locale, its language subtag (de-DE/de_DEde), then the fallbackLocale (and its language). Table keys may use - or _; the locale passed to Intl is normalized to BCP-47 internally. A missing key returns the key itself, or the result of an onMissing(locale, key) handler.

In templates

Drive translate from the subscriber's locale and feed the result into a layout or channel payload. For a {{t}} helper, register translate with your template engine — e.g. a Handlebars helper:

import Handlebars from "handlebars";

Handlebars.registerHelper("t", (key: string, options) => t.translate(options.data.root.locale, key, options.hash));
// template: "{{t 'greeting' name=user.name}}"

has(locale, key) reports whether a key resolves, and a bad ICU template / missing value throws an actionable NotificationError so failures surface clearly rather than rendering a broken string.

Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues