0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -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:
evelyn masso 2023-11-29 13:46:51 -05:00 committed by GitHub
parent 067a65f5b4
commit 8f8a40e93d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix i18n fallback routing with routing strategy of always-prefix

View file

@ -81,8 +81,8 @@ export function createI18nMiddleware(
const fallbackLocale = fallback[urlLocale];
let newPathname: string;
// If a locale falls back to the default locale, we want to **remove** the locale because
// the default locale doesn't have a prefix
if (fallbackLocale === defaultLocale) {
// the default locale doesn't have a prefix, but _only_ if prefix-always is false
if (fallbackLocale === defaultLocale && i18n.routingStrategy !== 'prefix-always') {
newPathname = url.pathname.replace(`/${urlLocale}`, ``);
} else {
newPathname = url.pathname.replace(`/${urlLocale}`, `/${fallbackLocale}`);

View file

@ -961,6 +961,35 @@ describe('[SSR] i18n routing', () => {
let response = await app.render(request);
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', () => {