I’m trying to build a multilingual website with Hugo, and one of my problems is that I need to check whether the current page (in a list) is available in a specific language. So I’m doing this:
{{ range ... }}
{{ $page := . }}
<li><a href="{{ .Permalink }}">
{{- range .Site.Languages }}<span class="post_{{ cond (in $page.AllTranslations .) .Lang `none` }}"></span>{{ end -}}
{{ .Title }}
</a></li>
{{ end }}
I want that for every site language a span be created with either language name (post_en
, post_fr
etc) or post_none
, depending on the translation availability for this page.
The problem is that the condition in $page.AllTranslations .
is never true, apparently because I’m comparing objects.
An alternative would be to convert $page.AllTranslations
to a list of language codes, but I failed to find an appropriate function for that. The closest call would be
apply $page.AllTranslations "index" ".Lang"
But that doesn’t seem to work because index
cannot resolve attributes of an object.
Essentially I want to map the map’s content, like I would in Java (pseudocode):
allTranslations.stream()
.map(page -> page.getLang())
.collect(Collectors.toList())
My current approach, which does work, is to make a list of language codes to check against:
<!-- Compile a list of languages the current page is translated to -->
{{ $pageLangs := slice .Lang }}
{{ range .Translations }}{{ $pageLangs = $pageLangs | append .Lang }}{{ end }}
But it feels rather lame to make a list in this way. Any ideas? Is there any other function I can give apply
?