HUGO

The efficient way to get the URL of the English-translated page

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>

Also see:

Thanks.
But .Sites.First.Home.RelPermalink points to the home page of the first defined language but not the post page.

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.

Great. I am happy that the solution is acknowledged.
Thanks.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.