Score:0

Check the active language

us flag

I'm sorry for my bad English. In my node.html.twig I have two layouts for two groups of languages.

  1. English, Ukrainian and Bulgarian
  2. Arabic, Farsi, Urdu I can't check with twig which is the active language to load one of the two layouts. I tried this but it didn't work. Thanks a lot if anyone can help me
{% if language.getId() == 'en' or 'uk' or 'bg' or 'ru' %}

    
        {{ content.field_mt_srv_body }}
        {{ content.field_sp_working_hours }}
        {{ content.field_sp_additional_information }}
        {{ content.field_mt_srv_video }}
    
    
         {{ content.field_sp_street_view }}
     
       
  • {{ content.field_sp_city }}
  • {{ content.field_sp_address }}
  • {{ content.field_sp_email }}
  • {{ content.field_field_sp_phone }}
  • {{ content.field_sp_languages }}
  • {{ content.field_mt_srv_tags }}
  • {{ content.field_sp_website }}
  • {{ content.field_sp_facebook }}
  • {% else %} {{ content.field_mt_srv_body }} {{ content.field_sp_working_hours }} {{ content.field_sp_additional_information }} {{ content.field_mt_srv_video }} {{ content.field_sp_street_view }} {{ content.field_sp_city }} {{ content.field_sp_address }} {{ content.field_sp_email }} {{ content.field_field_sp_phone }} {{ content.field_sp_languages }} {{ content.field_mt_srv_tags }} {{ content.field_sp_website }} {{ content.field_sp_facebook }} {% endif %}
    Score:1
    cn flag

    To get this working in Twig

    {% if language.getId() == 'en' or 'uk' or 'bg' or 'ru' %}
    

    you have to define the variable language in a preprocess hook:

    mytheme.theme or mymodule.module

    /**
     * Implements hook_preprocess_HOOK() for node templates.
     */
    function mytheme/mymodule_preprocess_node(&$variables) {
      $variables['language'] = \Drupal::languageManager()->getCurrentLanguage();
    }
    

    The only template where this variable is predefined is the page template, so that you don't need the preprocess hook. See this core preprocess hook: https://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/template_preprocess_page/

    Score:-1
    hr flag

    In Drupal 8.5.7 and upwards a global variable 'language' exists that can be called directly in twig like example below.

    {% if language == 'en' %} 
    

    See this resource here

    ru flag
    This is incorrect, there is no global language in templates. It was only added to `page.html.twig`, but not other templates likes `node.html.twig`
    I sit in a Tesla and translated this thread with Ai:

    mangohost

    Post an answer

    Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.