HUGO
Menu
GitHub 87110 stars Mastodon

AllTranslations

Returns all translations of the given page, including the current language, sorted by language weight then language name.

Syntax

PAGE.AllTranslations

Returns

page.Pages

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
  fr:
    contentDir: content/fr
    label: Français
    locale: fr-FR
    weight: 3
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
  [languages.fr]
    contentDir = 'content/fr'
    label = 'Français'
    locale = 'fr-FR'
    weight = 3
{
   "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
      },
      "fr": {
         "contentDir": "content/fr",
         "label": "Français",
         "locale": "fr-FR",
         "weight": 3
      }
   }
}

And this content:

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

And this template:

{{ with .AllTranslations }}
  <ul>
    {{ range . }}
      <li>
        <a href="{{ .RelPermalink }}" hreflang="{{ .Language.Locale }}">{{ .LinkTitle }} ({{ or .Language.Label .Language.Name }})</a>
      </li>
    {{ end }}
  </ul>
{{ end }}

Hugo will render this list on the “Book 1” page of each site:

<ul>
  <li><a href="/books/book-1/" hreflang="en-US">Book 1 (English)</a></li>
  <li><a href="/de/books/book-1/" hreflang="de-DE">Book 1 (Deutsch)</a></li>
  <li><a href="/fr/books/book-1/" hreflang="fr-FR">Book 1 (Français)</a></li>
</ul>

On the “Book 2” page of the English and German sites, Hugo will render this:

<ul>
  <li><a href="/books/book-1/" hreflang="en-US">Book 1 (English)</a></li>
  <li><a href="/de/books/book-1/" hreflang="de-DE">Book 1 (Deutsch)</a></li>
</ul>