Lefine bietet Middleware für Hono, Express, Fastify und Edge-Runtimes mit automatischer Spracherkennung.
Serverseitiges i18n für Hono, Express und Edge
Internationalisieren Sie Ihre API und Ihre serverseitig gerenderten Apps.
- Package
- lefine
- Adapters
- Hono · Express
npx lefine strings pullimport 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;}import { Hono } from 'hono';import { translator } from './i18n';type Variables = { locale: string; t: (key: string) => string };const app = new Hono<{ Variables: Variables }>();app.use('*', async (c, next) => { const locale = c.req.header('accept-language')?.slice(0, 2) ?? 'en'; c.set('locale', locale); c.set('t', translator(locale)); await next();});app.get('/api/users/:id', (c) => { const t = c.get('t'); return c.json({ error: t('errors.notFound') }, 404);});t('errors.notFound')In 3 Schritten einrichten
- 01
Installieren Sie das Paket
Installieren Sie @better-i18n/server. Optional installieren Sie Hono oder Express, je nach 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
Instanziieren Sie createServerI18n() einmal im Modul-Scope. Der TtlCache wird von allen Requests gemeinsam genutzt, kein CDN-Abruf pro 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
Binden Sie die Middleware für Ihr Framework ein. Die Middleware liest Accept-Language, löst die Sprache auf und injiziert t() in den Request-Kontext.
Framework middleware
Wählen Sie Ihr Server-Framework — die Middleware-API ist bei Hono und den Node.js-Adaptern gleich.
import { Hono } from 'hono';import { translator } from './i18n';type Variables = { locale: string; t: (key: string) => string };const app = new Hono<{ Variables: Variables }>();app.use('*', async (c, next) => { const locale = c.req.header('accept-language')?.slice(0, 2) ?? 'en'; c.set('locale', locale); c.set('t', translator(locale)); await next();});app.get('/api/users/:id', (c) => { const t = c.get('t'); return c.json({ error: t('errors.notFound') }, 404);});Warum Lefine auf dem Server einsetzen?
- Middleware nach Web-Standards — Hono, Cloudflare Workers, Deno Deploy
- Node.js-Adapter — Express, Fastify und Koa über fromNodeHeaders()
- Automatisches Accept-Language — Spracherkennung konform zu RFC 5646
- Singleton-TtlCache — nach dem Warm-up keine CDN-Abrufe pro Request
- Typisiertes t() — basiert auf createTranslator aus use-intl/core
- Edge-ready — native Unterstützung für Cloudflare Workers
Kostenlos starten.
Lokalisieren Sie Ihre API-Antworten noch heute
Liefern Sie übersetzte Fehlermeldungen, Benachrichtigungen und Inhalte aus Ihrem Backend, mit Messages, die über Anfragen hinweg im Arbeitsspeicher zwischengespeichert werden.