콘텐츠로 바로 가기
Server-Side i18n

Hono, Express, 엣지를 위한 서버 사이드 i18n

API와 서버에서 렌더링되는 앱을 국제화하세요. Lefine은 자동 로케일 감지와 함께 Hono, Express, Fastify, 엣지 런타임용 미들웨어를 제공합니다.

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

3단계로 설정하기

  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을 사용하는 이유

  • 웹 표준 미들웨어 — Hono, Cloudflare Workers, Deno Deploy
  • Node.js 어댑터 — fromNodeHeaders()를 통한 Express, Fastify, Koa 지원
  • 자동 Accept-Language — RFC 5646을 준수하는 로케일 감지
  • 싱글턴 TtlCache — 워밍업 이후에는 요청마다 CDN을 호출하지 않습니다
  • 타입이 지정된 t() — use-intl/core의 createTranslator 기반
  • 엣지 대응 — Cloudflare Workers 기본 지원
Get started

무료로 시작하세요.

지금 API 응답을 현지화하세요

번역된 오류 메시지, 알림, 콘텐츠를 백엔드에서 제공하세요. 메시지는 요청 간에 메모리에 캐시됩니다.