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… - - - -

- 继续前往首页 → -

- -