mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
[i18n] During fallback, only remove locale if prefix-always is false (#9226)
* i18n: During fallback, only remove locale if prefix-always is false * add test * add changeset
This commit is contained in:
parent
067a65f5b4
commit
8f8a40e93d
3 changed files with 36 additions and 2 deletions
5
.changeset/happy-beans-protect.md
Normal file
5
.changeset/happy-beans-protect.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix i18n fallback routing with routing strategy of always-prefix
|
|
@ -81,8 +81,8 @@ export function createI18nMiddleware(
|
||||||
const fallbackLocale = fallback[urlLocale];
|
const fallbackLocale = fallback[urlLocale];
|
||||||
let newPathname: string;
|
let newPathname: string;
|
||||||
// If a locale falls back to the default locale, we want to **remove** the locale because
|
// If a locale falls back to the default locale, we want to **remove** the locale because
|
||||||
// the default locale doesn't have a prefix
|
// the default locale doesn't have a prefix, but _only_ if prefix-always is false
|
||||||
if (fallbackLocale === defaultLocale) {
|
if (fallbackLocale === defaultLocale && i18n.routingStrategy !== 'prefix-always') {
|
||||||
newPathname = url.pathname.replace(`/${urlLocale}`, ``);
|
newPathname = url.pathname.replace(`/${urlLocale}`, ``);
|
||||||
} else {
|
} else {
|
||||||
newPathname = url.pathname.replace(`/${urlLocale}`, `/${fallbackLocale}`);
|
newPathname = url.pathname.replace(`/${urlLocale}`, `/${fallbackLocale}`);
|
||||||
|
|
|
@ -961,6 +961,35 @@ describe('[SSR] i18n routing', () => {
|
||||||
let response = await app.render(request);
|
let response = await app.render(request);
|
||||||
expect(response.status).to.equal(404);
|
expect(response.status).to.equal(404);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('with routing strategy [prefix-always]', () => {
|
||||||
|
before(async () => {
|
||||||
|
fixture = await loadFixture({
|
||||||
|
root: './fixtures/i18n-routing-fallback/',
|
||||||
|
output: 'server',
|
||||||
|
adapter: testAdapter(),
|
||||||
|
experimental: {
|
||||||
|
i18n: {
|
||||||
|
defaultLocale: 'en',
|
||||||
|
locales: ['en', 'pt', 'it'],
|
||||||
|
fallback: {
|
||||||
|
it: 'en',
|
||||||
|
},
|
||||||
|
routingStrategy: 'prefix-always',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await fixture.build();
|
||||||
|
app = await fixture.loadTestAdapterApp();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should redirect to the english locale, which is the first fallback', async () => {
|
||||||
|
let request = new Request('http://example.com/new-site/it/start');
|
||||||
|
let response = await app.render(request);
|
||||||
|
expect(response.status).to.equal(302);
|
||||||
|
expect(response.headers.get('location')).to.equal('/new-site/en/start');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('preferred locale', () => {
|
describe('preferred locale', () => {
|
||||||
|
|
Loading…
Reference in a new issue