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:
parent
25192a0599
commit
c1786d64c4
9 changed files with 61 additions and 5 deletions
5
.changeset/gorgeous-foxes-divide.md
Normal file
5
.changeset/gorgeous-foxes-divide.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes and issue where `Astro.currentLocale` always returned the default locale when consumed inside a server island.
|
|
@ -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' }),
|
||||
});
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
const locale = Astro.currentLocale
|
||||
---
|
||||
<p data-testid="greeting">Greeting {locale}!</p>
|
|
@ -3,6 +3,7 @@
|
|||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
"astro": "workspace:*",
|
||||
"@astrojs/node": "^8.3.4"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
import Greeting from "../../../components/Greeting.astro"
|
||||
---
|
||||
<p>This is a test</p>
|
||||
<Greeting server:defer />
|
6
packages/astro/e2e/fixtures/i18n/src/pages/island.astro
Normal file
6
packages/astro/e2e/fixtures/i18n/src/pages/island.astro
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
import Greeting from "../../components/Greeting.astro"
|
||||
---
|
||||
|
||||
<p>This is a test</p>
|
||||
<Greeting server:defer />
|
|
@ -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!');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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:../../..
|
||||
|
|
Loading…
Reference in a new issue