HUGO
Menu
GitHub 90545 stars Mastodon

TranslationKey

Returns the translation key of the given page.
Syntax
PAGE.TranslationKey
Returns
string

Usage

The TranslationKey method on a Page object returns the translation key of the given page. Hugo uses the translation key to relate translations of the same page to each other. Pages in different languages with the same translation key are translations of each other.

The translation key is the value of the translationKey field in front matter if set, otherwise the logical path of the page. Because the logical path does not include the language, pages with the same logical path in different languages are translations of each other. Set the translationKey field in front matter to relate translations that have different logical paths, such as pages with translated file names.

Examples

With this project configuration:

defaultContentLanguage: en
languages:
  de:
    contentDir: content/de
    label: Deutsch
    locale: de-DE
    weight: 2
  en:
    contentDir: content/en
    label: English
    locale: en-US
    weight: 1
defaultContentLanguage = 'en'
[languages]
  [languages.de]
    contentDir = 'content/de'
    label = 'Deutsch'
    locale = 'de-DE'
    weight = 2
  [languages.en]
    contentDir = 'content/en'
    label = 'English'
    locale = 'en-US'
    weight = 1
{
   "defaultContentLanguage": "en",
   "languages": {
      "de": {
         "contentDir": "content/de",
         "label": "Deutsch",
         "locale": "de-DE",
         "weight": 2
      },
      "en": {
         "contentDir": "content/en",
         "label": "English",
         "locale": "en-US",
         "weight": 1
      }
   }
}

And this content:

content/
├── de/
│   ├── books/
│   │   ├── buch-1.md
│   │   └── book-2.md
│   └── _index.md
└── en/
    ├── books/
    │   ├── book-1.md
    │   └── book-2.md
    └── _index.md

The English and German versions of Book 1 have different logical paths, /books/book-1 and /books/buch-1, so the front matter of each sets the same translation key:

---
title: Book 1
translationKey: book-1
---
+++
title = 'Book 1'
translationKey = 'book-1'
+++
{
   "title": "Book 1",
   "translationKey": "book-1"
}
---
title: Buch 1
translationKey: book-1
---
+++
title = 'Buch 1'
translationKey = 'book-1'
+++
{
   "title": "Buch 1",
   "translationKey": "book-1"
}

When rendering either version of Book 1:

{{ .TranslationKey }} → book-1

When rendering either version of Book 2, which has the same logical path in both languages and no translationKey field in front matter:

{{ .TranslationKey }} → /books/book-2