[SOLVED] Use If condition with current active language

Hi everyone,

I am still new to Hugo and kinda struggle with the GO syntax. I try to create an if condition based on the current active language. According to the Hugo docs I can use .Site.Language.LanguageName to get the current active language but somehow my if statement is ignored and the link is always shown.

{{ range .AllTranslations }}
     {{ if eq .Site.Language.LanguageName "English" }}
           <a href="{{ .Permalink }}">{{ .Language.LanguageName }}</a>
      {{ end }}
 {{ end }}

What is wrong with this condition? (I guess it must be a Hugo newbie issue :slight_smile: )

If I use {{ .Site.Language.LanguageName }} in my template for troubleshooting the active language is displayed correctly.

Any help is really appreciated.

{{ range .AllTranslations }}
     {{ if eq .Language $.Language }}
           <a href="{{ .Permalink }}">{{ .Language.LanguageName }}</a>
      {{ end }}
 {{ end }}

Not sure what you want, but the above may be give you a hint. Also see https://github.com/letsencrypt/website/blob/master/layouts/partials/header.html#L33

Which builds language nav for the home page only.

There are written lots here and elsewhere about Go templates and scopes and the “dot”, so I will not go into detail.

Thank you @bep for the example. Unfortunately your code doesn’t work either. I try to make a custom language switcher which has different code snippets depending on the language (Thai or English). So basically I try to create an if statement which will show a certain code only if the current active language is English and another code snipped if the language is Thai.

                    {{ range .AllTranslations }}
                         {{ if eq .Site.Language.LanguageName "English" }}
                           CODE FOR ENGLISH LANGUAGE
                        {{ else }}
                            CODE FOR THAI LANGUAGE
                        {{ end }}
                    {{ end }}

Any idea why my if conditions doesnt work?

{{ if eq .Site.Language.LanguageName "English" }}

Appreciate every help as I already have wasted more then an hour to get this simple thing working :slight_smile:

Try “$.Site”.

@bep You pointed me into the right direction. I finally got it working, thank you!

I will leave my final code here in in case someone else struggles with the same problem:

{{ range .AllTranslations }}
     {{ if eq $.Site.Language.LanguageName "English" }}
        {{ if eq .Language.LanguageName "ไทย" }}
            <a class="header-nav__main-menu-link header-nav__main-menu-link--language" href="{{ .Permalink }}">
                <span>English</span>
                <span class="header-nav__main-menu-link--language-switch">
                    <span class="header-nav__main-menu-link--language-slider header-nav__main-menu-link--language-slider-english"></span>
                </span>
                <span>ไทย</span>
            </a>
        {{ end }}
    {{end }}
    {{ if eq $.Site.Language.LanguageName "ไทย" }}
        {{ if eq .Language.LanguageName "English" }}
            <a class="header-nav__main-menu-link header-nav__main-menu-link--language" href="{{ .Permalink }}">
                <span>English</span>
                <span class="header-nav__main-menu-link--language-switch">
                    <span class="header-nav__main-menu-link--language-slider header-nav__main-menu-link--language-slider-thai"></span>
                </span>
                <span>ไทย</span>
            </a>
        {{ end }}
    {{ end }}
{{ end }}

Which will give you a simple language switcher with a single link pointing to the inactive language.

If current language is English:
33

If current language is Thai:
38

Thanks again for your help, really appreciate it :slight_smile:

2 Likes

Worked for me as well without this line though:

{{ range .AllTranslations }}
…
{{ end }}

Why did you add this line at the top?

How about this?

{{ cond (eq $.Lang "id" ) "id" "en" }}