1

I've worked up this code with big help from @carlcs. 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 #}
<span>{{ territoryCode|upper }}</span>

<ul>
{% 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}$/", '') %}    

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

Update

<ul>
{% 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}$/", '') %}    


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

1 Answer 1

2

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 %}
        <li class="{{ current ? 'myCurrentLocale'}}">
            Territory: {{ territory }},
            Language: {{ language }}
        </li>
    {% endif %}

{% endfor %}
9
  • 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. Mar 23, 2015 at 18:34
  • You could loop locales twice.
    – carlcs
    Mar 23, 2015 at 23:17
  • 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. Mar 24, 2015 at 8:22
  • 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
    Mar 24, 2015 at 8:58
  • 1
    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 :) Mar 24, 2015 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.