Hugo 多语言下 Collection 名不同导致语言切换失效的解决方法

问题

FixIt 主题的 collection(合集)是一个自定义 taxonomy。中英文文章如果用了不同的 collection 名:

# zh-CN
collections: ["Quadlet 配置分享"]

# en
collections: ["Quadlet Config Sharing"]

/collections/ 页面和文章内的语言切换按钮会消失。原因是 Hugo 把它们当成两个独立的 taxonomy term,.IsTranslated.AllTranslations 找不到对应的翻译页面。

解决方法

quadlet 配置分享 这个 collection 为例,创建目录和文件:

content/collections/quadlet-config-sharing/
├── _index.zh-CN.md
└── _index.en.md

_index.zh-CN.md:

---
title: "Quadlet 配置分享"
---

_index.en.md:

---
title: "Quadlet Config Sharing"
---
  • 该 collections 的中英文路径都为 /collections/quadlet-config-sharing,即目录的名字
  • 中文 collection 名为 Quadlet 配置分享
  • 英文 collection 名为 Quadlet Config Sharing

文章配置

xxx.en.md:

---
collections: ["quadlet-config-sharing"]
---

xxx.zh-CN.md:

---
collections: ["quadlet-config-sharing"]
---