mirror of
https://github.com/withastro/astro.git
synced 2025-01-22 10:31:53 -05:00
Fix i18n current locale (#12839)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
This commit is contained in:
parent
971cfe52ae
commit
57be3494e2
4 changed files with 28 additions and 4 deletions
5
.changeset/happy-pianos-report.md
Normal file
5
.changeset/happy-pianos-report.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix Astro.currentLocale returning the incorrect locale when using fallback rewrites in SSR mode
|
|
@ -605,8 +605,17 @@ export class RenderContext {
|
||||||
computedLocale = computeCurrentLocale(referer, locales, defaultLocale);
|
computedLocale = computeCurrentLocale(referer, locales, defaultLocale);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const pathname =
|
// For SSG we match the route naively, for dev we handle fallback on 404, for SSR we find route from fallbackRoutes
|
||||||
routeData.pathname && !isRoute404or500(routeData) ? routeData.pathname : url.pathname;
|
let pathname = routeData.pathname;
|
||||||
|
if (!routeData.pattern.test(url.pathname)) {
|
||||||
|
for (const fallbackRoute of routeData.fallbackRoutes) {
|
||||||
|
if (fallbackRoute.pattern.test(url.pathname)) {
|
||||||
|
pathname = fallbackRoute.pathname;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pathname = pathname && !isRoute404or500(routeData) ? pathname : url.pathname;
|
||||||
computedLocale = computeCurrentLocale(pathname, locales, defaultLocale);
|
computedLocale = computeCurrentLocale(pathname, locales, defaultLocale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
---
|
||||||
|
const locale = Astro.currentLocale
|
||||||
|
---
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Astro</title>
|
<title>Astro</title>
|
||||||
|
@ -7,6 +10,10 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
Hello
|
Hello
|
||||||
|
<p>
|
||||||
|
|
||||||
|
locale - {locale}
|
||||||
|
</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
import * as cheerio from 'cheerio';
|
||||||
import * as assert from 'node:assert/strict';
|
import * as assert from 'node:assert/strict';
|
||||||
import { after, afterEach, before, describe, it } from 'node:test';
|
import { after, afterEach, before, describe, it } from 'node:test';
|
||||||
import * as cheerio from 'cheerio';
|
|
||||||
import testAdapter from './test-adapter.js';
|
import testAdapter from './test-adapter.js';
|
||||||
import { loadFixture } from './test-utils.js';
|
import { loadFixture } from './test-utils.js';
|
||||||
|
|
||||||
|
@ -2014,13 +2014,13 @@ describe('Fallback rewrite dev server', () => {
|
||||||
locales: ['en', 'fr', 'es', 'it', 'pt'],
|
locales: ['en', 'fr', 'es', 'it', 'pt'],
|
||||||
routing: {
|
routing: {
|
||||||
prefixDefaultLocale: false,
|
prefixDefaultLocale: false,
|
||||||
|
fallbackType: 'rewrite',
|
||||||
},
|
},
|
||||||
fallback: {
|
fallback: {
|
||||||
fr: 'en',
|
fr: 'en',
|
||||||
it: 'en',
|
it: 'en',
|
||||||
es: 'pt',
|
es: 'pt',
|
||||||
},
|
},
|
||||||
fallbackType: 'rewrite',
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
devServer = await fixture.startDevServer();
|
devServer = await fixture.startDevServer();
|
||||||
|
@ -2032,6 +2032,7 @@ describe('Fallback rewrite dev server', () => {
|
||||||
it('should correctly rewrite to en', async () => {
|
it('should correctly rewrite to en', async () => {
|
||||||
const html = await fixture.fetch('/fr').then((res) => res.text());
|
const html = await fixture.fetch('/fr').then((res) => res.text());
|
||||||
assert.match(html, /Hello/);
|
assert.match(html, /Hello/);
|
||||||
|
assert.match(html, /locale - fr/);
|
||||||
// assert.fail()
|
// assert.fail()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -2085,6 +2086,7 @@ describe('Fallback rewrite SSG', () => {
|
||||||
it('should correctly rewrite to en', async () => {
|
it('should correctly rewrite to en', async () => {
|
||||||
const html = await fixture.readFile('/fr/index.html');
|
const html = await fixture.readFile('/fr/index.html');
|
||||||
assert.match(html, /Hello/);
|
assert.match(html, /Hello/);
|
||||||
|
assert.match(html, /locale - fr/);
|
||||||
// assert.fail()
|
// assert.fail()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -2138,6 +2140,7 @@ describe('Fallback rewrite SSR', () => {
|
||||||
const response = await app.render(request);
|
const response = await app.render(request);
|
||||||
assert.equal(response.status, 200);
|
assert.equal(response.status, 200);
|
||||||
const html = await response.text();
|
const html = await response.text();
|
||||||
|
assert.match(html, /locale - fr/);
|
||||||
assert.match(html, /Hello/);
|
assert.match(html, /Hello/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue