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:
parent
3a223b4811
commit
93993b77cf
5 changed files with 40 additions and 3 deletions
5
.changeset/brave-mayflies-share.md
Normal file
5
.changeset/brave-mayflies-share.md
Normal 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`
|
|
@ -24,6 +24,7 @@ export function requestIs404Or500(request: Request, base = '') {
|
|||
|
||||
return url.pathname.startsWith(`${base}/404`) || url.pathname.startsWith(`${base}/500`);
|
||||
}
|
||||
|
||||
// Checks if the pathname has any locale
|
||||
export function pathHasLocale(path: string, locales: Locales): boolean {
|
||||
const segments = path.split('/');
|
||||
|
@ -70,6 +71,7 @@ type GetLocaleAbsoluteUrl = GetLocaleRelativeUrl & {
|
|||
site: AstroConfig['site'];
|
||||
isBuild: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* The base URL
|
||||
*/
|
||||
|
|
|
@ -11,6 +11,7 @@ import * as I18nInternals from '../i18n/index.js';
|
|||
import type { RedirectToFallback } from '../i18n/index.js';
|
||||
import { toRoutingStrategy } from '../i18n/utils.js';
|
||||
import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js';
|
||||
|
||||
export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js';
|
||||
|
||||
const { trailingSlash, format, site, i18n, isBuild } =
|
||||
|
@ -19,7 +20,7 @@ const { trailingSlash, format, site, i18n, isBuild } =
|
|||
const { defaultLocale, locales, domains, fallback, routing } = i18n!;
|
||||
const base = import.meta.env.BASE_URL;
|
||||
|
||||
const strategy = toRoutingStrategy(routing, domains);
|
||||
let strategy = toRoutingStrategy(routing, domains);
|
||||
|
||||
export type GetLocaleOptions = I18nInternals.GetLocaleOptions;
|
||||
|
||||
|
@ -372,10 +373,11 @@ export let middleware: (customOptions: NewAstroRoutingConfigWithoutManual) => Mi
|
|||
|
||||
if (i18n?.routing === 'manual') {
|
||||
middleware = (customOptions: NewAstroRoutingConfigWithoutManual) => {
|
||||
strategy = toRoutingStrategy(customOptions, {});
|
||||
const manifest: SSRManifest['i18n'] = {
|
||||
...i18n,
|
||||
fallback: undefined,
|
||||
strategy: toRoutingStrategy(customOptions, {}),
|
||||
strategy,
|
||||
domainLookupTable: {},
|
||||
};
|
||||
return I18nInternals.createMiddleware(manifest, base, trailingSlash, format);
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
---
|
||||
import {getRelativeLocaleUrl} from "astro:i18n";
|
||||
|
||||
const customUrl = getRelativeLocaleUrl("en", "/blog/title")
|
||||
---
|
||||
<html>
|
||||
<head>
|
||||
<title>Astro</title>
|
||||
</head>
|
||||
<body>
|
||||
Hello
|
||||
Hello <p>{customUrl}</p>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -35,6 +35,14 @@ describe('Dev server manual routing', () => {
|
|||
const text = await response.text();
|
||||
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
|
||||
|
@ -61,6 +69,12 @@ describe('SSG manual routing', () => {
|
|||
let $ = cheerio.load(html);
|
||||
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
|
||||
|
@ -94,4 +108,13 @@ describe('SSR manual routing', () => {
|
|||
const text = await response.text();
|
||||
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/');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue