Nextjs Internationalized route, double parameter

Viewed 139

I would like to have my website url like that /[cc]/[lang]. The first part would be the country, the second one the current language. The main problem is that I have no idea how to create the second parameter

  i18n: {
    locales: ["en", "de"],
    defaultLocale: "en",
  },
1 Answers

Seems like a similar issue to: https://github.com/vercel/next.js/discussions/32295 try using the middleware provided there:

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

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

export function middleware(req: NextRequest) {
  const nextUrl =
    req.url.split('/')[1] === 'next' || req.url.split('/')[1] === '_next';
  if (!PUBLIC_FILE.test(req.nextUrl.pathname) && !nextUrl) {
    const [_, language, country] = req.url.split('/');
    const newUrl = req.nextUrl;
    newUrl.pathname = `/${language.toUpperCase()}/${country}/<your url>`;
    newUrl.locale = `${language}-${country.toUpperCase()}`;
    return NextResponse.rewrite(newUrl);
  }
}
Related