I found my answer and post it here so maybe it helps others later.
First, in MYTHEME.theme
:
In Drupal 8.2 language got added to page the variables in template_preprocess_page. But in my current version 8.1 it didn't exist so I added it with:
function MYTHEME_preprocess_page(&$vars) {
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$vars['language'] = $language;
}
Then in my Twig file I use {{ language }}
. Also I can get the language name with {{ language.getName() }}
.
Update
After Drupal 8.2 a global variable language
exists and now there's no more need to add it first.
{% if language.getId() == 'en' %}
Do something
{% else %}
Do something else
{% endif %}
Update
Thanks @Lucas for his comment
on Drupal > 8.7.5
{% if language.getId() == 'en' %} should be {% if language == 'en' %}