Vai al contenuto
Server-Side i18n

i18n lato server per Hono, Express e edge

Internazionalizza la tua API e le app renderizzate sul server. Lefine offre middleware per Hono, Express, Fastify e runtime edge, con rilevamento automatico della lingua.

Package
lefine
Adapters
Hono · Express
lefine
01Terminale
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

Configura in 3 passaggi

  1. 01

    Installa il pacchetto

    Installa @better-i18n/server. Facoltativamente installa Hono o Express, a seconda del 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

    Istanzia createServerI18n() una sola volta nello scope del modulo. Il TtlCache è condiviso tra tutte le richieste, nessuna chiamata al CDN per ogni 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

    Collega il middleware del tuo framework. Il middleware legge Accept-Language, risolve la lingua e inietta t() nel contesto della richiesta.

02/Middleware

Framework middleware

Scegli il tuo framework server — l'API del middleware è la stessa su Hono e sugli adattatori Node.js.

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

Perché usare Lefine sul server?

  • Middleware conforme agli standard web — Hono, Cloudflare Workers, Deno Deploy
  • Adattatore per Node.js — Express, Fastify e Koa tramite fromNodeHeaders()
  • Accept-Language automatico — rilevamento della lingua conforme a RFC 5646
  • TtlCache singleton — zero richieste al CDN per ogni request dopo il warm-up
  • t() tipizzato — basato su createTranslator di use-intl/core
  • Pronto per l'edge — supporto nativo per Cloudflare Workers
Get started

Inizia gratis.

Localizza subito le risposte della tua API

Servi messaggi di errore, notifiche e contenuti tradotti dal tuo backend, con i messaggi memorizzati in cache tra le richieste.