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.
Set up in 3 steps
Install the package
Installa @better-i18n/server. Facoltativamente installa Hono o Express, a seconda del framework.
npm install @better-i18n/server
# Hono: npm install hono
# Express: npm install expressCreate 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.
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.
Framework middleware
Scegli il tuo framework server — l'API del middleware è la stessa su Hono e sugli adattatori Node.js.
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);
});Perché usare Better I18N sul server?
Other frameworks
Get started
Localizza subito le risposte della tua API
Servi messaggi di errore, notifiche e contenuti tradotti dal tuo backend, senza overhead.