Zum Inhalt springen
Server-Side i18n

Serverseitiges i18n für Hono, Express und Edge

Internationalisieren Sie Ihre API und Ihre serverseitig gerenderten Apps.

Package
lefine
Adapters
Hono · Express
lefine
01Terminal
npx lefine strings pull
02i18n.ts
import 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;}
03app.ts
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')
Not found
01/Overview

Lefine bietet Middleware für Hono, Express, Fastify und Edge-Runtimes mit automatischer Spracherkennung.

02/Setup

In 3 Schritten einrichten

  1. 01

    Installieren Sie das Paket

    Installieren Sie @better-i18n/server. Optional installieren Sie Hono oder Express, je nach Framework.

    terminalbash
    npx lefine strings pull# Writes ./locales/en.json, ./locales/de.json, ... from your published strings.# Hono: npm install hono# Express: npm install express
  2. 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.ts
    import 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;}
  3. 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.

03/Middleware

Framework middleware

Wählen Sie Ihr Server-Framework — die Middleware-API ist bei Hono und den Node.js-Adaptern gleich.

app.ts
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);});
04/Capabilities

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
Get started

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.