Skip to content

Commit bb6d37f

Browse files
authored
fix(routing): default locale when there's a list (#12151)
* fix(routing): default locale when there's a list * fix(routing): default locale when there's a list
1 parent 50dd88b commit bb6d37f

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed

.changeset/stupid-seas-roll.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes an issue where `Astro.currentLocale` wasn't incorrectly computed when the `defaultLocale` belonged to a custom locale path.

packages/astro/src/actions/runtime/middleware.ts

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ async function redirectWithResult({
133133
if (!referer) {
134134
throw new Error('Internal: Referer unexpectedly missing from Action POST request.');
135135
}
136-
137136
return context.redirect(referer);
138137
}
139138

packages/astro/src/core/middleware/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export type CreateContext = {
3131
* A list of locales that are supported by the user
3232
*/
3333
userDefinedLocales?: string[];
34+
35+
/**
36+
* User defined default locale
37+
*/
38+
defaultLocale: string;
3439
};
3540

3641
/**
@@ -40,6 +45,7 @@ function createContext({
4045
request,
4146
params = {},
4247
userDefinedLocales = [],
48+
defaultLocale = '',
4349
}: CreateContext): APIContext {
4450
let preferredLocale: string | undefined = undefined;
4551
let preferredLocaleList: string[] | undefined = undefined;
@@ -75,7 +81,7 @@ function createContext({
7581
return (preferredLocaleList ??= computePreferredLocaleList(request, userDefinedLocales));
7682
},
7783
get currentLocale(): string | undefined {
78-
return (currentLocale ??= computeCurrentLocale(route, userDefinedLocales));
84+
return (currentLocale ??= computeCurrentLocale(route, userDefinedLocales, defaultLocale));
7985
},
8086
url,
8187
get clientAddress() {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ export class RenderContext {
535535
// and url.pathname to pass Astro.currentLocale tests.
536536
// A single call with `routeData.pathname ?? routeData.route` as the pathname still fails.
537537
return (this.#currentLocale ??=
538-
computeCurrentLocale(routeData.route, locales) ??
539-
computeCurrentLocale(url.pathname, locales) ??
538+
computeCurrentLocale(routeData.route, locales, defaultLocale) ??
539+
computeCurrentLocale(url.pathname, locales, defaultLocale) ??
540540
fallbackTo);
541541
}
542542

packages/astro/src/i18n/utils.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ export function computePreferredLocaleList(request: Request, locales: Locales):
148148
return result;
149149
}
150150

151-
export function computeCurrentLocale(pathname: string, locales: Locales): undefined | string {
151+
export function computeCurrentLocale(
152+
pathname: string,
153+
locales: Locales,
154+
defaultLocale: string,
155+
): string | undefined {
152156
for (const segment of pathname.split('/')) {
153157
for (const locale of locales) {
154158
if (typeof locale === 'string') {
@@ -171,6 +175,19 @@ export function computeCurrentLocale(pathname: string, locales: Locales): undefi
171175
}
172176
}
173177
}
178+
// If we didn't exit, it's probably because we don't have any code/locale in the URL.
179+
// We use the default locale.
180+
for (const locale of locales) {
181+
if (typeof locale === 'string') {
182+
if (locale === defaultLocale) {
183+
return locale;
184+
}
185+
} else {
186+
if (locale.path === defaultLocale) {
187+
return locale.codes.at(0);
188+
}
189+
}
190+
}
174191
}
175192

176193
export type RoutingStrategies =

packages/astro/test/fixtures/i18n-routing/astro.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig} from "astro/config";
22

33
export default defineConfig({
44
i18n: {
5-
defaultLocale: 'en',
5+
defaultLocale: 'spanish',
66
locales: [
77
'en',
88
'pt',

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ describe('[SSG] i18n routing', () => {
11061106

11071107
it('should return the default locale', async () => {
11081108
const html = await fixture.readFile('/current-locale/index.html');
1109-
assert.equal(html.includes('Current Locale: en'), true);
1109+
assert.equal(html.includes('Current Locale: es'), true);
11101110
});
11111111

11121112
it('should return the default locale when rendering a route with spread operator', async () => {
@@ -1121,7 +1121,7 @@ describe('[SSG] i18n routing', () => {
11211121

11221122
it('should return the default locale when a route is dynamic', async () => {
11231123
const html = await fixture.readFile('/dynamic/lorem/index.html');
1124-
assert.equal(html.includes('Current Locale: en'), true);
1124+
assert.equal(html.includes('Current Locale: es'), true);
11251125
});
11261126

11271127
it('should returns the correct locale when requesting a locale via path', async () => {
@@ -1701,7 +1701,7 @@ describe('[SSR] i18n routing', () => {
17011701
let request = new Request('http://example.com/current-locale', {});
17021702
let response = await app.render(request);
17031703
assert.equal(response.status, 200);
1704-
assert.equal((await response.text()).includes('Current Locale: en'), true);
1704+
assert.equal((await response.text()).includes('Current Locale: es'), true);
17051705
});
17061706

17071707
it('should return the default locale when rendering a route with spread operator', async () => {
@@ -1722,7 +1722,7 @@ describe('[SSR] i18n routing', () => {
17221722
let request = new Request('http://example.com/dynamic/lorem', {});
17231723
let response = await app.render(request);
17241724
assert.equal(response.status, 200);
1725-
assert.equal((await response.text()).includes('Current Locale: en'), true);
1725+
assert.equal((await response.text()).includes('Current Locale: es'), true);
17261726
});
17271727
});
17281728

0 commit comments

Comments
 (0)