A map of language codes to translation objects.
Optionaloptions: { fallback?: string; language?: string }
Configuration options.
Optionalfallback?: stringFallback language code when the default is unavailable; defaults to "en".
Optionallanguage?: stringDefault language code; defaults to the browser language.
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
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 totoLocaleStringfor direct value formatting.Language-sensitive formatting: