0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

fix(i18n): default locale in server islands (#12341)

This commit is contained in:
Emanuele Stoppa 2024-10-30 19:38:10 +00:00 committed by GitHub
parent 25192a0599
commit c1786d64c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 61 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes and issue where `Astro.currentLocale` always returned the default locale when consumed inside a server island.

View file

@ -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' }),
});

View file

@ -0,0 +1,4 @@
---
const locale = Astro.currentLocale
---
<p data-testid="greeting">Greeting {locale}!</p>

View file

@ -3,6 +3,7 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
"astro": "workspace:*",
"@astrojs/node": "^8.3.4"
}
}

View file

@ -0,0 +1,5 @@
---
import Greeting from "../../../components/Greeting.astro"
---
<p>This is a test</p>
<Greeting server:defer />

View file

@ -0,0 +1,6 @@
---
import Greeting from "../../components/Greeting.astro"
---
<p>This is a test</p>
<Greeting server:defer />

View file

@ -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!');
});
});

View file

@ -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.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 {
if (routeData.pathname) {
computedLocale = computeCurrentLocale(routeData.pathname, locales, defaultLocale);
} else {
computedLocale = computeCurrentLocale(url.pathname, locales, defaultLocale);
}
}
this.#currentLocale = computedLocale ?? fallbackTo;
return this.#currentLocale;

View file

@ -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:../../..