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(routing): default locale when there's a list #12151

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-seas-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where `Astro.currentLocale` wasn't incorrectly computed when the `defaultLocale` belonged to a custom locale path.
1 change: 0 additions & 1 deletion packages/astro/src/actions/runtime/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ async function redirectWithResult({
if (!referer) {
throw new Error('Internal: Referer unexpectedly missing from Action POST request.');
}

return context.redirect(referer);
}

Expand Down
8 changes: 7 additions & 1 deletion packages/astro/src/core/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export type CreateContext = {
* A list of locales that are supported by the user
*/
userDefinedLocales?: string[];

/**
* User defined default locale
*/
defaultLocale: string;
};

/**
Expand All @@ -40,6 +45,7 @@ function createContext({
request,
params = {},
userDefinedLocales = [],
defaultLocale = '',
}: CreateContext): APIContext {
let preferredLocale: string | undefined = undefined;
let preferredLocaleList: string[] | undefined = undefined;
Expand Down Expand Up @@ -75,7 +81,7 @@ function createContext({
return (preferredLocaleList ??= computePreferredLocaleList(request, userDefinedLocales));
},
get currentLocale(): string | undefined {
return (currentLocale ??= computeCurrentLocale(route, userDefinedLocales));
return (currentLocale ??= computeCurrentLocale(route, userDefinedLocales, defaultLocale));
},
url,
get clientAddress() {
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,8 @@ export class RenderContext {
// and url.pathname to pass Astro.currentLocale tests.
// A single call with `routeData.pathname ?? routeData.route` as the pathname still fails.
return (this.#currentLocale ??=
computeCurrentLocale(routeData.route, locales) ??
computeCurrentLocale(url.pathname, locales) ??
computeCurrentLocale(routeData.route, locales, defaultLocale) ??
computeCurrentLocale(url.pathname, locales, defaultLocale) ??
fallbackTo);
}

Expand Down
19 changes: 18 additions & 1 deletion packages/astro/src/i18n/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ export function computePreferredLocaleList(request: Request, locales: Locales):
return result;
}

export function computeCurrentLocale(pathname: string, locales: Locales): undefined | string {
export function computeCurrentLocale(
pathname: string,
locales: Locales,
defaultLocale: string,
): string | undefined {
for (const segment of pathname.split('/')) {
for (const locale of locales) {
if (typeof locale === 'string') {
Expand All @@ -171,6 +175,19 @@ export function computeCurrentLocale(pathname: string, locales: Locales): undefi
}
}
}
// If we didn't exit, it's probably because we don't have any code/locale in the URL.
// We use the default locale.
for (const locale of locales) {
if (typeof locale === 'string') {
if (locale === defaultLocale) {
return locale;
}
} else {
if (locale.path === defaultLocale) {
return locale.codes.at(0);
}
}
}
}

export type RoutingStrategies =
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/fixtures/i18n-routing/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig} from "astro/config";

export default defineConfig({
i18n: {
defaultLocale: 'en',
defaultLocale: 'spanish',
locales: [
'en',
'pt',
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ describe('[SSG] i18n routing', () => {

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

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

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

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

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

Expand Down
1 change: 0 additions & 1 deletion pnpm-lock.yaml

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

Loading