Ready for a happy ending?
After some hours of testing I had to abandon the idea of referring to (T "i18nCategories" | lower)
when iterating documents.
Instead I gave .Site.AllPages
a go. This function do make iteration of documents across the language-boundary. But it seems to catch pages, nodes and everything, which at least in my case threw a lot of errors, that was difficult to incapsulate. Besides .Site.AllPages
seems like a lot of work - even for Go.
So I went off-piste. .Site.AllPages
works fine. So does .Site.RegularPages
, which cut of all the overhead of pages.
How about .Site.AllRegularPages
? It’s not documentet, and a search in this forum returns 0 hits.
But bingo! It works like a charm!
This function is a real hit. Forget all about Hugo/static site generators being targetet for personal blogs and smaller websites. Iteration and extraction of anything from anywhere is full blown content management in my book.
Now we just miss a real nice webinterface for editing and managing the markdown-files, that Hugo generates from.
Here is the code for anyone, who needs to break down the barriers between languages in a i18n Hugo-setup:
{{ $lang := .Params.lang }}
{{ $field := trim (T "i18nCategories" | lower) "" }}
{{ $key := trim (delimit (index .Params $field) "") "" }}
{{- range .Site.AllRegularPages -}}
{{ $langcompare := .Params.lang }}
{{ if eq $langcompare "en" }}
{{ $.Scratch.Set "fieldcompare" "categories" }}
{{ else }}
{{ $.Scratch.Set "fieldcompare" "kategorier" }}
{{ end }}
{{ if ne $lang $langcompare }}
{{ $keycompare := (trim (delimit (index .Params ($.Scratch.Get "fieldcompare")) "") "") }}
{{ if eq $key $keycompare }}
{{ $.Scratch.Add "keymulticompare" "Y" }}
{{ else }}
{{ $.Scratch.Add "keymulticompare" "N" }}
{{ end }}
{{ end }}
{{ end }}
{{ if in ($.Scratch.Get "keymulticompare") "Y" }}
{{ $.Scratch.Set "keycompare" true }}
{{ else }}
{{ $.Scratch.Set "keycompare" false }}
{{ end }}
And in the end - this module to bring in the HTML, that depends on the result of traversing the documents in the other languages:
{{ if ($.Scratch.Get "keycompare") }}
The actual code tests, if there are any documents at hand with the same category but in the other language. When yes - a direct link to the particular node is added.