Skip to main content
5 of 5
improved formatting
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Get the user's current language code

How can I get the user's current language code in a view as a raw value, for example en or fr-ca?

This isn't for filtering content. I need the raw value of the users' current language to pass into a URL for a third-party tool. I added a custom text field. Then placed in some Twig code. The code is just failing to the fallback else value.

Based on other forum posts, I tried this code.

{% if language.getId() == 'en' %}
  en
{% elseif language.getId() == 'es-us' %}
  es-us
{% elseif language.getId() == 'en-us' %}
  en-us
{% else %}
  failed
{% endif %}

I get failed.

{% if language == 'en' %} 
  en
{% elseif language == 'es-us' %}
  es-us
{% elseif language == 'en-us' %}
  en-us
{% else %}
  failed
{% endif %}

I still get failed.

{% if language.id() == 'en' %}
  en
{% elseif language.id() == 'es-us' %}
  es-us
{% elseif language.id() == 'en-us' %}
  en-us
{% else %}
  failed
{% endif %}

I still get the same output.

turpentyne
  • 477
  • 4
  • 17