From 47476d408b6503cad420d4b8a4045732b99a6b7b Mon Sep 17 00:00:00 2001 From: caozh Date: Sat, 18 Apr 2026 02:15:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A4=9A=E8=AF=AD=E8=A8=80=E5=B0=86?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E8=AE=BE=E7=BD=AE=E4=B8=BA=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E7=9A=84=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/i18n/index.ts | 8 +++-- src/i18n/locales.ts | 10 +++++-- src/pages/{[lang] => [...lang]}/about.astro | 7 ++--- .../{[lang] => [...lang]}/curriculum.astro | 7 ++--- src/pages/{[lang] => [...lang]}/faculty.astro | 7 ++--- src/pages/{[lang] => [...lang]}/index.astro | 7 ++--- .../teaching-philosophy.astro | 7 ++--- src/pages/index.astro | 30 ------------------- 8 files changed, 28 insertions(+), 55 deletions(-) rename src/pages/{[lang] => [...lang]}/about.astro (94%) rename src/pages/{[lang] => [...lang]}/curriculum.astro (93%) rename src/pages/{[lang] => [...lang]}/faculty.astro (96%) rename src/pages/{[lang] => [...lang]}/index.astro (96%) rename src/pages/{[lang] => [...lang]}/teaching-philosophy.astro (96%) delete mode 100644 src/pages/index.astro diff --git a/src/i18n/index.ts b/src/i18n/index.ts index eda3632..62a75e5 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -12,12 +12,14 @@ export function getCopy(lang: string | undefined): SiteCopy { return copies[DEFAULT_LANG]; } -/** Astro `getStaticPaths`:为四种语言各生成一页 */ +/** Astro `getStaticPaths`:默认语言生成无前缀路径,其他语言带前缀 */ export function langStaticPaths() { - return LANGS.map((lang) => ({ params: { lang } })); + return LANGS.map((lang) => ({ + params: { lang: lang === DEFAULT_LANG ? undefined : lang }, + })); } export type { Lang } from './locales'; export type { SiteCopy } from './types'; -export { LANGS, DEFAULT_LANG, htmlLang, langLabels, hrefFor, isLang } from './locales'; +export { LANGS, DEFAULT_LANG, htmlLang, langLabels, hrefFor, isLang, resolveLang } from './locales'; export type { RouteKey } from './locales'; diff --git a/src/i18n/locales.ts b/src/i18n/locales.ts index 079bf36..3316ab1 100644 --- a/src/i18n/locales.ts +++ b/src/i18n/locales.ts @@ -26,6 +26,12 @@ export function isLang(value: string | undefined): value is Lang { export type RouteKey = '' | 'teaching-philosophy' | 'curriculum' | 'faculty' | 'about'; export function hrefFor(lang: Lang, segment: RouteKey): string { - if (!segment) return `/${lang}`; - return `/${lang}/${segment}`; + const prefix = lang === DEFAULT_LANG ? '' : `/${lang}`; + if (!segment) return prefix || '/'; + return `${prefix}/${segment}`; +} + +/** 从 [...lang] rest 参数解析语言,undefined 时返回默认语言 */ +export function resolveLang(param: string | undefined): Lang { + return isLang(param) ? param : DEFAULT_LANG; } diff --git a/src/pages/[lang]/about.astro b/src/pages/[...lang]/about.astro similarity index 94% rename from src/pages/[lang]/about.astro rename to src/pages/[...lang]/about.astro index 09bf8b0..bd1f46f 100644 --- a/src/pages/[lang]/about.astro +++ b/src/pages/[...lang]/about.astro @@ -1,14 +1,13 @@ --- import BaseLayout from '../../layouts/BaseLayout.astro'; -import { getCopy, langStaticPaths, type Lang } from '../../i18n'; +import { getCopy, langStaticPaths, resolveLang } from '../../i18n'; export function getStaticPaths() { return langStaticPaths(); } -const { lang } = Astro.params; -const L = getCopy(lang); -const locale = lang as Lang; +const locale = resolveLang(Astro.params.lang); +const L = getCopy(locale); --- - - - - - - - - Redirecting… - - - -

- 继续前往首页 → -

- -