콘텐츠로 바로 가기
Hono

Hono 국제화

미들웨어 하나가 요청 로케일을 감지해 모든 라우트 핸들러에 번역기를 전달합니다. 메시지는 CDN에서 오므로 재배포 없이 새 문구를 배포할 수 있습니다.

Setup

Better I18N으로 Hono 설정하기

패키지 설치

Hono와 서버 어댑터를 추가합니다. 그 밖에 필요한 것은 없습니다.

terminal
npm install hono @better-i18n/server

i18n 싱글턴 생성

모듈 스코프에서 한 번만 생성하세요. 메시지 캐시는 모든 요청이 공유합니다.

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

// Module scope, not per request: the TtlCache is shared across requests.
export const i18n = createServerI18n({
  project: 'your-org/your-project',
  defaultLocale: 'en',
});

미들웨어 연결

app.use 이후에는 모든 핸들러가 컨텍스트에서 locale과 t를 읽을 수 있습니다.

src/index.ts
import { Hono } from 'hono';
import { betterI18n } from '@better-i18n/server/hono';
import type { Translator } from '@better-i18n/server';
import { i18n } from './i18n';

const app = new Hono<{
  Variables: { locale: string; t: Translator };
}>();

app.use('*', betterI18n(i18n));

app.get('/users/:id', (c) => {
  const t = c.get('t');
  return c.json({ error: t('errors.notFound') }, 404);
});

export default app;
Capabilities

Hono 팀이 Better I18N을 선택하는 이유

설정은 한 줄: app.use('*', betterI18n(i18n))
웹 표준 기반 — 요청 헤더를 직접 읽으므로 어댑터 계층이 필요 없습니다
Hono가 동작하는 곳이라면 어디든: Cloudflare Workers, Bun, Deno, Node
모듈 스코프 인스턴스가 요청 간에 메시지 캐시를 공유합니다
새 메시지는 약 60초 안에 CDN에서 도착하며 재배포가 필요 없습니다
locale과 t에 대한 타입이 지정된 컨텍스트 변수
Example

현지화된 Hono 라우트

미들웨어가 먼저 실행되므로 핸들러는 필요한 것만 읽습니다.

import { OpenAPIHono } from '@hono/zod-openapi';
import { betterI18n } from '@better-i18n/server/hono';
import type { Translator } from '@better-i18n/server';
import { i18n } from './i18n';

const app = new OpenAPIHono<{
  Variables: { locale: string; t: Translator };
}>();

// Detect locale + inject `locale` and `t` into every request context
app.use('*', betterI18n(i18n));

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;

다른 프레임워크

Get started

Hono API를 모든 언어로 출시하세요

프로젝트를 연결하고 미들웨어를 추가하면 배포 파이프라인을 건드리지 않고 번역할 수 있습니다.