i18n-tiniest
v1.0.0
Published
Tiny, dependency-free i18n helper with dot-notation, lazy loading, interpolation, and plurals.
Downloads
93
Maintainers
Readme
i18n-tiniest
Tiny, dependency-free i18n helper with:
- dot-notation keys (e.g.
label.back) - async language loading (lazy loading)
- interpolation via
{{var}} - i18next-style plurals via
Intl.PluralRules(key_one,key_other, ...)
This package is ESM-only.
Installation
pnpm add i18n-tiniestUsage
import { createI18n } from "i18n-tiniest";
const en = {
label: { back: "Back" },
hello: "Hello {{name}}",
// i18next-style plurals
item_one: "item",
item_other: "items",
itemWithCount_one: "{{count}} item",
itemWithCount_other: "{{count}} items",
itemWithCount_zero: "no items",
} as const;
const i18n = createI18n({
defaultLanguage: "en",
defaultTranslations: en,
languageLoaders: {
de: async () => (await import("./locales/de")).default,
},
});
// translate
console.log(i18n.$t("label.back"));
// interpolation
console.log(i18n.$t("hello", { name: "David" }));
// plurals (cardinal)
console.log(i18n.$t("item", { count: 1 })); // -> "item"
console.log(i18n.$t("item", { count: 2 })); // -> "items"
console.log(i18n.$t("itemWithCount", { count: 0 })); // -> "no items"
console.log(i18n.$t("itemWithCount", { count: 2 })); // -> "2 items"
// or via $tc convenience helper
console.log(i18n.$tc("item", 2));
// switch language (async)
await i18n.setLanguage("de");
console.log(i18n.$t("label.back"));Notes
- Plural selection relies on
Intl.PluralRules.- If you target environments that don’t support it, you must polyfill
Intl.PluralRules.
- If you target environments that don’t support it, you must polyfill
