Skip to content

[i18n] Prefix for default locale #18419

Closed Answered by leerob
omgMath asked this question in Help
Oct 28, 2020 · 50 comments · 134 replies
Discussion options

You must be logged in to vote

Credit to @pecoram and @dgoerdes, we now have a solution for this in Next.js 12 with Middleware!

// middleware.ts
import { NextRequest, NextResponse } from 'next/server';

const PUBLIC_FILE = /\.(.*)$/;

export async function middleware(req: NextRequest) {
  if (
    req.nextUrl.pathname.startsWith('/_next') ||
    req.nextUrl.pathname.includes('/api/') ||
    PUBLIC_FILE.test(req.nextUrl.pathname)
  ) {
    return;
  }

  if (req.nextUrl.locale === 'default') {
    return NextResponse.redirect(
      new URL(`/en${req.nextUrl.pathname}`, req.url)
    );
  }
}
// next.config.js

module.exports = {
    i18n: {
        locales: ['default', 'en', 'de', 'fr'],
        defaultLocale: 'default',

Replies: 50 comments 134 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@CaptainN
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@omgMath
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
26 replies
@vforvalerio87
Comment options

@rlenoir-codepoint
Comment options

@martinratinaud
Comment options

@svassaux
Comment options

@dnbastos
Comment options

Comment options

You must be logged in to vote
8 replies
@joan-saum
Comment options

@angeloreale
Comment options

@martinratinaud
Comment options

@angeloreale
Comment options

@angeloreale
Comment options

Comment options

You must be logged in to vote
1 reply
@elias-thok
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@igorzinar
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@magsout
Comment options

@leerob
Comment options

@leerob
Comment options

@magsout
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@rebzden-baracoda
Comment options

@mderrick
Comment options

@gregghawes
Comment options

@mderrick
Comment options

Comment options

You must be logged in to vote
5 replies
@anders-photowall
Comment options

@anders-photowall
Comment options

@actraiser
Comment options

@dennisdittrich
Comment options

@anders-photowall
Comment options

Comment options

You must be logged in to vote
4 replies
@joggienl
Comment options