From 00ba9f1947ca9016cd0ee4d8f6048027fab2ab9a Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 29 Jan 2024 14:47:40 +0000 Subject: [PATCH] fix(i18n): compute current locale from route instead of request (#9865) * fix(i18n): compute current locale from route instead of request * Update .changeset/large-kangaroos-camp.md Co-authored-by: Bjorn Lu --------- Co-authored-by: Bjorn Lu --- .changeset/large-kangaroos-camp.md | 5 +++++ packages/astro/src/core/constants.ts | 2 ++ packages/astro/src/core/render/context.ts | 10 ++++++++-- packages/astro/src/i18n/middleware.ts | 3 ++- .../i18n-routing-fallback/src/pages/pt/start.astro | 6 +++++- packages/astro/test/i18n-routing.test.js | 2 +- 6 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 .changeset/large-kangaroos-camp.md diff --git a/.changeset/large-kangaroos-camp.md b/.changeset/large-kangaroos-camp.md new file mode 100644 index 000000000000..bc42ff75661c --- /dev/null +++ b/.changeset/large-kangaroos-camp.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes a bug in `Astro.currentLocale` where the value was incorrectly computed during the build. diff --git a/packages/astro/src/core/constants.ts b/packages/astro/src/core/constants.ts index 471614ce3359..af00d655d7c2 100644 --- a/packages/astro/src/core/constants.ts +++ b/packages/astro/src/core/constants.ts @@ -13,3 +13,5 @@ export const SUPPORTED_MARKDOWN_FILE_EXTENSIONS = [ // The folder name where to find the middleware export const MIDDLEWARE_PATH_SEGMENT_NAME = 'middleware'; + +export const ROUTE_DATA_SYMBOL = 'astro.routeData'; diff --git a/packages/astro/src/core/render/context.ts b/packages/astro/src/core/render/context.ts index 9b94f4ad2f8f..898e74d8f396 100644 --- a/packages/astro/src/core/render/context.ts +++ b/packages/astro/src/core/render/context.ts @@ -12,8 +12,10 @@ import { AstroError, AstroErrorData } from '../errors/index.js'; import type { Environment } from './environment.js'; import { getParamsAndProps } from './params-and-props.js'; import type { RoutingStrategies } from '../config/schema.js'; +import { ROUTE_DATA_SYMBOL } from '../constants.js'; const clientLocalsSymbol = Symbol.for('astro.locals'); +const routeDataSymbol = Symbol.for(ROUTE_DATA_SYMBOL); /** * The RenderContext represents the parts of rendering that are specific to one request. @@ -243,8 +245,12 @@ export function computeCurrentLocale( routingStrategy: RoutingStrategies | undefined, defaultLocale: string | undefined ): undefined | string { - const requestUrl = new URL(request.url); - for (const segment of requestUrl.pathname.split('/')) { + const routeData: RouteData | undefined = Reflect.get(request, routeDataSymbol); + if (!routeData) { + return defaultLocale; + } + + for (const segment of routeData.route.split('/')) { for (const locale of locales) { if (typeof locale === 'string') { if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) { diff --git a/packages/astro/src/i18n/middleware.ts b/packages/astro/src/i18n/middleware.ts index 1298dcac8154..a7c50dc82d53 100644 --- a/packages/astro/src/i18n/middleware.ts +++ b/packages/astro/src/i18n/middleware.ts @@ -3,8 +3,9 @@ import type { Locales, MiddlewareHandler, RouteData, SSRManifest } from '../@typ import type { PipelineHookFunction } from '../core/pipeline.js'; import { getPathByLocale, normalizeTheLocale } from './index.js'; import { shouldAppendForwardSlash } from '../core/build/util.js'; +import { ROUTE_DATA_SYMBOL } from '../core/constants.js'; -const routeDataSymbol = Symbol.for('astro.routeData'); +const routeDataSymbol = Symbol.for(ROUTE_DATA_SYMBOL); // Checks if the pathname has any locale, exception for the defaultLocale, which is ignored on purpose. function pathnameHasLocale(pathname: string, locales: Locales): boolean { diff --git a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/pt/start.astro b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/pt/start.astro index 5a4a84c2cf0c..e0ae5bb5fcd5 100644 --- a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/pt/start.astro +++ b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/pt/start.astro @@ -1,8 +1,12 @@ +--- +const currentLocale = Astro.currentLocale; +--- + Astro -Oi essa e start +Oi essa e start: {currentLocale} diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index 6cc445c1ceea..da9fdc7c8316 100644 --- a/packages/astro/test/i18n-routing.test.js +++ b/packages/astro/test/i18n-routing.test.js @@ -844,7 +844,7 @@ describe('[SSG] i18n routing', () => { it('should render localised page correctly', async () => { let html = await fixture.readFile('/pt/start/index.html'); let $ = cheerio.load(html); - expect($('body').text()).includes('Oi essa e start'); + expect($('body').text()).includes('Oi essa e start: pt'); html = await fixture.readFile('/pt/blog/1/index.html'); $ = cheerio.load(html);