Skip to main content
3 of 5
deleted 6 characters in body; edited title
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182

How to get user's current language code in Views?

On Drupal 8.7.10

How can I get the user's current language code into Views as a raw value, such as 'en' or 'fr-ca'?

This is not 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 posts, I've tried it these three ways:

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

I'm getting 'failed'

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

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 %}

.... and, still, 'failed'.

turpentyne
  • 477
  • 4
  • 17