Skip to content

Conversation

@neveler
Copy link
Contributor

@neveler neveler commented Oct 3, 2025

尝试通过插件支持多语言

插件会根据内容所在文件的名称判断文件对应的语言区域代码,具体流程如下:

首先需要在 _config.yml 中配置字符串数组类型参数 locales 例如 locales: [zh, zh-TW, en] 作为系统支持的语言区域代码集合,插件获取 locale 作为系统的默认语言区域代码故默认代码的可以不写。

然后插件会在 :site:pre_render 时期遍历系统内的所有页面 Page 与文档 Document 根据其实际对应的文件名判断当前文档的语言区域代码。以配置 locale: zh locales: [zh, zh-TW, en] 为例 index.md index.es.md 的文件名中均未包含配置列表中的代码因此会统一采用默认代码 zhhelp.en.mdfaq.zh-TW.md 会分别被识别为 enzh-TW

多语言内容的 permalink 规则为 /{lang}{default content permalink} 例如 /test/a.md 对应的 permalink/test-a.html/test/a.en.md 对应的 permalink/en/test-a.html 。因此要求内容必须存在默认语言版本,如无默认语言版本则多语言插件则不会处理改内容。

需要启用多语言的内容需要在页面 data 中配置 i18n: true

插件为启用了多语言的内容启用了回退页面,即如何某种语言的内容并不存在,插件会自动在系统中使用语言代码匹配回退页面(当前是使用默认语言作为回退页面,即页面页面不存在时使用默认语言的内容填充,并使用警告提示用户当前显示语言并非实际页面对应的语言)。

关联 ISSUES

@zkitefly
Copy link
Member

zkitefly commented Oct 5, 2025

assets/img/hmcl/自动管理所有驱动器分页文件大小.webp
index.json

以上文件保留,其余可以删除

@neveler neveler marked this pull request as draft October 5, 2025 10:22
@zkitefly
Copy link
Member

zkitefly commented Oct 6, 2025

现在会自动获取所有可见集合中的内容生成 index.json 文件,但有一个问题当前会对集合中的所有文章生成索引数据,是否需要排除一些文章?生成索引的逻辑:获取所有布局为 document-collection 且指定了 collection 属性的页面,通过这些页面的 collection 属性获取指定集合的所有文章并提取文章的 title description url 属性进而生成 json 索引文件。

将 下载 集合的文章排除就好

@neveler neveler marked this pull request as ready for review October 7, 2025 05:57
@neveler neveler mentioned this pull request Oct 11, 2025
@neveler neveler marked this pull request as draft October 14, 2025 00:59
@neveler neveler changed the title 支持多语言 尝试通过插件的方式支持多语言 Nov 15, 2025
@neveler neveler changed the title 尝试通过插件的方式支持多语言 尝试通过插件支持多语言 Nov 15, 2025
@neveler neveler marked this pull request as ready for review November 16, 2025 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants