Next.js Discord

Discord Forum

pathname localization

Unanswered
American Wirehair posted this in #help-forum
Open in Discord
Original message was deleted.

2 Replies

Avatar
You can have some redirection logic to your default prefix in your middleware?
// middleware.js

import { NextResponse } from 'next/server';

export function middleware(req) {
  const { nextUrl, cookies } = req;
  const locale = nextUrl.locale;
  const defaultLocale = 'en';

  // If no locale is present in the URL, check the cookie
  if (!locale) {
    const cookieLocale = cookies.get('NEXT_LOCALE')?.value || defaultLocale;
    return NextResponse.redirect(new URL(`/${cookieLocale}${nextUrl.pathname}`, req.url));
  }

  // Optionally, set the cookie based on the URL locale
  cookies.set('NEXT_LOCALE', locale, { path: '/' });

  return NextResponse.next();
}
Avatar
American Wirehair
i guess i explanied the situation incompletely. currently i have translation to several languages in my app, but some parts like meta tags and descriptions are only english without translation . I want to add another language to that static part, which will not affect the language selection in my app. I want to also translate the urls for that language and generate new urls