Conditionally show translation option

Borrowing from this example, is it possible to show the “Translations” option at the top of the page only if the default language page has a corresponding translated page in the secondary language folder?

I was thinking of doing translations to some content on a schedule but would like to post the translated pages as soon as they are done. But I don’t want the translations option to show up unless these pages have corresponding translated pages.

Yes, I will give you my example from my site.

I am using approach, when there is translation available the link serving same article in other language[1], where there is not, serving as translation using Google translate[2]. The second option can be left blank.

{{ if .IsTranslated }}
        {{ range .Translations }}
          <div class="hreflang">
            <a hreflang="{{ .Lang }}" href="{{ .Permalink }}">
              {{ T "LanguageTextReadIn" }} {{ T "LanguageTextShort" }}
              <img class="flag" src="{{ .Language.Params.flag }}" alt="{{ T "LanguageTextReadIn" }} {{ T "LanguageTextShort" }}" height="10" width="13">
            </a>
          </div>
        {{ end }}
        {{ else }}
        {{ $translatelink := .Permalink | absURL}}
          {{ range site.Home.Translations }}
            <div class="hreflang">
              <a href="http://translate.google.com/translate?js=n&sl=auto&tl={{ .Lang }}&u={{ $translatelink }}">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" height="10" width="13"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M12.87 15.07l-2.54-2.51.03-.03c1.74-1.94 2.98-4.17 3.71-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04zM18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12zm-2.62 7l1.62-4.33L19.12 17h-3.24z"/></svg>
                {{ T "TranslateTo" }} {{ T "TranslateLang" }}
                <img class="flag" src="{{ .Language.Params.flag }}" alt="{{ T "TranslateTo" }} {{ T "TranslateLang" }}" height="10" width="13">
              </a>
            </div>
          {{ end }}
        {{ end }}

  1. 10 things to do after installing/upgrading to Windows 11 (+1) – Dariusz Więckiewicz 🇬🇧 ↩︎

  2. A very Minimal Google Analytics 4 Snippet – Dariusz Więckiewicz 🇬🇧 ↩︎

I am thinking more of the language switch option does not appear completely when there is no translation.

From the example you orginally referenced, change .AllTranslations to .Translations

{{ with .Translations }}
  <p>Translations:
    {{ range . }}
      <a href="{{ .RelPermalink }}">{{ .Language.LanguageName }}</a>
    {{ end }}
  </p>
{{ end }}
2 Likes

Awesome!

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