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:
parent
07754f5873
commit
c351352360
9 changed files with 82 additions and 10 deletions
5
.changeset/sharp-garlics-float.md
Normal file
5
.changeset/sharp-garlics-float.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes a regression in the computation of `Astro.currentLocale`
|
|
@ -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'];
|
||||
|
|
|
@ -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;
|
||||
|
|
13
packages/astro/test/fixtures/i18n-routing-dynamic/astro.config.mjs
vendored
Normal file
13
packages/astro/test/fixtures/i18n-routing-dynamic/astro.config.mjs
vendored
Normal 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,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
8
packages/astro/test/fixtures/i18n-routing-dynamic/package.json
vendored
Normal file
8
packages/astro/test/fixtures/i18n-routing-dynamic/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@test/i18n-routing-dynamic",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
11
packages/astro/test/fixtures/i18n-routing-dynamic/src/pages/[language].astro
vendored
Normal file
11
packages/astro/test/fixtures/i18n-routing-dynamic/src/pages/[language].astro
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
export async function getStaticPaths() {
|
||||
return [{ params: { language: "ru" } }, { params: { language: "en" } }];
|
||||
}
|
||||
|
||||
const { currentLocale } = Astro;
|
||||
---
|
||||
|
||||
<div>
|
||||
{currentLocale}
|
||||
</div>
|
0
packages/astro/test/fixtures/i18n-routing-dynamic/src/pages/index.astro
vendored
Normal file
0
packages/astro/test/fixtures/i18n-routing-dynamic/src/pages/index.astro
vendored
Normal file
|
@ -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';
|
||||
|
@ -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);
|
||||
});
|
||||
|
||||
|
@ -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', () => {
|
||||
|
|
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
|
@ -3135,6 +3135,12 @@ importers:
|
|||
specifier: workspace:*
|
||||
version: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/i18n-routing-dynamic:
|
||||
dependencies:
|
||||
astro:
|
||||
specifier: workspace:*
|
||||
version: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/i18n-routing-fallback:
|
||||
dependencies:
|
||||
astro:
|
||||
|
|
Loading…
Add table
Reference in a new issue