Zum Inhalt springen
Server-Side i18n

Server-Side i18n for Hono, Express & Edge

Internationalize your API and server-rendered apps. Better i18n provides middleware for Hono, Express, Fastify, and edge runtimes with automatic locale detection.

Set up in 3 steps

1

Install the package

Install @better-i18n/server. Optionally install Hono or Express depending on your framework.

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

Create a singleton

Instantiate createServerI18n() once at module scope. The TtlCache is shared across all requests — no CDN fetch per request.

i18n.ts
import { createServerI18n } from '@better-i18n/server';

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

Register middleware

Wire up the middleware for your framework. The middleware reads Accept-Language, resolves the locale, and injects t() into the request context.

Framework middleware

Choose your server framework — the middleware API is consistent across Hono and Node.js adapters.

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);
});

Why use Better i18n on the server?

Web Standards middleware — Hono, Cloudflare Workers, Deno Deploy
Node.js adaptor — Express, Fastify, Koa via fromNodeHeaders()
Auto Accept-Language — RFC 5646 compliant locale detection
Singleton TtlCache — zero CDN fetches per request after warm-up
Type-safe t() — full TypeScript inference for translation keys
Edge-ready — native Cloudflare Workers support

Localize your API responses today

Serve translated error messages, notifications, and content from your backend with zero overhead.