Naar inhoud gaan
Server-Side i18n

Server-side i18n voor Hono, Express en edge

Internationaliseer uw API en uw servergerenderde apps. Lefine biedt middleware voor Hono, Express, Fastify en edge-runtimes, met automatische taaldetectie.

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/Setup

Stel in in 3 stappen

  1. 01

    Installeer het pakket

    Installeer @better-i18n/server. Installeer optioneel Hono of Express, afhankelijk van uw 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

    Maak createServerI18n() één keer aan in de module-scope. De TtlCache wordt door alle requests gedeeld, dus geen CDN-aanvraag per 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

    Sluit de middleware voor uw framework aan. De middleware leest Accept-Language, bepaalt de taal en injecteert t() in de requestcontext.

02/Middleware

Framework middleware

Kies uw serverframework — de middleware-API is hetzelfde voor Hono en de Node.js-adapters.

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);});
03/Capabilities

Waarom Lefine op de server gebruiken?

  • Middleware volgens webstandaarden — Hono, Cloudflare Workers, Deno Deploy
  • Node.js-adapter — Express, Fastify en Koa via fromNodeHeaders()
  • Automatische Accept-Language — taaldetectie conform RFC 5646
  • Singleton TtlCache — na het opwarmen geen CDN-aanvragen per request
  • Getypeerde t() — gebouwd op createTranslator van use-intl/core
  • Klaar voor de edge — native ondersteuning voor Cloudflare Workers
Get started

Gratis beginnen.

Localiseer vandaag nog uw API-responses

Lever vertaalde foutmeldingen, notificaties en content vanuit uw backend, met berichten die over verzoeken heen in het geheugen worden gecachet.