Function: i18nMiddleware()
i18nMiddleware(
__namedParameters):Middleware
Defined in: i18n.ts:58
Negotiates the request locale and renders LocalizedErrors at the HTTP
boundary.
- Sets
ctx.state.localeandctx.state.i18nfor handlers that render message references themselves. The locale comes fromgetUserLocale(a stored preference) first, thenAccept-Language, then the catalog's fallback. - A thrown
LocalizedErrorthat is client-facing (a 4xxstatus, or theexpectedmarker) becomes{ error: { code, message } }with that status (400 by default). - Any other
LocalizedErrorhas itsmessagerendered in place and is rethrown, so the app's own error handling (and reporting) still sees it. Every non-localized error is rethrown untouched.
Mount it before the routes and after any catch-all error middleware.
Parameters
| Parameter | Type |
|---|---|
__namedParameters | { catalog: Pick<Catalog, "getI18n">; getUserLocale?: (ctx) => Requested | Promise<Requested>; } |
__namedParameters.catalog | Pick<Catalog, "getI18n"> |
__namedParameters.getUserLocale? | (ctx) => Requested | Promise<Requested> |
Returns
Middleware