Skip to content

Commit

Permalink
fix(i18n): compute current locale from route instead of request (#9865)
Browse files Browse the repository at this point in the history
* fix(i18n): compute current locale from route instead of request

* Update .changeset/large-kangaroos-camp.md

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
ematipico and bluwy committed Jan 29, 2024
1 parent f27f790 commit 00ba9f1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-kangaroos-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a bug in `Astro.currentLocale` where the value was incorrectly computed during the build.
2 changes: 2 additions & 0 deletions packages/astro/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
10 changes: 8 additions & 2 deletions packages/astro/src/core/render/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
const currentLocale = Astro.currentLocale;
---

<html>
<head>
<title>Astro</title>
</head>
<body>
Oi essa e start
Oi essa e start: {currentLocale}
</body>
</html>
2 changes: 1 addition & 1 deletion packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 00ba9f1

Please sign in to comment.