Skip to content

Commit

Permalink
use localeDetector when using noPrefix option
Browse files Browse the repository at this point in the history
  • Loading branch information
dcporter44 committed Apr 23, 2024
1 parent 49fbc44 commit 192e4cc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.4.2

- Fixes bug where `noPrefix` did not use `localeDetector` when no cookie present

## 5.4.1

- Update `next` dev dependency to 14.2.2
Expand Down
42 changes: 39 additions & 3 deletions __tests__/i18nRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ basePaths.forEach(basePath => {
const mockRewrite = jest.fn().mockReturnValue(new NextResponse());
NextResponse.rewrite = mockRewrite;

const request = mockRequest('/faq', ['en'], 'jp');
const request = mockRequest('/faq', ['zh']);

i18nRouter(request, {
locales: ['en', 'de', 'jp'],
Expand All @@ -360,8 +360,44 @@ basePaths.forEach(basePath => {

expect(mockRedirect).toHaveBeenCalledTimes(0);
expect(mockRewrite).toHaveBeenCalledTimes(1);
expect(mockRewrite.mock.calls[0][0]).toEqual(
new URL(`${basePath}/jp/faq`, 'https://example.com/faq')
expect(mockRewrite.mock.calls[0][0].href).toEqual(
new URL(`${basePath}/en/faq`, 'https://example.com/faq').href
);
});

it('should use cookie when noPrefix is true', () => {
const mockRewrite = jest.fn().mockReturnValue(new NextResponse());
NextResponse.rewrite = mockRewrite;

const request = mockRequest('/faq', ['en'], 'jp');

i18nRouter(request, {
locales: ['en', 'de', 'jp'],
defaultLocale: 'en',
basePath,
noPrefix: true
});

expect(mockRewrite.mock.calls[0][0].href).toEqual(
new URL(`${basePath}/jp/faq`, 'https://example.com/faq').href
);
});

it('should use localeDetector when noPrefix is true and no cookie', () => {
const request = mockRequest('/faq', ['de']);

const mockRewrite = jest.fn().mockReturnValue(new NextResponse());
NextResponse.rewrite = mockRewrite;

i18nRouter(request, {
locales: ['en', 'de', 'jp'],
defaultLocale: 'en',
basePath,
noPrefix: true
});

expect(mockRewrite.mock.calls[0][0].href).toEqual(
new URL(`${basePath}/de/faq`, 'https://example.com/faq').href
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-i18n-router",
"version": "5.4.1",
"version": "5.4.2",
"description": "Next.js App Router internationalized routing and locale detection.",
"repository": "https://github.com/i18nexus/next-i18n-router",
"keywords": [
Expand Down
44 changes: 14 additions & 30 deletions src/i18nRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,12 @@ function i18nRouter(request: NextRequest, config: Config): NextResponse {
}
}

if (noPrefix) {
let locale = cookieLocale || defaultLocale;

let newPath = `${locale}${pathname}`;

newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;

if (request.nextUrl.search) {
newPath += request.nextUrl.search;
}

response = NextResponse.rewrite(
new URL(newPath, request.url),
responseOptions
);

response.headers.set('x-next-i18n-router-locale', locale);

return response;
}

const pathLocale = locales.find(
locale => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
const pathLocale = noPrefix
? undefined
: locales.find(
locale =>
pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);

if (!pathLocale) {
let locale = cookieLocale;
Expand Down Expand Up @@ -99,13 +81,15 @@ function i18nRouter(request: NextRequest, config: Config): NextResponse {
newPath += request.nextUrl.search;
}

// redirect to prefixed path
if (prefixDefault || locale !== defaultLocale) {
if (noPrefix) {
response = NextResponse.rewrite(
new URL(newPath, request.url),
responseOptions
);
} else if (prefixDefault || locale !== defaultLocale) {
return NextResponse.redirect(new URL(newPath, request.url));
}

// If we get here, we're using the defaultLocale.
if (!prefixDefault) {
} else {
// prefixDefault is false and using default locale
response = NextResponse.rewrite(
new URL(newPath, request.url),
responseOptions
Expand Down

0 comments on commit 192e4cc

Please sign in to comment.