import { defineConfig } from "sonamu";export default defineConfig({ // ... other settings i18n: { defaultLocale: "ko", // Default language supportedLocales: ["ko", "en"], // List of supported languages },});
Recommended key naming pattern: Using namespace patterns like "domain.item" helps organize
keys systematically and makes IDE autocomplete more convenient. - "common.*" - Common UI -
"error.*" - Error messages - "validation.*" - Validation messages - "entity.*" - Entity
related
To set the locale per request, configure the Context in middleware:
// api/src/middlewares/locale.tsimport { Sonamu } from "sonamu";export async function localeMiddleware(request: FastifyRequest) { // Extract locale from Accept-Language header or query parameter const locale = request.headers["accept-language"]?.split(",")[0]?.split("-")[0] || request.query.locale || "ko"; // Set locale in Context const ctx = Sonamu.getContext(); ctx.locale = locale;}
When the client calls setLocale(), the axios interceptor automatically sends the
Accept-Language header, and the serverβs locale middleware reads it so that SD() calls return
translations in the correct language. Client-server locale synchronization works without any
additional configuration.