Skip to content

Commit 57be349

Browse files
Fix i18n current locale (#12839)
Co-authored-by: Emanuele Stoppa <[email protected]>
1 parent 971cfe5 commit 57be349

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

.changeset/happy-pianos-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix Astro.currentLocale returning the incorrect locale when using fallback rewrites in SSR mode

packages/astro/src/core/render-context.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,17 @@ export class RenderContext {
605605
computedLocale = computeCurrentLocale(referer, locales, defaultLocale);
606606
}
607607
} else {
608-
const pathname =
609-
routeData.pathname && !isRoute404or500(routeData) ? routeData.pathname : url.pathname;
608+
// For SSG we match the route naively, for dev we handle fallback on 404, for SSR we find route from fallbackRoutes
609+
let pathname = routeData.pathname;
610+
if (!routeData.pattern.test(url.pathname)) {
611+
for (const fallbackRoute of routeData.fallbackRoutes) {
612+
if (fallbackRoute.pattern.test(url.pathname)) {
613+
pathname = fallbackRoute.pathname;
614+
break;
615+
}
616+
}
617+
}
618+
pathname = pathname && !isRoute404or500(routeData) ? pathname : url.pathname;
610619
computedLocale = computeCurrentLocale(pathname, locales, defaultLocale);
611620
}
612621

packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
---
2+
const locale = Astro.currentLocale
3+
---
14
<html>
25
<head>
36
<title>Astro</title>
@@ -7,6 +10,10 @@
710
</head>
811
<body>
912
Hello
13+
<p>
14+
15+
locale - {locale}
16+
</p>
1017
</body>
1118
</html>
1219

packages/astro/test/i18n-routing.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import * as cheerio from 'cheerio';
12
import * as assert from 'node:assert/strict';
23
import { after, afterEach, before, describe, it } from 'node:test';
3-
import * as cheerio from 'cheerio';
44
import testAdapter from './test-adapter.js';
55
import { loadFixture } from './test-utils.js';
66

@@ -2014,13 +2014,13 @@ describe('Fallback rewrite dev server', () => {
20142014
locales: ['en', 'fr', 'es', 'it', 'pt'],
20152015
routing: {
20162016
prefixDefaultLocale: false,
2017+
fallbackType: 'rewrite',
20172018
},
20182019
fallback: {
20192020
fr: 'en',
20202021
it: 'en',
20212022
es: 'pt',
20222023
},
2023-
fallbackType: 'rewrite',
20242024
},
20252025
});
20262026
devServer = await fixture.startDevServer();
@@ -2032,6 +2032,7 @@ describe('Fallback rewrite dev server', () => {
20322032
it('should correctly rewrite to en', async () => {
20332033
const html = await fixture.fetch('/fr').then((res) => res.text());
20342034
assert.match(html, /Hello/);
2035+
assert.match(html, /locale - fr/);
20352036
// assert.fail()
20362037
});
20372038

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

@@ -2138,6 +2140,7 @@ describe('Fallback rewrite SSR', () => {
21382140
const response = await app.render(request);
21392141
assert.equal(response.status, 200);
21402142
const html = await response.text();
2143+
assert.match(html, /locale - fr/);
21412144
assert.match(html, /Hello/);
21422145
});
21432146

0 commit comments

Comments
 (0)