After upgrade to 0.112 Page.Language.ContentDir undefined

I am following the discussion about the latest changes in Hugo 0.112 but didn’t explicitly read about this:

If I am building the exampleSite of my theme (GitHub - McShelby/hugo-theme-relearn: A theme for Hugo designed for documentation) with Hugo 0.112 I get an error trying to access .Language.ContentDir of a page.

Start building sites … 
hugo v0.112.3-ba6f74e40420d76f15fc8c2358be90f7aca98e0e windows/amd64 BuildDate=2023-05-24T14:42:50Z VendorInfo=gohugoio
Built in 811 ms
Error: error building site: "C:\Daten\Soren\hugo-theme-relearn\exampleSite\content\shortcodes\attachments.en.md:8:1": failed to render shortcode "attachments": failed to process shortcode: execute of template failed: template: shortcodes/attachments.html:2:4: executing "shortcodes/attachments.html" at <partial "shortcodes/attachments.html" (dict "context" .Page "color" (.Get "color") "content" .Inner "icon" (.Get "icon") "pattern" (.Get "pattern") "style" (.Get "style") "sort" (.Get "sort") "title" (.Get "title"))>: error calling partial: "C:\Daten\Soren\hugo-theme-relearn\layouts\partials\shortcodes\attachments.html:35:44": execute of template failed: template: partials/shortcodes/attachments.html:35:44: executing "partials/shortcodes/attachments.html" at <.Language.ContentDir>: can't evaluate field ContentDir in type *langs.Language

I did not explicitly set contentDir in my config.toml, but that wasnt an issue previously.

So is this a regression? What can I use instead?

Does it work with site.contentDir?

Sadly, no.

Start building sites … 
hugo v0.112.3-ba6f74e40420d76f15fc8c2358be90f7aca98e0e windows/amd64 BuildDate=2023-05-24T14:42:50Z VendorInfo=gohugoio
Built in 842 ms
Error: error building site: "C:\Daten\Soren\hugo-theme-relearn\exampleSite\content\shortcodes\attachments.en.md:8:1": failed to render shortcode "attachments": failed to process shortcode: execute of template failed: template: shortcodes/attachments.html:2:4: executing "shortcodes/attachments.html" at <partial "shortcodes/attachments.html" (dict "context" .Page "color" (.Get "color") "content" .Inner "icon" (.Get "icon") "pattern" (.Get "pattern") "style" (.Get "style") "sort" (.Get "sort") "title" (.Get "title"))>: error calling partial: "C:\Daten\Soren\hugo-theme-relearn\layouts\partials\shortcodes\attachments.html:35:35": execute of template failed: template: partials/shortcodes/attachments.html:35:35: executing "partials/shortcodes/attachments.html" at <site>: can't evaluate field contentDir in type interface {}

Use $.hugo.workingDir instead.

That doesn’t work either.

According to the docs $.hugo.workingDir returns eg. /home/user/projects/my-hugo-site.

What I need is the relative path (eg. content/english) that may be configured in the hugo.toml for multilang configurations - note, that this setting is according to the docs is optional.

So basically I need a way, to check if page.Language.ContentDir is set.

Just seen yet, that you’ve submitted a PR. Thanks for that. Will test that out although I’m not sure why this works :crazy_face:

Correct me if I’m wrong, but when I review your partial it looks like ultimately you want the path to the content file from the root of your project. For example:

/content/en/posts/post-1.md

If my assessment is correct, you can get that with:

{{ strings.TrimPrefix hugo.WorkingDir .File.Filename }}

I am surprised that the contentDir configuration value was exposed to the templates in earlier versions. It shouldn’t be.

1 Like

Thats correct. I am still in the process of testing. Especially because I haven’t had a test case for Translation by content directory.

Thanks to all of you, this should be enough information to fix this nasty one.

Note that $.hugo.workingDir isn’t a thing. It’s hugo.WorkingDir.

A post was split to a new topic: Error: can’t evaluate field ContentDir in type *langs.Language

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.