Skip to content

Commit

Permalink
feat(next-international): remove locale for useChangeLocale with re…
Browse files Browse the repository at this point in the history
…write strate (QuiiBz#137)
  • Loading branch information
QuiiBz authored Aug 23, 2023
1 parent d12b3cf commit 4792fe3
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/next-international/src/app/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ export function createI18nMiddleware<Locales extends readonly string[]>(
}

const response = NextResponse.next();
const requestLocale = request.nextUrl.pathname.split('/')?.[1] ?? locale;
const requestLocale = request.nextUrl.pathname.split('/')?.[1];

if (locales.includes(requestLocale) && config?.urlMappingStrategy === 'rewrite') {
const newUrl = new URL(request.nextUrl.pathname.slice(requestLocale.length + 1), request.url);
const response = NextResponse.redirect(newUrl);

if (locales.includes(requestLocale)) {
return addLocaleToResponse(response, requestLocale);
}

if (!requestLocale || locales.includes(requestLocale)) {
return addLocaleToResponse(response, requestLocale ?? defaultLocale);
}

return response;
};
}
Expand Down

0 comments on commit 4792fe3

Please sign in to comment.