Vai al contenuto
Server-Side i18n

i18n lato server per Hono, Express e edge

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

Setup

Set up in 3 steps

Install the package

Installa @better-i18n/server. Facoltativamente installa Hono o Express, a seconda del framework.

terminal
npm install @better-i18n/server
# Hono: npm install hono
# Express: npm install express

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 { createServerI18n } from '@better-i18n/server';

export const i18n = createServerI18n({
  projectId: 'your-org/your-api',
  defaultLocale: 'en',
});

Register middleware

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

Example

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 { betterI18n } from '@better-i18n/server/hono';
import { i18n } from './i18n';

type Variables = { locale: string; t: Translator };
const app = new Hono<{ Variables: Variables }>();

app.use('*', betterI18n(i18n));

app.get('/api/users/:id', (c) => {
  const t = c.get('t');
  return c.json({ error: t('errors.notFound') }, 404);
});
Capabilities

Perché usare Better I18N 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() type-safe — inferenza TypeScript completa per le chiavi di traduzione
Pronto per l'edge — supporto nativo per Cloudflare Workers

Other frameworks

Get started

Localizza subito le risposte della tua API

Servi messaggi di errore, notifiche e contenuti tradotti dal tuo backend, senza overhead.