Dhrub Kumar Sharma
Dhrub Kumar Sharma

Reputation: 21

How to apply locale routing only for specific page while using next-i18next translation

I have 4 pages in nextjs project as index, service, contact-us, disclaimer. So I want next-i18next translation system only in index and service not in contact-us and disclaimer page. I made this translation work but still i found locale routing as localhost/fr/contact-us and localhost/fr/disclaimer.

I want to have generate locale translation system only for index and service and ignore remaing pages as it is.i dont want to generate locale for this two pages localhost/fr/contact-us and localhost/fr/disclaimer.

Upvotes: 2

Views: 332

Answers (1)

sm7
sm7

Reputation: 679

You can setup temporary redirect (or anything else that suits your need) in middleware.ts for the pages for which you do not want to have localization. Something like the following:

export const middleware = (request: NextRequest): NextResponse => {
  if (request.nextUrl.pathname !== '/') {
    // for non-index paths, omit the locale
    // and temp redirect to the default url for the pages
    return NextResponse.redirect(
      new URL(
        `${request.nextUrl.pathname}${request.nextUrl.search}`,
        request.nextUrl.origin
      )
    );
  }

  return NextResponse.next();
};

Upvotes: 0

Related Questions