neux - v0.17.3
    Preparing search index...

    Function i18n

    • Creates a translation function.

      The returned function resolves a translation string by dot-separated path (or a flat key) and interpolates %{key}-style placeholders from a data object. When called with a non-string first argument, it delegates to toLocaleString for direct value formatting.

      Language-sensitive formatting:

      Parameters

      • locales: Record<string, Record<string, any>>

        A map of language codes to translation objects.

      • Optionaloptions: { fallback?: string; language?: string }

        Configuration options.

        • Optionalfallback?: string

          Fallback language code when the default is unavailable; defaults to "en".

        • Optionallanguage?: string

          Default language code; defaults to the browser language.

      Returns Translator

      A translation function. Missing keys and fallback misses return the key itself.

      const t = i18n({
      en: {
      say: { hello: "Hello, %{name}!" },
      speed: "Speed: %{value}",
      birthday: "Birthday: %{date}",
      },
      de: {
      say: { hello: "Hallo, %{name}!" },
      speed: "Geschwindigkeit: %{value}",
      birthday: "Geburtstag: %{date}",
      },
      });

      // text with variables
      t("say.hello", { name: "Alice" }); // Hello, Alice!
      // explicitly specified locale
      t("say.hello", { name: "Emma" }, "de"); // Hallo, Emma!

      // Intl.NumberFormat
      t("speed", { value: [15, { style: "unit", unit: "kilometer-per-hour" }] }); // Speed: 15 km/h
      t(1234, { style: "currency", currency: "USD" }); // $1,234.00

      // Intl.DateTimeFormat
      t("birthday", { date: [new Date("2021-01-01"), { dateStyle: "short" }] }); // Birthday: 1/1/21
      t(new Date("2021-01-01"), { dateStyle: "full" }); // Friday, January 1, 2021