# Custom language switcher / Language switching

**URL:** <https://community.hubspot.com/t/custom-language-switcher-language-switching/14291>\
**Category:** CMS Development\
**Created:** [May 9, 2019, 10:18am UTC](https://community.hubspot.com/t/custom-language-switcher-language-switching/14291 "2019-05-09T10:18:09Z")\
**Posts on this page:** 6\
**Page:** 3

<div class="post-metadata">

**Author:** ![FelTigre](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/feltigre/32/28305_2.png) [@FelTigre](https://community.hubspot.com/u/FelTigre)\
**Post date:** [March 4, 2021, 4:43pm UTC](https://community.hubspot.com/t/custom-language-switcher-language-switching/14291/41 "2021-03-04T16:43:54Z")

</div>

Hello 🙂  
i’m trying to create a switcher language on blog but i can’t sort the languages by country.

My switcher :

 ![39048iF6A19F37C169C171.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/f/f3b876b22c932aa75abde2dbc14f9114d3daa238.png)

and what i want :

 ![39049iA06AD067CEA390F1.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/0/6/0665a29eb3c308a186659e5c31d6680ffe779c6e.png)

```lia-code-sample
{% elif group.translations %}

 <ul class="dropdown menu country-list" data-dropdown-menu>

 {% set currLang = locale_name(locale) %}
 {% set codesLang = {'English' : 'en', 'Français - France' : 'fr-fr', 'Français' : 'fr' ,'Français - Luxembourg' : 'fr-lu', 'Français - Belgique' : 'fr-be', 'Nederlands' :'nl', 'Nederlands - België' || 'Nederlands - Belgique' : 'nl-be' } %}

 <li class="">
 <a class="lang-select" href="#" data-country="{{ codesLang[currLang]| truncate (2, true, '') }}" data-lang="{{ codesLang[currLang] }}">
 <img class="lang-flag" src="https://ressources.birdee.co/hubfs/Media/Common/{{ codesLang[currLang] }}.svg">
 {{ currLang }}
 </a>
 </li>

 {% for item in group.translations|sort(False, False, 'language') %}

 {% if item.language == 'fr' %}
 {% set name = 'Français' %}
 {% set country = 'France' %}
 {% set data = 'fr' %}
 {% elif item.language == 'fr-fr' %}
 {% set name = 'Français - France' %}
 {% set country = 'France' %}
 {% set data = 'fr' %}
 {% elif item.language == 'en' %}
 {% set name = 'English' %}
 {% set country = 'Europe' %}
 {% set data = 'eu' %}
 {% elif item.language == 'fr-be' %}
 {% set name = 'Français - Belgique' %}
 {% set country = 'Belgique' %}
 {% set data = 'be' %}
 {% elif item.language == 'nl-be' %}
 {% set name = 'Nederlands - Belgium' %}
 {% set country = 'Belgique' %}
 {% set data = 'be' %}
 {% elif item.language == 'nl' %}
 {% set name = 'Nederlands' %}
 {% set country = 'Nederlands' %}
 {% set data = 'nl' %}
 {% elif item.language == 'fr-lu' %}
 {% set name = 'Français - Luxembourg' %}
 {% set country = 'Luxembourg' %}
 {% set data = 'lu' %}
 {% endif %}
 <li class="menu desktop-lang">
 <ul class="others-lang--menu">
 <li class="other-lang">
 <span class="langTitle">
 <span class="flag">
 <img src="https://ressources.birdee.co/hubfs/Media/Common/{{ item.language }}.svg" class="dd-selected-image">
 </span>
 {{ country }}
 </span>
 <a class="lang-option" href="{{ item.absolute_url }}" data-country="{{ data }}" data-lang="{{ item.language }}">
 {{name}} 
 </a>
 </li>
 </ul>
 </li>
 {% endfor %}
 </ul>
 {% endif %}

```

And second question, in the articles, the switcher isn’t the same as on blog-archive, how can i display only the languages whose articles are translated :

```lia-code-sample
{% if content.translated_content|length %}
 <ul class="dropdown menu country-list" data-dropdown-menu>
 {% set currLang = locale_name(locale) %}
 {% set codes = {'English' : 'en', 'Français' : 'fr', 'Français - France' : 'fr-fr', 'Nederlands - Belgium' :'nl-be', 'Français - Luxembourg' : 'fr-lu' } %}
 <li class="">
 <a class="lang-select" href="#"><img class="lang-flag" src="https://ressources.birdee.co/hubfs/{{ codes[currLang] }}.svg" data-country="{{ codes[currLang]| truncate (2, true, '') }}" data-lang="{{ codes[currLang] }}">{{ currLang }}</a>
 </li>
 <li class="menu desktop-lang">
 <ul class="others-lang--menu">
 {% for key, val in codes.items() %}

 {% if val == 'fr' || val == 'fr-fr' %}
 {% set name = 'Français' %}
 {% set country = 'France' %}
 {% set data = 'fr' %}
 {% elif val == 'en' %}
 {% set name = 'English' %}
 {% set country = 'Europe' %}
 {% set data = 'eu' %}
 {% elif val == 'fr-be' %}
 {% set name = 'Français - Belgique' %}
 {% set country = 'Belgique' %}
 {% set data = 'be' %}
 {% elif val == 'nl-be' %}
 {% set name = 'Nederlands - Belgium' %}
 {% set country = 'Belgique' %}
 {% set data = 'be' %}
 {% elif val == 'nl' %}
 {% set name = 'Nederlands' %}
 {% set country = 'Nederlands' %}
 {% set data = 'nl' %}
 {% elif val == 'fr-lu' %}
 {% set name = 'Français - Luxembourg' %}
 {% set country = 'Luxembourg' %}
 {% set data = 'lu' %}
 {% endif %}
 {% set URL = "/"~content.translated_content[val].slug || val %}
 <li class="menu desktop-lang">
 <ul class="others-lang--menu">
 <li class="other-lang">
 <span class="langTitle">
 <span class="flag">
 <img src="https://ressources.birdee.co/hubfs/{{val}}.svg" class="dd-selected-image">
 </span>
 {{ country }}
 </span>
 <a class="lang-option" href="{{ URL }}" data-country="{{ data }}" data-lang="{{ val }}">{{key}}</a>
 </li>
 </ul>
 </li>
 {% endfor %}
 </ul>
 </li>
 </ul>

```

i use the same code 😋

Thank you very much.

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [March 4, 2021, 7:47pm UTC](https://community.hubspot.com/t/custom-language-switcher-language-switching/14291/42 "2021-03-04T19:47:36Z")

</div>

Hi @FelTigre,

The issue with sorting by country is that the country appears last in the language code e.g. ‘fr-be’ is country: Belgium, language: French, so to sort it by that you’d have to somehow get that substring out of item.language and then sort the list by that substring. It would be easier to do this with JS:

```lia-code-sample
var items = document.getElementsByClassName('other-lang');

items = Array.prototype.slice.call(items).sort(function(a, b) {
 return a.lastElementChild.dataset.country.localeCompare(b.lastElementChild.dataset.country);
});

var menu = document.getElementsByClassName('others-lang--menu')[0];
for (var i = 0, length = items.length; i < length; i++) {
 menu.appendChild(items[i]);
}

```

For the second question, instead of {% for key, val in codes.items() %} do {% for item in content.translated\_content %}.

---

<div class="post-metadata">

**Author:** ![FelTigre](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/feltigre/32/28305_2.png) [@FelTigre](https://community.hubspot.com/u/FelTigre)\
**Post date:** [March 5, 2021, 1:25pm UTC](https://community.hubspot.com/t/custom-language-switcher-language-switching/14291/43 "2021-03-05T13:25:53Z")

</div>

@piersg Thank you 😁

I will try this and i’ll tell you if it works 😃

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [March 5, 2021, 4:26pm UTC](https://community.hubspot.com/t/custom-language-switcher-language-switching/14291/44 "2021-03-05T16:26:07Z")

</div>

@FelTigre Sorry, small error, I used firstElementChild instead of lastElementChild, will update the above accordingly.

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [March 8, 2021, 10:35pm UTC](https://community.hubspot.com/t/custom-language-switcher-language-switching/14291/45 "2021-03-08T22:35:56Z")

</div>

jeez @piersg …

---

<div class="post-metadata">

**Author:** ![ALarsson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alarsson/32/38459_2.png) [@ALarsson](https://community.hubspot.com/u/ALarsson)\
**Post date:** [April 20, 2021, 9:52pm UTC](https://community.hubspot.com/t/custom-language-switcher-language-switching/14291/46 "2021-04-20T21:52:16Z")

</div>

Why is all this \*\*\*\*? Why can’t we simply get something like:

foreach translations as translation {

if (translation.current) {

/\* show current language label \*/

} else {

\<a href=“get\_url\_for\_translated\_content(translation)”\>translation.super\_nice\_locale\_name\</a\>

}

}

??

I’ve been tearing my hair out for hours to accomplish this simple little thing. I don’t want to do a billion conditions over “yeah it might be french, or maybe swedish”. This is just stupid. Fix the CMS already and let real programmers, who have to work with this platform whether we want or not, to accomplish simple BASIC things.

Sorry if I’m sounding angry, but I am. 😞

[Previous page](https://community.hubspot.com/t/custom-language-switcher-language-switching/14291.md?page=2)
