Skip to content

Commit

Permalink
fix(i18n): default locale in server islands (#12341)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Oct 30, 2024
1 parent 25192a0 commit c1786d6
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-foxes-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes and issue where `Astro.currentLocale` always returned the default locale when consumed inside a server island.
4 changes: 3 additions & 1 deletion packages/astro/e2e/fixtures/i18n/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from 'astro/config';

import nodejs from "@astrojs/node"
// https://astro.build/config
export default defineConfig({
i18n: {
Expand All @@ -17,4 +17,6 @@ export default defineConfig({
],
}],
},
output: 'static',
adapter: nodejs({ mode: 'standalone' }),
});
4 changes: 4 additions & 0 deletions packages/astro/e2e/fixtures/i18n/components/Greeting.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
const locale = Astro.currentLocale
---
<p data-testid="greeting">Greeting {locale}!</p>
3 changes: 2 additions & 1 deletion packages/astro/e2e/fixtures/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
"astro": "workspace:*",
"@astrojs/node": "^8.3.4"
}
}
5 changes: 5 additions & 0 deletions packages/astro/e2e/fixtures/i18n/src/pages/fr/island.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
import Greeting from "../../../components/Greeting.astro"
---
<p>This is a test</p>
<Greeting server:defer />
6 changes: 6 additions & 0 deletions packages/astro/e2e/fixtures/i18n/src/pages/island.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
import Greeting from "../../components/Greeting.astro"
---

<p>This is a test</p>
<Greeting server:defer />
18 changes: 18 additions & 0 deletions packages/astro/e2e/i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ test.describe('i18n', () => {
await expect(p3).toHaveText('Locale: pt-AO');
});
});

test.describe('i18n default locale', () => {
test('is "en" when navigating the default locale page', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/island'));
let el = page.getByTestId('greeting');

await expect(el, 'element rendered').toBeVisible();
await expect(el).toHaveText('Greeting en!');
});

test('is "fr" when navigating the french page', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/fr/island'));
let el = page.getByTestId('greeting');

await expect(el, 'element rendered').toBeVisible();
await expect(el).toHaveText('Greeting fr!');
});
});
18 changes: 15 additions & 3 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { sequence } from './middleware/index.js';
import { renderRedirect } from './redirects/render.js';
import { type Pipeline, Slots, getParams, getProps } from './render/index.js';
import { copyRequest, setOriginPathname } from './routing/rewrite.js';
import { SERVER_ISLAND_COMPONENT } from './server-islands/endpoint.js';

export const apiContextRoutesSymbol = Symbol.for('context.routes');

Expand Down Expand Up @@ -526,11 +527,22 @@ export class RenderContext {
}

let computedLocale;
if (routeData.pathname) {
computedLocale = computeCurrentLocale(routeData.pathname, locales, defaultLocale);
if (routeData.component === SERVER_ISLAND_COMPONENT) {
let referer = this.request.headers.get('referer');
if (referer) {
if (URL.canParse(referer)) {
referer = new URL(referer).pathname;
}
computedLocale = computeCurrentLocale(referer, locales, defaultLocale);
}
} else {
computedLocale = computeCurrentLocale(url.pathname, locales, defaultLocale);
if (routeData.pathname) {
computedLocale = computeCurrentLocale(routeData.pathname, locales, defaultLocale);
} else {
computedLocale = computeCurrentLocale(url.pathname, locales, defaultLocale);
}
}

this.#currentLocale = computedLocale ?? fallbackTo;

return this.#currentLocale;
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit c1786d6

Please sign in to comment.