Skip to content

Commit

Permalink
fix: return correct locale in 404 pages with i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
apatel369 committed Nov 3, 2024
1 parent 5751488 commit 8f35066
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
7 changes: 2 additions & 5 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,8 @@ export class RenderContext {
}

let computedLocale;
if (routeData.pathname) {
computedLocale = computeCurrentLocale(routeData.pathname, locales, defaultLocale);
} else {
computedLocale = computeCurrentLocale(url.pathname, locales, defaultLocale);
}
const pathname = routeData.pathname && routeData.pathname !== '/404' ? routeData.pathname : url.pathname;
computedLocale = computeCurrentLocale(pathname, locales, defaultLocale);
this.#currentLocale = computedLocale ?? fallbackTo;

return this.#currentLocale;
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/fixtures/i18n-routing/src/pages/404.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
const currentLocale = Astro.currentLocale;
---
<html>
<head>
<title>404 - Not Found</title>
</head>
<body>
<h1>404 - Not Found</h1>
<p>Current Locale: {currentLocale ? currentLocale : "none"}</p>
</body>
</html>
24 changes: 24 additions & 0 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ describe('[DEV] i18n routing', () => {
const response = await fixture.fetch('/fr/start');
assert.equal(response.status, 404);
});

it('should return the correct locale on 404 page for default locale', async () => {
const html = await fixture.fetch('/nonexistent-page').then((res) => res.text());
assert.match(html, /Current Locale: es/);
});

it('should return the correct locale on 404 page for english locale', async () => {
const html = await fixture.fetch('/en/nonexistent-page').then((res) => res.text());
assert.match(html, /Current Locale: en/);
});
});

describe('i18n routing, with base', () => {
Expand Down Expand Up @@ -1255,6 +1265,20 @@ describe('[SSR] i18n routing', () => {
let response = await app.render(request);
assert.equal(response.status, 404);
});

it('should return the correct locale on 404 page for default locale', async () => {
let request = new Request('http://example.com/non-existent');
let response = await app.render(request);
assert.equal(response.status, 404);
assert.equal((await response.text()).includes('Current Locale: es'), true);
});

it('should return the correct locale on 404 page for english locale', async () => {
let request = new Request('http://example.com/en/non-existent');
let response = await app.render(request);
assert.equal(response.status, 404);
assert.equal((await response.text()).includes('Current Locale: en'), true);
});
});

describe('with base', () => {
Expand Down

0 comments on commit 8f35066

Please sign in to comment.