Skip to content
Hono

Hono i18n

One middleware detects the request locale and hands every route handler a translator. Messages come from the CDN, so new copy ships without a redeploy.

Package
lefine/strings
Runtime
Hono · edge or Node
lefine
01Terminal
npm install hono lefine && npx lefine strings pull -o locales
02src/i18n.ts
import { createTranslator } from 'lefine/strings';import en from '../locales/en.json';import de from '../locales/de.json';// Module scope, not per request: the files are read once.export const files = { en, de } as const;export type Locale = keyof typeof files;export const defaultLocale: Locale = 'en';export const translatorFor = (locale: Locale) =>  createTranslator({ locale, messages: files[locale] });
03src/index.ts
import { Hono } from 'hono';import { files, defaultLocale, translatorFor, type Locale } from './i18n';const app = new Hono<{  Variables: { locale: string; t: ReturnType<typeof translatorFor> };}>();// Pick the locale and set `locale` and `t` on every request contextapp.use('*', async (c, next) => {  const asked = c.req.header('accept-language')?.slice(0, 2) ?? '';  const locale = (asked in files ? asked : defaultLocale) as Locale;  c.set('locale', locale);  c.set('t', translatorFor(locale));  await next();});app.get('/greeting', (c) => {  const t = c.get('t');  const locale = c.get('locale');  return c.json({    locale,    message: t('greeting.hello'),  });});export default app;
t('greeting.hello')
Hello

A localized Hono route

The middleware runs first, so the handler only reads what it needs.

01/Setup

Set up Hono with Lefine

  1. 01

    Install the packages

    Add Hono and the server adapter. Nothing else is required.

    terminalbash
    npm install hono lefine && npx lefine strings pull -o locales
  2. 02

    Create the i18n singleton

    Build it once at module scope — the message cache is shared across every request.

    src/i18n.ts
    import { createTranslator } from 'lefine/strings';import en from '../locales/en.json';import de from '../locales/de.json';// Module scope, not per request: the files are read once.export const files = { en, de } as const;export type Locale = keyof typeof files;export const defaultLocale: Locale = 'en';export const translatorFor = (locale: Locale) =>  createTranslator({ locale, messages: files[locale] });
  3. 03

    Mount the middleware

    After app.use, every handler can read locale and t from the context.

    src/index.ts
    import { Hono } from 'hono';import { files, defaultLocale, translatorFor, type Locale } from './i18n';const app = new Hono<{  Variables: { locale: string; t: ReturnType<typeof translatorFor> };}>();app.use('*', async (c, next) => {  const asked = c.req.header('accept-language')?.slice(0, 2) ?? '';  const locale = (asked in files ? asked : defaultLocale) as Locale;  c.set('locale', locale);  c.set('t', translatorFor(locale));  await next();});app.get('/users/:id', (c) => {  const t = c.get('t');  return c.json({ error: t('errors.notFound') }, 404);});export default app;
02/Capabilities

Why Hono teams choose Lefine

  • One line of setup: app.use('*', betterI18n(i18n))
  • Built on Web Standards — request headers are read directly, with no adapter layer
  • Runs anywhere Hono runs: Cloudflare Workers, Bun, Deno and Node
  • A module-scope instance shares its message cache across requests
  • New messages arrive from the CDN within minutes, with no redeploy
  • Typed context variables for locale and t
Get started

Start free.

Ship your Hono API in every language

Connect a project, add the middleware, and translate without touching the deploy pipeline.