Semola

i18n

Nested translation keys with typed placeholders

Load locale dictionaries, switch language, and interpolate parameters.

import { I18n } from "semola/i18n";

Setup

const i18n = new I18n({
  defaultLocale: "en",
  locales: {
    en: {
      greeting: "Hello",
      welcome: "Welcome, {name:string}!",
      messages: {
        unread: "You have {count:number} unread messages",
      },
    },
    es: {
      greeting: "Hola",
      welcome: "Bienvenido, {name:string}!",
    },
  },
});

i18n.setLocale("es");
i18n.translate("greeting"); // "Hola"
i18n.translate("welcome", { name: "Ada" }); // "Bienvenido, Ada!"

Nested keys use dots: "messages.unread". Missing keys fall back to the default locale, then to the key string itself.

Placeholder syntax

In message strings, declare the type next to the name:

{name:string}  {count:number}  {active:boolean}

Pass matching values as the second argument to translate. getLocale() returns the active locale.

On this page