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:
parent
f27f790b92
commit
00ba9f1947
6 changed files with 23 additions and 5 deletions
5
.changeset/large-kangaroos-camp.md
Normal file
5
.changeset/large-kangaroos-camp.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Fixes a bug in `Astro.currentLocale` where the value was incorrectly computed during the build.
|
|
@ -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';
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue