Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix i18n current locale #12839

Merged
merged 9 commits into from
Jan 10, 2025
Merged
5 changes: 5 additions & 0 deletions .changeset/happy-pianos-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Astro.currentLocale returning the incorrect locale when using fallback rewrites in SSR mode
10 changes: 8 additions & 2 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,14 @@ export class RenderContext {
computedLocale = computeCurrentLocale(referer, locales, defaultLocale);
}
} else {
const pathname =
routeData.pathname && !isRoute404or500(routeData) ? routeData.pathname : url.pathname;
// For SSG we match the route naively, for dev we handle fallback on 404, for SSR we find route from fallbackRoutes
const route =
(routeData.pattern.test(url.pathname)
? routeData
: routeData.fallbackRoutes.find((fallbackRoute) =>
fallbackRoute.pattern.test(url.pathname),
)) ?? routeData;
mtwilliams-code marked this conversation as resolved.
Show resolved Hide resolved
const pathname = route.pathname && !isRoute404or500(route) ? route.pathname : url.pathname;
computedLocale = computeCurrentLocale(pathname, locales, defaultLocale);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
const locale = Astro.currentLocale
---
<html>
<head>
<title>Astro</title>
Expand All @@ -7,6 +10,10 @@
</head>
<body>
Hello
<p>

locale - {locale}
</p>
</body>
</html>

Expand Down
7 changes: 5 additions & 2 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cheerio from 'cheerio';
import * as assert from 'node:assert/strict';
import { after, afterEach, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';

Expand Down Expand Up @@ -2014,13 +2014,13 @@ describe('Fallback rewrite dev server', () => {
locales: ['en', 'fr', 'es', 'it', 'pt'],
routing: {
prefixDefaultLocale: false,
fallbackType: 'rewrite',
},
fallback: {
fr: 'en',
it: 'en',
es: 'pt',
},
fallbackType: 'rewrite',
},
});
devServer = await fixture.startDevServer();
Expand All @@ -2032,6 +2032,7 @@ describe('Fallback rewrite dev server', () => {
it('should correctly rewrite to en', async () => {
const html = await fixture.fetch('/fr').then((res) => res.text());
assert.match(html, /Hello/);
assert.match(html, /locale - fr/);
// assert.fail()
});

Expand Down Expand Up @@ -2085,6 +2086,7 @@ describe('Fallback rewrite SSG', () => {
it('should correctly rewrite to en', async () => {
const html = await fixture.readFile('/fr/index.html');
assert.match(html, /Hello/);
assert.match(html, /locale - fr/);
// assert.fail()
});

Expand Down Expand Up @@ -2138,6 +2140,7 @@ describe('Fallback rewrite SSR', () => {
const response = await app.render(request);
assert.equal(response.status, 200);
const html = await response.text();
assert.match(html, /locale - fr/);
assert.match(html, /Hello/);
});

Expand Down
Loading