| 1 |
--- |
| 2 |
title: TranslationKey |
| 3 |
description: Returns the translation key of the given page. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [PAGE.TranslationKey] |
| 10 |
--- |
| 11 |
|
| 12 |
The translation key creates a relationship between all translations of a given page. The translation key is derived from the file path, or from the `translationKey` parameter if defined in front matter. |
| 13 |
|
| 14 |
With this project configuration: |
| 15 |
|
| 16 |
{{< code-toggle file=hugo >}} |
| 17 |
defaultContentLanguage = 'en' |
| 18 |
|
| 19 |
[languages.en] |
| 20 |
contentDir = 'content/en' |
| 21 |
label = 'English' |
| 22 |
locale = 'en-US' |
| 23 |
weight = 1 |
| 24 |
|
| 25 |
[languages.de] |
| 26 |
contentDir = 'content/de' |
| 27 |
label = 'Deutsch' |
| 28 |
locale = 'de-DE' |
| 29 |
weight = 2 |
| 30 |
{{< /code-toggle >}} |
| 31 |
|
| 32 |
And this content: |
| 33 |
|
| 34 |
```tree |
| 35 |
content/ |
| 36 |
├── de/ |
| 37 |
│ ├── books/ |
| 38 |
│ │ ├── buch-1.md |
| 39 |
│ │ └── book-2.md |
| 40 |
│ └── _index.md |
| 41 |
├── en/ |
| 42 |
│ ├── books/ |
| 43 |
│ │ ├── book-1.md |
| 44 |
│ │ └── book-2.md |
| 45 |
│ └── _index.md |
| 46 |
└── _index.md |
| 47 |
``` |
| 48 |
|
| 49 |
And this front matter: |
| 50 |
|
| 51 |
{{< code-toggle file=content/en/books/book-1.md fm=true >}} |
| 52 |
title = 'Book 1' |
| 53 |
translationKey = 'foo' |
| 54 |
{{< /code-toggle >}} |
| 55 |
|
| 56 |
{{< code-toggle file=content/de/books/buch-1.md fm=true >}} |
| 57 |
title = 'Buch 1' |
| 58 |
translationKey = 'foo' |
| 59 |
{{< /code-toggle >}} |
| 60 |
|
| 61 |
When rendering either either of the pages above: |
| 62 |
|
| 63 |
```go-html-template |
| 64 |
{{ .TranslationKey }} → page/foo |
| 65 |
``` |
| 66 |
|
| 67 |
If the front matter of Book 2, in both languages, does not include a translation key: |
| 68 |
|
| 69 |
```go-html-template |
| 70 |
{{ .TranslationKey }} → page/books/book-2 |
| 71 |
``` |