0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-17 22:44:24 -05:00

fix(i18n): update strategy when defining manually astro i18n middleware (#11362)

This commit is contained in:
Emanuele Stoppa 2024-07-01 14:42:07 +01:00 committed by GitHub
parent 3a223b4811
commit 93993b77cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 40 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes an issue where creating manually the i18n middleware could break the logic of the functions of the virtual module `astro:i18n`

View file

@ -24,6 +24,7 @@ export function requestIs404Or500(request: Request, base = '') {
return url.pathname.startsWith(`${base}/404`) || url.pathname.startsWith(`${base}/500`); return url.pathname.startsWith(`${base}/404`) || url.pathname.startsWith(`${base}/500`);
} }
// Checks if the pathname has any locale // Checks if the pathname has any locale
export function pathHasLocale(path: string, locales: Locales): boolean { export function pathHasLocale(path: string, locales: Locales): boolean {
const segments = path.split('/'); const segments = path.split('/');
@ -70,6 +71,7 @@ type GetLocaleAbsoluteUrl = GetLocaleRelativeUrl & {
site: AstroConfig['site']; site: AstroConfig['site'];
isBuild: boolean; isBuild: boolean;
}; };
/** /**
* The base URL * The base URL
*/ */

View file

@ -11,6 +11,7 @@ import * as I18nInternals from '../i18n/index.js';
import type { RedirectToFallback } from '../i18n/index.js'; import type { RedirectToFallback } from '../i18n/index.js';
import { toRoutingStrategy } from '../i18n/utils.js'; import { toRoutingStrategy } from '../i18n/utils.js';
import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js'; import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js';
export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js'; export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js';
const { trailingSlash, format, site, i18n, isBuild } = const { trailingSlash, format, site, i18n, isBuild } =
@ -19,7 +20,7 @@ const { trailingSlash, format, site, i18n, isBuild } =
const { defaultLocale, locales, domains, fallback, routing } = i18n!; const { defaultLocale, locales, domains, fallback, routing } = i18n!;
const base = import.meta.env.BASE_URL; const base = import.meta.env.BASE_URL;
const strategy = toRoutingStrategy(routing, domains); let strategy = toRoutingStrategy(routing, domains);
export type GetLocaleOptions = I18nInternals.GetLocaleOptions; export type GetLocaleOptions = I18nInternals.GetLocaleOptions;
@ -372,10 +373,11 @@ export let middleware: (customOptions: NewAstroRoutingConfigWithoutManual) => Mi
if (i18n?.routing === 'manual') { if (i18n?.routing === 'manual') {
middleware = (customOptions: NewAstroRoutingConfigWithoutManual) => { middleware = (customOptions: NewAstroRoutingConfigWithoutManual) => {
strategy = toRoutingStrategy(customOptions, {});
const manifest: SSRManifest['i18n'] = { const manifest: SSRManifest['i18n'] = {
...i18n, ...i18n,
fallback: undefined, fallback: undefined,
strategy: toRoutingStrategy(customOptions, {}), strategy,
domainLookupTable: {}, domainLookupTable: {},
}; };
return I18nInternals.createMiddleware(manifest, base, trailingSlash, format); return I18nInternals.createMiddleware(manifest, base, trailingSlash, format);

View file

@ -1,8 +1,13 @@
---
import {getRelativeLocaleUrl} from "astro:i18n";
const customUrl = getRelativeLocaleUrl("en", "/blog/title")
---
<html> <html>
<head> <head>
<title>Astro</title> <title>Astro</title>
</head> </head>
<body> <body>
Hello Hello <p>{customUrl}</p>
</body> </body>
</html> </html>

View file

@ -35,6 +35,14 @@ describe('Dev server manual routing', () => {
const text = await response.text(); const text = await response.text();
assert.equal(text.includes('ABOUT ME'), true); assert.equal(text.includes('ABOUT ME'), true);
}); });
it('should correctly print the relative locale url', async () => {
const response = await fixture.fetch('/en/start');
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('p').text(), '/en/blog/title/');
});
}); });
// //
// // SSG // // SSG
@ -61,6 +69,12 @@ describe('SSG manual routing', () => {
let $ = cheerio.load(html); let $ = cheerio.load(html);
assert.equal($('body').text().includes('ABOUT ME'), true); assert.equal($('body').text().includes('ABOUT ME'), true);
}); });
it('should correctly print the relative locale url', async () => {
const html = await fixture.readFile('/en/start/index.html');
const $ = cheerio.load(html);
assert.equal($('p').text(), '/en/blog/title/');
});
}); });
// // SSR // // SSR
@ -94,4 +108,13 @@ describe('SSR manual routing', () => {
const text = await response.text(); const text = await response.text();
assert.equal(text.includes('ABOUT ME'), true); assert.equal(text.includes('ABOUT ME'), true);
}); });
it('should correctly print the relative locale url', async () => {
let request = new Request('http://example.com/en/start');
let response = await app.render(request);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('p').text(), '/en/blog/title/');
});
}); });