0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-10 23:01:26 -05:00

fix(i18n): correctly compute the current locale (#12199)

* fix(i18n): correctly compute the current locale

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

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

* make code more readable

* rebase

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
Emanuele Stoppa 2024-10-14 14:58:33 +01:00 committed by GitHub
parent 07754f5873
commit c351352360
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 82 additions and 10 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a regression in the computation of `Astro.currentLocale`

View file

@ -531,13 +531,19 @@ export class RenderContext {
? defaultLocale ? defaultLocale
: undefined; : undefined;
// TODO: look into why computeCurrentLocale() needs routeData.route to pass ctx.currentLocale tests, if (this.#currentLocale) {
// and url.pathname to pass Astro.currentLocale tests. return this.#currentLocale
// A single call with `routeData.pathname ?? routeData.route` as the pathname still fails. }
return (this.#currentLocale ??=
computeCurrentLocale(routeData.route, locales, defaultLocale) ?? let computedLocale;
computeCurrentLocale(url.pathname, locales, defaultLocale) ?? if (routeData.pathname) {
fallbackTo); computedLocale = computeCurrentLocale(routeData.pathname, locales, defaultLocale)
} else {
computedLocale = computeCurrentLocale(url.pathname, locales, defaultLocale)
}
this.#currentLocale = computedLocale ?? fallbackTo;
return this.#currentLocale
} }
#preferredLocale: APIContext['preferredLocale']; #preferredLocale: APIContext['preferredLocale'];

View file

@ -157,7 +157,6 @@ export function computeCurrentLocale(
for (const locale of locales) { for (const locale of locales) {
if (typeof locale === 'string') { if (typeof locale === 'string') {
// we skip ta locale that isn't present in the current segment // we skip ta locale that isn't present in the current segment
if (!segment.includes(locale)) continue; if (!segment.includes(locale)) continue;
if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) { if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) {
return locale; return locale;

View file

@ -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,
},
},
});

View file

@ -0,0 +1,8 @@
{
"name": "@test/i18n-routing-dynamic",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,11 @@
---
export async function getStaticPaths() {
return [{ params: { language: "ru" } }, { params: { language: "en" } }];
}
const { currentLocale } = Astro;
---
<div>
{currentLocale}
</div>

View file

@ -1,5 +1,5 @@
import * as assert from 'node:assert/strict'; 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 * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js'; import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js'; import { loadFixture } from './test-utils.js';
@ -1105,7 +1105,7 @@ describe('[SSG] i18n routing', () => {
}); });
it('should return the default locale', async () => { 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); assert.equal(html.includes('Current Locale: es'), true);
}); });
@ -1151,6 +1151,30 @@ describe('[SSG] i18n routing', () => {
assert.equal(html.includes('Current Locale: pt'), true); 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', () => { describe('[SSR] i18n routing', () => {

6
pnpm-lock.yaml generated
View file

@ -3135,6 +3135,12 @@ importers:
specifier: workspace:* specifier: workspace:*
version: link:../../.. version: link:../../..
packages/astro/test/fixtures/i18n-routing-dynamic:
dependencies:
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/i18n-routing-fallback: packages/astro/test/fixtures/i18n-routing-fallback:
dependencies: dependencies:
astro: astro: