多语言 (Multilingual)
Snow 内置多语言支持,通过文件后缀或 FrontMatter 区分内容语言。
配置
1# 默认语言 2language: "zh" 3 4languages: 5 en: 6 translations: "i18n/en.yaml" 7 fr: 8 translations: "i18n/fr.yaml"
languages.{lang} 可覆盖任何全局配置(sections、pages、taxonomies、base_url 等)。
语言检测
每个文件的语言按以下优先级确定:
- FrontMatter 中的
lang字段 - 文件后缀(如
hello.en.md→en) - 站点默认语言
语言必须存在于 languages 配置中,否则回退到默认语言。
使用方式
文件后缀
1content/ 2├── _index.md # 中文(默认语言) 3├── _index.en.md # 英文 4├── about.md # 中文 5└── posts/ 6 ├── hello.md # 中文 7 └── hello.en.md # 英文
FrontMatter
1--- 2title: "Hello World" 3lang: "en" 4---
URL 映射
默认 URL 规则:
1content/_index.md → /index.html 2content/_index.en.md → /en/index.html 3content/posts/hello.md → /posts/hello/ 4content/posts/hello.en.md → /en/posts/hello/
通过路径变量自定义:
1pages: 2 posts: 3 path: "articles/{date:%Y}/{date:%m}/{slug}/" 4 5languages: 6 en: 7 pages: 8 posts: 9 path: "/english/articles/{date:%Y}/{slug}/"
i18n
模板中使用
1{% i18n "tags" %} 2{% T "共 %d 篇文章" 12 %} 3{{ i18n("authors") }} 4{{ T("hello %s", "world") }} 5{{ _("共 %.2f", 3.14) }}
i18n、T、_ 等价。
翻译文件
1# i18n/zh.yaml 2--- 3- id: "authors" 4 tr: "作者" 5- id: "tags" 6 tr: "标签"
1# i18n/en.yaml 2--- 3- id: "authors" 4 tr: "Authors" 5- id: "tags" 6 tr: "Tags"
配置翻译
1languages: 2 en: 3 translations: "i18n/en.yaml" 4 zh: 5 translations: 6 - id: "authors" 7 tr: "作者" 8 - id: "tags" 9 tr: "标签"
translations 可为文件路径或内联数组。主题 i18n/ 目录下的文件也会自动加载。
跨语言函数
1{{ get_page("posts/hello", "en") }} 2{{ get_section("posts", "en") }} 3{{ get_taxonomy("tags", "en") }}
省略语言参数则使用当前语言。