Skip to main content

Function: i18nMiddleware()

i18nMiddleware(__namedParameters): Middleware

Defined in: i18n.ts:58

Negotiates the request locale and renders LocalizedErrors at the HTTP boundary.

  • Sets ctx.state.locale and ctx.state.i18n for handlers that render message references themselves. The locale comes from getUserLocale (a stored preference) first, then Accept-Language, then the catalog's fallback.
  • A thrown LocalizedError that is client-facing (a 4xx status, or the expected marker) becomes { error: { code, message } } with that status (400 by default).
  • Any other LocalizedError has its message rendered 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​

ParameterType
__namedParameters{ catalog: Pick<Catalog, "getI18n">; getUserLocale?: (ctx) => Requested | Promise<Requested>; }
__namedParameters.catalogPick<Catalog, "getI18n">
__namedParameters.getUserLocale?(ctx) => Requested | Promise<Requested>

Returns​

Middleware