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

目录

问题

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

# zh-CN
collections: ["Podman 教程"]

# en
collections: ["Podman Tutorial"]

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

解决方法

以 podman-tutorial 这个 collection 为例,创建目录和文件:

content/collections/podman-tutorial/
├── _index.zh-CN.md
└── _index.en.md

_index.zh-CN.md:

---
title: "Podman 教程"
---

_index.en.md:

---
title: "Podman Tutorial"
---
  • 该 collections 的中英文路径都为 /collections/podman-tutorial,即目录的名字
  • 中文 collection 名为 Podman 教程
  • 英文 collection 名为 Podman Tutorial

文章配置

xxx.en.md:

---
collections: ["podman-tutorial"]
---

xxx.zh-CN.md:

---
collections: ["podman-tutorial"]
---
编辑此页

目录