Skip to content

Commit

Permalink
fix(i18n): fix regression in current locale (#9998)
Browse files Browse the repository at this point in the history
* fix(i18n): fix regression in current locale

* Update .changeset/shy-wolves-ring.md

* Update packages/astro/test/i18n-routing.test.js

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>

* Update .changeset/six-fishes-beg.md

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>

* fix test case

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
ematipico and bluwy committed Feb 6, 2024
1 parent c53a313 commit 18ac094
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-wolves-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a bug in `Astro.currentLocale` that wasn't returning the correct locale when a locale is configured via `path`
5 changes: 5 additions & 0 deletions .changeset/six-fishes-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a regression in `Astro.currentLocale` where it stopped working properly with dynamic routes
16 changes: 14 additions & 2 deletions packages/astro/src/core/render/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,32 @@ export function computeCurrentLocale(
if (!routeData) {
return defaultLocale;
}

for (const segment of routeData.route.split('/')) {
// Typically, RouteData::pathname has the correct information in SSR, but it's not available in SSG, so we fall back
// to use the pathname from the Request
const pathname = routeData.pathname ?? new URL(request.url).pathname;
for (const segment of pathname.split('/').filter(Boolean)) {
for (const locale of locales) {
if (typeof locale === 'string') {
// we skip ta locale that isn't present in the current segment

if (!segment.includes(locale)) continue;
if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) {
return locale;
}
} else {
if (locale.path === segment) {
return locale.codes.at(0);
} else {
for (const code of locale.codes) {
if (normalizeTheLocale(code) === normalizeTheLocale(segment)) {
return code;
}
}
}
}
}
}

if (
routingStrategy === 'pathname-prefix-other-locales' ||
routingStrategy === 'domains-prefix-other-locales'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
const currentLocale = Astro.currentLocale;
export async function getStaticPaths() {
return [
{ params: { lang: undefined } },
{ params: { lang: 'es' } }
]
}
---


<html>
<head>
<title>Astro</title>
</head>
<body>
Current Locale: {currentLocale ? currentLocale : "none"}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
const currentLocale = Astro.currentLocale;
---

<html>
<head>
<title>Astro</title>
</head>
<body>
Current Locale: {currentLocale ? currentLocale : "none"}
</body>
</html>
69 changes: 68 additions & 1 deletion packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ describe('astro:i18n virtual module', () => {
let html = await response.text();
let $ = cheerio.load(html);

console.log(html);
expect($('body').text()).includes("Virtual module doesn't break");
expect($('body').text()).includes('Absolute URL pt: https://example.pt/about');
expect($('body').text()).includes('Absolute URL it: http://it.example.com/');
Expand Down Expand Up @@ -1005,6 +1004,67 @@ describe('[SSG] i18n routing', () => {
expect(html).to.include('Redirecting to: /new-site/');
});
});

describe('current locale', () => {
describe('with [prefix-other-locales]', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing/',
});
await fixture.build();
});

it('should return the default locale', async () => {
const html = await fixture.readFile('/current-locale/index.html');
expect(html).includes('Current Locale: en');
});

it('should return the default locale when rendering a route with spread operator', async () => {
const html = await fixture.readFile('/blog/es/index.html');
expect(html).includes('Current Locale: es');
});

it('should return the default locale of the current URL', async () => {
const html = await fixture.readFile('/pt/start/index.html');
expect(html).includes('Current Locale: pt');
});

it('should return the default locale when a route is dynamic', async () => {
const html = await fixture.readFile('/dynamic/lorem/index.html');
expect(html).includes('Current Locale: en');
});

it('should returns the correct locale when requesting a locale via path', async () => {
const html = await fixture.readFile('/spanish/index.html');
expect(html).includes('Current Locale: es');
});
});

describe('with [pathname-prefix-always]', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing-prefix-always/',
});
await fixture.build();
});

it('should return the locale of the current URL (en)', async () => {
const html = await fixture.readFile('/en/start/index.html');
expect(html).includes('Current Locale: en');
});

it('should return the locale of the current URL (pt)', async () => {
const html = await fixture.readFile('/pt/start/index.html');
expect(html).includes('Current Locale: pt');
});
});
});
});
describe('[SSR] i18n routing', () => {
let app;
Expand Down Expand Up @@ -1525,6 +1585,13 @@ describe('[SSR] i18n routing', () => {
expect(await response.text()).includes('Current Locale: en');
});

it('should return the default locale when rendering a route with spread operator', async () => {
let request = new Request('http://example.com/blog/es', {});
let response = await app.render(request);
expect(response.status).to.equal(200);
expect(await response.text()).includes('Current Locale: es');
});

it('should return the default locale of the current URL', async () => {
let request = new Request('http://example.com/pt/start', {});
let response = await app.render(request);
Expand Down

0 comments on commit 18ac094

Please sign in to comment.