Licence
Apache-2.0
Version
0.0.2
Deps
0
Size
21 kB
Vulns
0
Weekly
0
@askrjs/i18n
Typed, application-owned internationalization for Askr. The package does not choose locales, parse ICU messages, or install process-global state.
import { createI18n } from "@askrjs/i18n";
const i18n = createI18n("en", {
en: {
welcome: ({ name }: { name: string }) => `Welcome, ${name}`,
total: (value: number) =>
new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(value),
},
fr: {
welcome: ({ name }: { name: string }) => `Bienvenue, ${name}`,
total: (value: number) =>
new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" }).format(value),
},
});
function Welcome() {
return <h1>{i18n.text("welcome", { name: "Ada" })}</h1>;
}
<i18n.Scope locale="fr" dir="ltr">
<Welcome />
</i18n.Scope>;
Applications own locale resolution from URL prefixes, hosts, cookies, or user
profiles. i18n.dehydrate() returns an immutable, versioned snapshot containing
the active locale, direction, and selected catalog identity. Pass that snapshot
back as <i18n.Scope hydration={snapshot}> during hydration.
The first argument names the source locale. Its exact keys and message argument tuples are required from every other locale; missing, extra, or incompatible messages fail during type checking and are also rejected at runtime.
See the runtime contract for isolation and hydration details.