Set up in 3 steps
- 01
Install the package
Install @better-i18n/server. Optionally install Hono or Express depending on your framework.
terminalbashnpx lefine strings pull# Writes ./locales/en.json, ./locales/de.json, ... from your published strings.# Hono: npm install hono# Express: npm install express - 02
Create a singleton
Instantiate createServerI18n() once at module scope. The TtlCache is shared across all requests — no CDN fetch per request.
i18n.tsimport en from './locales/en.json';import de from './locales/de.json';const messages = { en, de } as const;export const defaultLocale = 'en';// Files hold { namespace: { key: text } }, so 'errors.notFound' is a path.export function translator(locale: string) { const dict = messages[locale as keyof typeof messages] ?? messages[defaultLocale]; return (key: string): string => key.split('.').reduce<any>((node, part) => node?.[part], dict) ?? key;} - 03
Register middleware
Wire up the middleware for your framework. The middleware reads Accept-Language, resolves the locale, and injects t() into the request context.