just-t
v0.2.0
Published
The smallest practical i18n library: JSON in → string out.
Downloads
64
Maintainers
Readme
just-t
The smallest practical i18n library: JSON in → string out.
Features
- Zero config:
createJustT({ locale, messages }) - Interpolation:
"Hello {name}" - Optional, super simple plurals:
one/otherviacount === 1 - Fail loudly: in
devit throws on missing keys/params, inprodit returns the key - Type Safety:
- Keys are derived from
messages - Params are derived from
{placeholders}(withas const; for plurals includescount: number)
- Keys are derived from
Install
pnpm add just-tUsage
import { createJustT } from "just-t";
const justT = createJustT({
locale: "en",
fallbackLocale: "de",
messages: {
en: {
welcome: "Hello",
hello_user: "Hello {name}",
items: { one: "{count} item", other: "{count} items" }
},
de: {
welcome: "Hallo",
hello_user: "Hallo {name}",
items: { one: "{count} Ding", other: "{count} Dinge" }
}
} as const
});
justT.t("welcome");
justT.t("hello_user", { name: "Max" });
justT.t("items", { count: 3 });Messages from JSON
You can keep messages in one or multiple JSON files and load them into createJustT.
Browser (or any runtime): JSON string/object
import { createJustT } from "just-t";
const i18n = createJustT({
locale: "en",
json: `{
"en": { "welcome": "Hello" },
"de": { "welcome": "Hallo" }
}`,
});If your JSON is a single-locale message tree, provide jsonLocale:
const i18n = createJustT({
locale: "en",
jsonLocale: "en",
json: { welcome: "Hello" }
});Node.js: file/folder
// Node only
const i18n = await createJustT({
locale: "en",
folder: "./locales" // en.json, de.json, ...
});API (Exports)
import {
createJustT,
type JustT,
type CreateJustTOptions,
type MessagesByLocale,
type MessageValue,
type PluralMessage,
type Locale
} from "just-t";Dev / Build
pnpm install
pnpm test
pnpm test:watch
pnpm test:types
pnpm build
pnpm typecheckNote: pnpm build produces a bundled & minified dist/index.js via Rolldown and dist/index.d.ts via tsc.
