Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i18n): fix regression in current locale #9998

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 an regression in `Astro.currentLocale`, where it stopped working properly with dynamic routes
ematipico marked this conversation as resolved.
Show resolved Hide resolved
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>
71 changes: 70 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,15 @@ 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);
let text = await response.text();
console.log('text', text);
ematipico marked this conversation as resolved.
Show resolved Hide resolved
expect(response.status).to.equal(200);
// expect(await response.text()).includes('Current Locale: en');
});

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