diff --git a/.changeset/gorgeous-foxes-divide.md b/.changeset/gorgeous-foxes-divide.md
new file mode 100644
index 0000000000..2620074bde
--- /dev/null
+++ b/.changeset/gorgeous-foxes-divide.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fixes and issue where `Astro.currentLocale` always returned the default locale when consumed inside a server island.
diff --git a/packages/astro/e2e/fixtures/i18n/astro.config.mjs b/packages/astro/e2e/fixtures/i18n/astro.config.mjs
index 79d2b25f13..170c00451b 100644
--- a/packages/astro/e2e/fixtures/i18n/astro.config.mjs
+++ b/packages/astro/e2e/fixtures/i18n/astro.config.mjs
@@ -1,5 +1,5 @@
import { defineConfig } from 'astro/config';
-
+import nodejs from "@astrojs/node"
// https://astro.build/config
export default defineConfig({
i18n: {
@@ -17,4 +17,6 @@ export default defineConfig({
],
}],
},
+ output: 'static',
+ adapter: nodejs({ mode: 'standalone' }),
});
diff --git a/packages/astro/e2e/fixtures/i18n/components/Greeting.astro b/packages/astro/e2e/fixtures/i18n/components/Greeting.astro
new file mode 100644
index 0000000000..270fbacf4a
--- /dev/null
+++ b/packages/astro/e2e/fixtures/i18n/components/Greeting.astro
@@ -0,0 +1,4 @@
+---
+const locale = Astro.currentLocale
+---
+
Greeting {locale}!
diff --git a/packages/astro/e2e/fixtures/i18n/package.json b/packages/astro/e2e/fixtures/i18n/package.json
index 3d28c6bf01..fcc349c0af 100644
--- a/packages/astro/e2e/fixtures/i18n/package.json
+++ b/packages/astro/e2e/fixtures/i18n/package.json
@@ -3,6 +3,7 @@
"version": "0.0.0",
"private": true,
"dependencies": {
- "astro": "workspace:*"
+ "astro": "workspace:*",
+ "@astrojs/node": "^8.3.4"
}
}
diff --git a/packages/astro/e2e/fixtures/i18n/src/pages/fr/island.astro b/packages/astro/e2e/fixtures/i18n/src/pages/fr/island.astro
new file mode 100644
index 0000000000..5bc26968ef
--- /dev/null
+++ b/packages/astro/e2e/fixtures/i18n/src/pages/fr/island.astro
@@ -0,0 +1,5 @@
+---
+import Greeting from "../../../components/Greeting.astro"
+---
+This is a test
+
diff --git a/packages/astro/e2e/fixtures/i18n/src/pages/island.astro b/packages/astro/e2e/fixtures/i18n/src/pages/island.astro
new file mode 100644
index 0000000000..5cb4a2ed95
--- /dev/null
+++ b/packages/astro/e2e/fixtures/i18n/src/pages/island.astro
@@ -0,0 +1,6 @@
+---
+import Greeting from "../../components/Greeting.astro"
+---
+
+This is a test
+
diff --git a/packages/astro/e2e/i18n.test.js b/packages/astro/e2e/i18n.test.js
index 88d3a0b08c..bc86d5a49f 100644
--- a/packages/astro/e2e/i18n.test.js
+++ b/packages/astro/e2e/i18n.test.js
@@ -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!');
+ });
+});
diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts
index a4f2b26000..880d1f6187 100644
--- a/packages/astro/src/core/render-context.ts
+++ b/packages/astro/src/core/render-context.ts
@@ -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');
@@ -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;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index a148ecb121..83c8a8d535 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1038,6 +1038,9 @@ importers:
packages/astro/e2e/fixtures/i18n:
dependencies:
+ '@astrojs/node':
+ specifier: ^8.3.4
+ version: 8.3.4(astro@packages+astro)
astro:
specifier: workspace:*
version: link:../../..