Skip to main content

Drupal 8 - get language id from field.html.twig

Add this function inside your theme file. Example for theme named scriptun.

// /themes/scriptun/scriptun.theme

function scriptun_preprocess_field(&$variables) {
    $variables['language_id'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}

Variable with language id will be available for all fields and any theme suggestion used.

{# /themes/scriptun/templates/field/field.html.twig #}

This is language id: {{ language_id }}
{% if (language_id == 'en') %}
This text is visible only for english
{% endif %}

 

Add new comment

CAPTCHA

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

We use cookies on our website to enhance your user experience. We also use Google analytics and ads.

Click here to read more I've read it