3단계로 설정하기
- 01
패키지 설치하기
@better-i18n/server를 설치하세요. 사용하는 프레임워크에 따라 Hono 또는 Express를 선택적으로 설치합니다.
terminalbashnpx lefine strings pull# Writes ./locales/en.json, ./locales/de.json, ... from your published strings.# Hono: npm install hono# Express: npm install express - 02
Create a singleton
createServerI18n()을 모듈 스코프에서 한 번만 인스턴스화하세요. TtlCache는 모든 요청이 공유하므로 요청마다 CDN을 호출하지 않습니다.
i18n.tsimport 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;} - 03
Register middleware
사용하는 프레임워크에 미들웨어를 연결하세요. 미들웨어는 Accept-Language를 읽어 로케일을 확인하고 요청 컨텍스트에 t()를 주입합니다.