I’m trying to set up a language switcher that fulfils these requirements:
- the order of the languages should be permanent
- if there isn’t a translation for that piece of content in the alternate language the button should be disabled or show the result of the Lang.Merge
So far, I came up with this:
{{ if .IsHome }}
<li><a href="/pt" class="{{- if eq (string .Lang) "pt" -}}active{{- end -}}">Portuguese</a></li>
<li><a href="/en" class="{{- if eq (string .Lang) "en" -}}active{{- end -}}">English</a></li>
{{ else }}
<li><a href="/pt{{ .Permalink | relURL }}" class="{{- if eq (string .Lang) "pt" -}}active{{- end -}}">Portuguese</a></li>
<li><a href="/en{{ .Permalink | relURL }}" class="{{- if eq (string .Lang) "en" -}}active{{- end -}}">English</a></li>
{{ end }}
But of course, this breaks when there isn’t a translation and is too hard coded. I also tried to range through the translations, but that had two problems:
- the order of languages would change
- if there wasn’t an available translation, it would only show one button.
I’m a bit lost as to how to cycle through all the available languages and set the href appropriately.
Is there something I’m missing?