DocsNext.js

Next.js middleware

Locale routing for the App Router with createGenericTranslationMiddleware, prefix modes and detection order.

On this page
// middleware.ts
import { createGenericTranslationMiddleware } from "@generic-translation/react/next/middleware";

export default createGenericTranslationMiddleware({ locales: ["es", "fr"], defaultLocale: "en" });

export const config = { matcher: ["/((?!api|_next|_generic-translation|.*\\..*).*)"] };

The middleware decides the locale of every request, forwards it to server components in the x-generic-translation-locale request header, stores it in the generic-translation-locale cookie, and (by default) rewrites to an app/[locale]/... segment.

Options

Option Type Default Description
locales string[] required Locales you translate into. The default locale is added.
defaultLocale string required Source locale.
localePrefix "always" | "as-needed" | "never" "as-needed" See below.
localeDetection boolean true Use the cookie and Accept-Language for URLs without a locale. With false, un-prefixed URLs use the default locale.
localeSegment boolean true Your routes live under app/[locale]/. With false the middleware only sets the header and cookie and does not rewrite.
ignore (pathname) => boolean see below Paths to leave alone.

By default the middleware ignores /_next, /_generic-translation, /api, and any path with a file extension.

Prefix modes

Mode Default-locale URL Other locales
"always" /en/about /es/about
"as-needed" /about /es/about
"never" /about /about (locale comes from the cookie or Accept-Language)

Use the same localePrefix in withGenericTranslation (see Next.js setup) so the language selector builds matching URLs.

Resolution order

  1. A locale in the URL path (case-insensitive; /FR/x redirects to /fr/x).
  2. The generic-translation-locale cookie.
  3. Accept-Language, matched with core’s determineLocale.
  4. The default locale.

Details by mode:

  • as-needed: /en/about redirects to /about. An un-prefixed URL whose detected locale is not the default redirects to /{locale}/about.
  • always: un-prefixed URLs redirect to the detected locale.
  • never: a locale in the URL is treated as an explicit choice, saved to the cookie, and removed from the URL by redirect.

Reading the locale

In server code, getLocale() from @generic-translation/react/next returns, in order: the value set with setRequestLocale(), the x-generic-translation-locale header, the generic-translation-locale cookie, the default locale. Reading headers makes a route dynamic. In app/[locale]/ layouts and pages call setRequestLocale(params.locale) first, and pass locale to the provider, so static generation keeps working.

export function generateStaticParams() {
  return ["en", "es", "fr"].map((locale) => ({ locale }));
}

Combining with other middleware

createGenericTranslationMiddleware returns a function (request: NextRequest) => NextResponse. Call it from your own middleware and return its response (or merge cookies and headers from it) when your logic does not handle the request.

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.