Next.js Discord

Discord Forum

Internationalization always returning 404

Unanswered
Mossyrose gall wasp posted this in #help-forum
Open in Discord
Mossyrose gall waspOP
Hi

I've followed the docs at https://nextjs.org/docs/app/building-your-application/routing/internationalization with no luck and several other articles. Following the code in the Next.js docs gives me a redirect issue, following another post I keep getting 404.

Here is how my middleware is currently looking:

import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

import i18n from './i18n.json';

export function middleware(request: NextRequest) {
    const { pathname } = request.nextUrl;

    const pathnameHasLocale = i18n.locales.some(
        (locale) =>
            pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
    );

    if (pathnameHasLocale) return;

    return NextResponse.rewrite(
        new URL(`/${i18n.defaultLocale}${pathname}`, request.url),
    );
}

export const config = {
    matcher: ['/((?!_next).*)'],
};

I've also moved all my files in app to app/[lang] as per the docs and other posts.

0 Replies