<p>The comparison is meant to be used within the for loop that loops your array of locales. And you can of course only compare the current locale tag <code>craft.locale</code> to real locale tags and not with fake ones like 'en_001' that Craft doesn't know about.</p>

<pre><code>{% set locales = ['en', 'en_gb'] %}

{% for locale in locales %}
    {% set current = (locale == craft.locale) ? true %}
{% endfor %}
</code></pre>

<p>So you need to find a way to map real locales to your fake ones! The real ones for your <code>craft.locale</code> comparison and the fake ones to get your territories. But then there would be no need to compose fake ones to start with and you would simply map the real locales you installed and use in Craft to the territory names or territory codes for your output string. So let's do this.</p>

<pre><code>{% set locales = {
    'en':    '001',
    'en_gb': 'gb',
    'hu':    'sk',
    'de':    'sk',
} %}

{% for locale, territory in locales %}

    {% set current = (locale == craft.locale) ? true %}

    {% set territory = craft.i18n.getLocaleData(craft.locale).getTerritory(territory) %}
    {% set language = craft.i18n.getLocaleById(locale).name %}
    {% set language = language|replace("/- #{territory}$/", '') %}

    {% if territory and language %}
        &lt;li class="{{ current ? 'myCurrentLocale'}}"&gt;
            Territory: {{ territory }},
            Language: {{ language }}
        &lt;/li&gt;
    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-03-23T18:34:50.933Z
I was thinking that it should be within the for loop. I moved it there, but I've encountered on a problem when `if locale == craft.locale` evaluates to `false`. So variable `current` doesn't work in this case. Actually `locales` are  joined with territories. You can see it in the code -> `ul` list. I think, we can't compare `if locale` to `craft.locale`.
Dominik Krulak
CC BY-SA 3.0
2015-03-23T23:17:50.977Z
You could loop `locales` twice.
carlcs
CC BY-SA 3.0
2015-03-24T08:22:07.673Z
I've tried to loop twice, but wouldn't that generate the same result twice, If we loop by the same element criteria model? Again I'm getting, variable `current` doesn't exists.
Dominik Krulak
CC BY-SA 3.0
2015-03-24T08:58:56.373Z
Use the code I posted to set `current` and in the second loop you could check for it like so `{{ current is defined ? 'myCurrentClass' }}`.
carlcs
CC BY-SA 3.0
2015-03-24T09:32:58.347Z
I've updated my question, with only part, I'm actually struggling with. I did what you said, I think right, but with the same effect. The update is as It is now, with no implementation from your suggestion.
Dominik Krulak
CC BY-SA 3.0
2015-03-24T13:27:33.783Z
`001` is a territory. From the current code in question I get list of languages and territories like: English (World), British English (United Kingdom) ... If I exclude key `001` I won't get my expected result. If I wouldn't need territories, I wouldn't post that question, because It would work.
Dominik Krulak
CC BY-SA 3.0
2015-03-24T13:38:53.010Z
But Craft never returns a locale 'en_001' so you won't ever get that matched!
carlcs
CC BY-SA 3.0
2015-03-24T13:48:35.470Z
Any suggestion How to circumvent this, and keep both?
Dominik Krulak
CC BY-SA 3.0
2015-03-24T14:26:15.063Z
I found the bug @Carlcs. It was in my general.php. I don't know why, but I had in `siteUrl` array `en_001 => '/'` Your code is perfectly working, like a charm :)
Dominik Krulak
CC BY-SA 3.0
2015-03-23T17:57:57.517Z
2015-03-24T13:57:27.953Z
2015-03-24T13:57:27.953Z
carlcs
<p>I've worked up this code with big help from @carlcs.
I want to set up a <code>class</code> if current locale is turned on. </p>

<ul>
<li>Firstly, I tried the easiest way by putting <code>{{ current ? 'my-class' }}</code> into <code>li</code>.</li>
<li>Secondly, I tried to set variable for current locale.</li>
</ul>

<p>I've end up by struggling to answer my question, Why all locales are turned on?</p>

<pre><code>{# Language switcher #}
{% set locale = craft.i18n.getCurrentLocale() %}

{# Configure site locales #}
{% set locales = ['en', 'en_gb'] %}

{# Check if locale equals the requested page locale #}
{% if locale == craft.locale %}
    {% set current = true %}
{% else %}
    {% set current = false %}
{% endif %}

{# Give locales class or text #}   
{% if locale == 'en' %}
     {% set territoryCode = 'world' %}
     {% set active = 'uk-active' %}    
{% endif %}

{% if locale == 'en_gb' %}
     {% set territoryCode = 'uk' %} 
     {% set active = 'uk-active' %}     
{% endif %}  


{# This is the current locale #}
&lt;span&gt;{{ territoryCode|upper }}&lt;/span&gt;

&lt;ul&gt;
{% set locales = ['en_001', 'en_gb'] %}
{% for locale in locales %}
{% set subTags = locale|split('_') %}
{% set territory = subTags|length &gt; 1 ?craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
{% set language = craft.i18n.getLocaleById(locale).name %}
{% set language = language ?: craft.i18n.getLocaleById(subTags|first).name %}
{% set language = language|replace("/- #{territory}$/", '') %}    

&lt;li {% if active %}class="{{ active }}{% endif %}"&gt;
    &lt;a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}"&gt;
       {% if territory and language %}
            {{ territory }} ({{ language }})
       {% endif %}
    &lt;/a&gt;
&lt;/li&gt;
{% endfor %} 
&lt;/ul&gt;
</code></pre>

<p><strong>Update</strong></p>

<pre><code>&lt;ul&gt;
{% set locales = ['en_001', 'en_gb'] %}  

 {% for locale in locales %}    

 {# Check if locale equals the requested page locale #}
 {% if locale == craft.locale %}
      {% set current = true %}
 {% else %}
      {% set current = false %}        
 {% endif %}

 {% set subTags = locale|split('_') %}
 {% set territory = subTags|length &gt; 1 ? craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
 {% set language = craft.i18n.getLocaleById(locale).name %}
 {% set language = language ?: craft.i18n.getLocaleById(subTags|first).name %}
 {% set language = language|replace("/- #{territory}$/", '') %}    


 &lt;li class="{{ current is defined ? 'uk-active' }}"&gt;
     &lt;a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}"&gt;
        {% if territory and language %}
            {{ territory }} ({{ language }})
        {% endif %}
     &lt;/a&gt;
 &lt;/li&gt;
 {% endfor %} 
&lt;/ul&gt;
</code></pre>

<localization>
Add active class and territory name to language switcher
# Add active class and territory name to language switcher

(Asked by: username_0 on 2015-03-23)

I've worked up this code with big help from @username_1.
I want to set up a class if current locale is turned on. 

Firstly, I tried the easiest way by putting {{ current ? 'my-class' }} into li.
Secondly, I tried to set variable for current locale.

I've end up by struggling to answer my question, Why all locales are turned on?
{# Language switcher #}
{% set locale = craft.i18n.getCurrentLocale() %}

{# Configure site locales #}
{% set locales = ['en', 'en_gb'] %}

{# Check if locale equals the requested page locale #}
{% if locale == craft.locale %}
    {% set current = true %}
{% else %}
    {% set current = false %}
{% endif %}

{# Give locales class or text #}   
{% if locale == 'en' %}
     {% set territoryCode = 'world' %}
     {% set active = 'uk-active' %}    
{% endif %}

{% if locale == 'en_gb' %}
     {% set territoryCode = 'uk' %} 
     {% set active = 'uk-active' %}     
{% endif %}  


{# This is the current locale #}
{{ territoryCode|upper }}


{% set locales = ['en_001', 'en_gb'] %}
{% for locale in locales %}
{% set subTags = locale|split('_') %}
{% set territory = subTags|length > 1 ?craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
{% set language = craft.i18n.getLocaleById(locale).name %}
{% set language = language ?: craft.i18n.getLocaleById(subTags|first).name %}
{% set language = language|replace("/- #{territory}$/", '') %}    


    
       {% if territory and language %}
            {{ territory }} ({{ language }})
       {% endif %}
    

{% endfor %} 


Update

{% set locales = ['en_001', 'en_gb'] %}  

 {% for locale in locales %}    

 {# Check if locale equals the requested page locale #}
 {% if locale == craft.locale %}
      {% set current = true %}
 {% else %}
      {% set current = false %}        
 {% endif %}

 {% set subTags = locale|split('_') %}
 {% set territory = subTags|length > 1 ? craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
 {% set language = craft.i18n.getLocaleById(locale).name %}
 {% set language = language ?: craft.i18n.getLocaleById(subTags|first).name %}
 {% set language = language|replace("/- #{territory}$/", '') %}    


 
     
        {% if territory and language %}
            {{ territory }} ({{ language }})
        {% endif %}
     
 
 {% endfor %}

**Tags:** localization


---

## 1 answers

### Answer by username_1 on 2015-03-23. Score: 2 | Accepted answer

The comparison is meant to be used within the for loop that loops your array of locales. And you can of course only compare the current locale tag craft.locale to real locale tags and not with fake ones like 'en_001' that Craft doesn't know about.
{% set locales = ['en', 'en_gb'] %}

{% for locale in locales %}
    {% set current = (locale == craft.locale) ? true %}
{% endfor %}

So you need to find a way to map real locales to your fake ones! The real ones for your craft.locale comparison and the fake ones to get your territories. But then there would be no need to compose fake ones to start with and you would simply map the real locales you installed and use in Craft to the territory names or territory codes for your output string. So let's do this.
{% set locales = {
    'en':    '001',
    'en_gb': 'gb',
    'hu':    'sk',
    'de':    'sk',
} %}

{% for locale, territory in locales %}

    {% set current = (locale == craft.locale) ? true %}

    {% set territory = craft.i18n.getLocaleData(craft.locale).getTerritory(territory) %}
    {% set language = craft.i18n.getLocaleById(locale).name %}
    {% set language = language|replace("/- #{territory}$/", '') %}

    {% if territory and language %}
        
            Territory: {{ territory }},
            Language: {{ language }}
        
    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: I was thinking that it should be within the for loop. I moved it there, but I've encountered on a problem when `if locale == craft.locale` evaluates to `false`. So variable `current` doesn't work in this case. Actually `locales` are  joined with territories. You can see it in the code -> `ul` list. I think, we can't compare `if locale` to `craft.locale`.  
> **username_1**: You could loop `locales` twice.  
> **username_0**: I've tried to loop twice, but wouldn't that generate the same result twice, If we loop by the same element criteria model? Again I'm getting, variable `current` doesn't exists.  
> **username_1**: Use the code I posted to set `current` and in the second loop you could check for it like so `{{ current is defined ? 'myCurrentClass' }}`.  
> **username_0**: I've updated my question, with only part, I'm actually struggling with. I did what you said, I think right, but with the same effect. The update is as It is now, with no implementation from your suggestion.  
> **username_0**: `001` is a territory. From the current code in question I get list of languages and territories like: English (World), British English (United Kingdom) ... If I exclude key `001` I won't get my expected result. If I wouldn't need territories, I wouldn't post that question, because It would work.  
> **username_1**: But Craft never returns a locale 'en_001' so you won't ever get that matched!  
> **username_0**: Any suggestion How to circumvent this, and keep both?  
> **username_0**: I found the bug @Carlcs. It was in my general.php. I don't know why, but I had in `siteUrl` array `en_001 => '/'` Your code is perfectly working, like a charm :)  

---

