Skip to content

Commit

Permalink
fix(i18n): correctly compute the current locale (#12199)
Browse files Browse the repository at this point in the history
* fix(i18n): correctly compute the current locale

* Update packages/astro/test/fixtures/i18n-routing-dynamic/src/pages/[language].astro

Co-authored-by: Bjorn Lu <[email protected]>

* make code more readable

* rebase

---------

Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
ematipico and bluwy authored Oct 14, 2024
1 parent 07754f5 commit c351352
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-garlics-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a regression in the computation of `Astro.currentLocale`
20 changes: 13 additions & 7 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,19 @@ export class RenderContext {
? defaultLocale
: undefined;

// TODO: look into why computeCurrentLocale() needs routeData.route to pass ctx.currentLocale tests,
// and url.pathname to pass Astro.currentLocale tests.
// A single call with `routeData.pathname ?? routeData.route` as the pathname still fails.
return (this.#currentLocale ??=
computeCurrentLocale(routeData.route, locales, defaultLocale) ??
computeCurrentLocale(url.pathname, locales, defaultLocale) ??
fallbackTo);
if (this.#currentLocale) {
return this.#currentLocale
}

let computedLocale;
if (routeData.pathname) {
computedLocale = computeCurrentLocale(routeData.pathname, locales, defaultLocale)
} else {
computedLocale = computeCurrentLocale(url.pathname, locales, defaultLocale)
}
this.#currentLocale = computedLocale ?? fallbackTo;

return this.#currentLocale
}

#preferredLocale: APIContext['preferredLocale'];
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/i18n/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ export function computeCurrentLocale(
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;
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/test/fixtures/i18n-routing-dynamic/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "astro/config";

// https://astro.build/config
export default defineConfig({
i18n: {
defaultLocale: "ru",
locales: ["ru", "en"],
routing: {
prefixDefaultLocale: true,
},
},
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/i18n-routing-dynamic",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
export async function getStaticPaths() {
return [{ params: { language: "ru" } }, { params: { language: "en" } }];
}
const { currentLocale } = Astro;
---

<div>
{currentLocale}
</div>
Empty file.
28 changes: 26 additions & 2 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'node:assert/strict';
import { after, 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 { loadFixture } from './test-utils.js';
Expand Down Expand Up @@ -1105,7 +1105,7 @@ describe('[SSG] i18n routing', () => {
});

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

Expand Down Expand Up @@ -1151,6 +1151,30 @@ describe('[SSG] i18n routing', () => {
assert.equal(html.includes('Current Locale: pt'), true);
});
});

describe('with dynamic paths', async () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;

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

afterEach(async () => {
devServer.stop();
});

it('should return the correct current locale', async () => {
let html = await fixture.fetch('/en').then((r) => r.text());
assert.match(html, /en/);
html = await fixture.fetch('/ru').then((r) => r.text());
assert.match(html, /ru/);
});
});
});
});
describe('[SSR] i18n routing', () => {
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c351352

Please sign in to comment.