How to get data from sibling i18n-sites [solved]

Working on a multi-language site I now bump into the challenge of cross reference to the sibling-sites.

It seems, that .Site.Pages and .Site.RegularPages only refer to the current site.

How is it possible to get across the boundary and count or fetch some documents in one of the other languages?

I need this feature in order to put a link from the actual document to the same section in the other languages - in case they contain documents in the relevant section.

The rest is possible thanks to the i18n-files, where you can get the translation of the section name and use this as a lookup key.

But an easy gate - like .Site.Pages.[lang] - would be really nice.

Did you try .Site.AllPages?

Also note that every page has:

  • IsTranslated
  • Translations (other languages, will be empty if IsTranslated = false)
  • AllTranslations (includes the page you’re on)

Doh. Missed that one. I’ll give it a shot right away. Thank you for the really quick response - 5 min…:wink:

1 Like

Well. Just went ahead. But still no bingo on my Hugo 0.18.1.

     {{ $langs := slice .Params.lang }}
      {{ $field := T "i18nCategories" | lower }}
      {{ $keys := index .Params $field }}
      {{- range .Site.AllPages -}}
     {{- $page := . -}}
      {{ $fieldcompare := T "i18nCategories" | lower }}
    {{ $keyscompare := index .Params $fieldcompare }}
     {{ $common_tags := intersect $keys $keyscompare }}
     {{ $.Scratch.Add "SCcommon_tags" (delimit $common_tags "-") }}
    {{ $has_common_tags := $common_tags | len | lt 0 }}

For the first time I managed to get Hugo to actually freeze when generating… The line with $fieldcompare did the job, even though the same syntax works fine above in $field.

This is about comparing the content of taxonomies in different languages - but with terms, that are in one language! To be specific I use the taxonomy categories for storing the name of the website (=term). The websitename is the same, even though categories is kategorier in danish.

So I need the translation between categories and kategorier, which is defined in T i18nCategories. But it seems, that I’m not iterating the other pages in $page. At least the translated name of Categories doesn’t seem to be availble from there.

I’m may be out of scope. But I’m certainly in too deep here. Suggestions. Anyone.

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.

1 Like