# \[SOLVED\] Use If condition with current active language

**URL:** <https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196>\
**Category:** support\
**Created:** [May 30, 2018, 5:03pm UTC](https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196 "2018-05-30T17:03:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![menelik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/menelik/32/6082_2.png) [@menelik](https://discourse.gohugo.io/u/menelik)\
**Post date:** [May 30, 2018, 5:03pm UTC](https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196/1 "2018-05-30T17:03:50Z")

</div>

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 🙂 )

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

Any help is really appreciated.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [May 30, 2018, 5:14pm UTC](https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196/2 "2018-05-30T17:14:05Z")

</div>

> [@menelik](#):
>
> {{ range .AllTranslations }} {{ if eq .Site.Language.LanguageName “English” }} \<a href=“{{ .Permalink }}”\>{{ .Language.LanguageName }}\</a\> {{ end }} {{ end }}

```auto
{{ 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](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.

---

<div class="post-metadata">

**Author:** ![menelik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/menelik/32/6082_2.png) [@menelik](https://discourse.gohugo.io/u/menelik)\
**Post date:** [May 30, 2018, 5:30pm UTC](https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196/3 "2018-05-30T17:30:00Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [May 30, 2018, 5:30pm UTC](https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196/4 "2018-05-30T17:30:37Z")

</div>

> [@menelik](#):
>
> {{ if eq .Site.Language.LanguageName “English” }}

Try “$.Site”.

---

<div class="post-metadata">

**Author:** ![menelik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/menelik/32/6082_2.png) [@menelik](https://discourse.gohugo.io/u/menelik)\
**Post date:** [May 30, 2018, 6:01pm UTC](https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196/5 "2018-05-30T18:01:47Z")

</div>

@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](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/5/5372b6084e5d0a1fd32550060e866e4b6a9762dc.png)

If current language is Thai:  
 ![38](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/8/8049bec8b972bf764f811ba06235ef0a495ddca0.png)

Thanks again for your help, really appreciate it 🙂

---

<div class="post-metadata">

**Author:** ![cerrone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cerrone/32/7764_2.png) [@cerrone](https://discourse.gohugo.io/u/cerrone)\
**Post date:** [February 4, 2019, 11:19am UTC](https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196/6 "2019-02-04T11:19:16Z")

</div>

Worked for me as well without this line though:

```auto
{{ range .AllTranslations }}
…
{{ end }}

```

Why did you add this line at the top?

---

<div class="post-metadata">

**Author:** ![fathurrohman26](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fathurrohman26/32/14794_2.png) [@fathurrohman26](https://discourse.gohugo.io/u/fathurrohman26)\
**Post date:** [July 14, 2021, 7:14pm UTC](https://discourse.gohugo.io/t/solved-use-if-condition-with-current-active-language/12196/7 "2021-07-14T19:14:15Z")

</div>

How about this?

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

```
