HUGO

GetPage with translated ContentDirs

Hello,

I’m using hugo to create a multilingual site but I have a small problem I don’t know how to solve. I want to include a “Contact me” button on the site and it should link to the respective contact form. This is how I structured my content:

content
|--de
   |--kontakt
      |--index.md
|--en
   |--contact
      |--index.md

I now want to link to $baseURL/kontakt on the german version and $baseURL/contact on the english version. How can I achieve this with .Site.GetPage given the current content structure?

Thanks in advance

I had a similar issue recently and only managed to get it to work by using translation by filename. Not sure if it’s a bug or a config issue.

If you want/need to have different paths (kontakt vs contact) you can link them using translationKey in front matter.

The different paths are for more manageable translations (instead of having lots of files next to each other and setting all required things in front matter I directly put them into translated directories which will help me find the files) so keeping it will be a bonus. I’ve read about translationKey earlier but I wasn’t sure if it was what I wanted so I will give it a try.

I tried to used translationKey but it seems not to work for my use-case. Using this construct only print nopPage:

{{ $contact := .Site.GetPage "contact" }}
{{ print $contact }}

What am I doing wrong?

I now created a partial as workaround which does what I want. It’s probably not the best but at least it works.

Here’s the code for the partial:

{{ $page :=  index (where ._global.Site.RegularPages "TranslationKey" "eq" (printf "%s/%s" "page" .Key)) 0 }}
{{ $displayName := .DisplayName }}
{{ with $page }}
<a href="{{ .RelPermalink }}">{{ $displayName }}</a>
{{ else }}
<span style="color:red">No page with TranslationKey <strong>{{ .Key }}</strong> exists!</span>
{{ end }}

Usage:

{{ partial "helper/translatedPageLink" (dict "Key" "yourTranslationKey" "DisplayName" "VisibleTextOfTheLink" "_global" . ) }}

It’s important to leave the _global key in otherwise the partial can’t access the global scope and thus can’t find the file. The partial also only works for regular pages, not sections and anything else.

I’ll consider opening an issue on github for this later