feat: language switch links to same post in other language
Deploy / build (push) Successful in 10s

- Add translation field to schema + auto-match by zh/ prefix
- BaseLayout accepts per-page switchHref; post page resolves translation link
- Falls back to section landing (/ or /zh/) when no translation exists
This commit is contained in:
hoelee committed 2026-09-06 06:50:37 +08:00
1 parent 677fc7cb20
commit 6b56c1f4db
3 files changed
+20 -3

No files matched your search

+9
View File
@@ -37,6 +37,14 @@ const related = all
.sort((a, b) => b.score - a.score)
.slice(0, 3)
.map((r) => r.post);
// Translation link for the language switch:
// 1. Explicit `translation` field wins.
// 2. Else auto-match the other-language slug (strip/add the `zh/` prefix).
const otherLangSlug = lang === 'en' ? `zh/${post.slug}` : post.slug.replace(/^zh\//, '');
const translation = post.data.translation
?? (all.some((p) => p.slug === otherLangSlug) ? otherLangSlug : undefined);
const switchHref = translation ? `/posts/${translation}/` : undefined;
---
<BaseLayout
@@ -45,6 +53,7 @@ const related = all
lang={lang}
ogImage={post.data.ogImage ?? '/og-default.png'}
type="article"
switchHref={switchHref}
article={{
publishedTime: post.data.pubDate.toISOString(),
modifiedTime: post.data.updatedDate?.toISOString(),