How to `GetPage` a specific file? (Multilingual mode)

In my front matter files I define custom information. I want to keep that information management easy by not replicating that information in each translated language.

My idea was to do:

{{ $person := .Site.GetPage (print "/en/about/" .File.LogicalName) }}
First name of person: {{ $person.Params.firstName }}

This, unfortunately, doesn’t work:

Failed to render pages: render of “page” failed: “C:\Temp\site\themes\theme2019\layouts\custom\person.html:1:1”: execute of template failed: panic in Execute: runtime error: invalid memory address or nil pointer dereference. See “Method invoke in templates panics if the receiver is nil · Issue #5327 · gohugoio/hugo · GitHub” for the reason why we cannot provide a better error message for this.

GetPage seems to default to the current language, since this works fine:

{{ $person := .Site.GetPage (print "/about/" .File.LogicalName) }}
First name of person: {{ $person.Params.firstName }}

But then I still have to replicate the front matter in each content file. It would be easier to have the English Markdown file serve as ‘master’, and each language pull the front matter from that file.

Can I achieve such a thing with Hugo?

I prefer using the front matter instead of data files in this case, since the front matter information is specific to the page (but not specific to the language).

Language settings:

defaultContentLanguage = "nl"
defaultContentLanguageInSubDir = true

    contentDir = "content/nl"
    contentDir = "content/en"

(etc for other languages)
Hugo Static Site Generator v0.54.0/extended windows/amd64 BuildDate: unknown

See this previous thread, it may help

1 Like

Thanks for the quick reply and finding that thread. :slight_smile:

I’ve fixed my problem, and this is the code I ended up using:

{{ $en := index (where .Sites "Language.Lang" "en") 0 }}

{{ $person := $en.GetPage (print "/about/" .File.LogicalName) }}