Skip to content
Server-Side i18n

Server-Side i18n for Hono, Express & Edge

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

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

Set up in 3 steps

  1. 01

    Install the package

    Install @better-i18n/server. Optionally install Hono or Express depending on your 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

    Instantiate createServerI18n() once at module scope. The TtlCache is shared across all requests — no CDN fetch 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

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

02/Middleware

Framework middleware

Choose your server framework — the middleware API is consistent across Hono and 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

Why use Lefine 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
  • Typed t() — built on use-intl/core's createTranslator
  • Edge-ready — native Cloudflare Workers support
Get started

Start free.

Localize your API responses today

Serve translated error messages, notifications, and content from your backend, with messages cached in memory across requests.