I have multiple languages on the website and plan to add a link from the non-English to the English page.
I use the code below and am aware that it is not efficient.
What is a better way to get it? Thank you.
{{ if and ( ne .Page.Lang "en" ) .IsTranslated}}
{{ $pageLang := .Page.Lang}}
{{ range .Translations }}
{{ $translation := . }}
{{ if eq $translation.Lang "en"}}
<li>
<a href="{{ .Permalink }}"><span class="flag-icon flag-icon-gb"></span></a>
</li>
{{ end }}
{{ end }}
{{ end }}
If English is the first defined language you can use for example: if ne .Language .Sites.First.Language
and <a href="{{ .Sites.First.Home.RelPermalink }}">...</a>
To me that looks effective. If Go templates had a break statement (it’s an open issue about it on the Go issue tracker), this would pretty much match how a TranslationByLang function in native Go. So I would just stick with what you got.