HUGO
Menu
GitHub 90529 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. The translation key relates all translations of a page to each other. If you set the translationKey field in front matter, this method returns its value. Otherwise, it returns the page’s logical path. This method is rarely used within a template.

Hugo identifies each page by its logical path, so pages with different logical paths remain different pages, even if they share a translation key. When you fill in missing translations, each one fills the gap left by the other. Instead of setting a translation key, give each translation the same path, and set the slug in front matter to translate the URL.

Examples

These examples use 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
      }
   }
}

The project contains this content:

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

The front matter of Book 1 sets the same translation key in both languages:

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

Rendering either Book 1 page:

{{ .TranslationKey }} → foo

Rendering either Book 2 page, where front matter does not set a translation key:

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