跳转至主要内容
Server-Side i18n

面向 Hono、Express 和 edge 的服务端 i18n

为你的 API 和服务端渲染应用做国际化。Lefine 为 Hono、Express、Fastify 和 edge 运行时提供中间件,并自动检测语言。

Package
lefine
Adapters
Hono · Express
lefine
01终端
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

三步设置完成

  1. 01

    安装软件包

    安装 @better-i18n/server。可根据所用框架选择性安装 Hono 或 Express。

    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

    在模块作用域中只实例化 createServerI18n() 一次。TtlCache 在所有请求之间共享,不会每个请求都访问 CDN。

    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

    为你的框架接入中间件。中间件会读取 Accept-Language、解析语言,并把 t() 注入请求上下文。

02/Middleware

Framework middleware

选择你的服务端框架 — 中间件 API 在 Hono 和 Node.js 适配器上保持一致。

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

为什么在服务端使用 Lefine?

  • 遵循 Web 标准的中间件 — Hono、Cloudflare Workers、Deno Deploy
  • Node.js 适配器 — 通过 fromNodeHeaders() 支持 Express、Fastify 和 Koa
  • 自动 Accept-Language — 符合 RFC 5646 的语言检测
  • 单例 TtlCache — 预热之后每个请求都无需再访问 CDN
  • 类型化的 t() — 基于 use-intl/core 的 createTranslator 构建
  • 面向 edge — 原生支持 Cloudflare Workers
Get started

免费开始。

立即本地化你的 API 响应

从后端提供翻译后的错误信息、通知和内容,消息会在多个请求之间缓存在内存中。