TranslationKey
Syntax
PAGE.TranslationKey
Returns
string
翻译键会为给定页面的所有翻译之间创建一个关联。翻译键可以从文件路径派生,或者如果在前面的正文中定义了translationKey
参数的话,也可以从translationKey
参数派生。
使用以下站点配置:
hugo.
defaultContentLanguage: en
languages:
de:
contentDir: content/de
languageCode: de-DE
languageName: Deutsch
weight: 2
en:
contentDir: content/en
languageCode: en-US
languageName: English
weight: 1
defaultContentLanguage = 'en'
[languages]
[languages.de]
contentDir = 'content/de'
languageCode = 'de-DE'
languageName = 'Deutsch'
weight = 2
[languages.en]
contentDir = 'content/en'
languageCode = 'en-US'
languageName = 'English'
weight = 1
{
"defaultContentLanguage": "en",
"languages": {
"de": {
"contentDir": "content/de",
"languageCode": "de-DE",
"languageName": "Deutsch",
"weight": 2
},
"en": {
"contentDir": "content/en",
"languageCode": "en-US",
"languageName": "English",
"weight": 1
}
}
}
以及以下内容:
content/
├── de/
│ ├── books/
│ │ ├── buch-1.md
│ │ └── book-2.md
│ └── _index.md
├── en/
│ ├── books/
│ │ ├── book-1.md
│ │ └── book-2.md
│ └── _index.md
└── _index.md
还有以下前置正文:
content/en/books/book-1.md
---
title: Book 1
translationKey: foo
---
+++
title = 'Book 1'
translationKey = 'foo'
+++
{
"title": "Book 1",
"translationKey": "foo"
}
content/de/books/buch-1.md
---
title: Buch 1
translationKey: foo
---
+++
title = 'Buch 1'
translationKey = 'foo'
+++
{
"title": "Buch 1",
"translationKey": "foo"
}
在呈现上述页面的任意一个页面时:
{{ .TranslationKey }} → page/foo
如果Book 2的正文,在两种语言中都没有包含翻译键:
{{ .TranslationKey }} → page/books/book-2