0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

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 <bjornlu.dev@gmail.com>

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
Emanuele Stoppa 2024-01-29 14:47:40 +00:00 committed by GitHub
parent f27f790b92
commit 00ba9f1947
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 23 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes a bug in `Astro.currentLocale` where the value was incorrectly computed during the build.

View file

@ -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';

View file

@ -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)) {

View file

@ -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 {

View file

@ -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>

View file

@ -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);