Module always returns the default language in hook_theme() I am developing a custom block that shows a different image URLs for different languages. Currently, I enabled English and French.

The code I am using is the following one.

function MODULENAME_theme($existing, $type, $theme, $path) {   
    $currentLangCode = \Drupal::service('language_manager')-&gt;getCurrentLanguage()-&gt;getId(); 
    $imageUrl = "";
    # If current language code is english
    if ($currentLangCode == "en") {
        $imageUrl = "English image url"
    } elseif ($currentLangCode == "fr") {
        $imageUrl = "French image url"
    }
    return [
       "modulename_block_template" =&gt; [
            "variables" =&gt; [
               "description" =&gt; [],
               "image" =&gt; $imageUrl,
               "current_language" =&gt; $currentLangCode
            ],
       ],
    ];
}


The problem is that the value for $currentLangCode is always 'en'.

What can I do to get the current language?
Data from hook_theme is cached, any logic decisions you make in it will only apply when the theme registry is rebuilt.

The point of variables is that they're passed in at the time the theme is called, not when the theme hook is declared.

For example hook_theme() should look like this:

MODULENAME_theme($existing, $type, $theme, $path) {
  return [
    'modulename_block_template' =&gt; [
      'variables' = [
        'description' =&gt; '',
        'image' =&gt; '',
        'current_language' =&gt; '',
      ],
    ],
  ];    
}


And you pass the variables in when you use it:

$currentLangCode = \Drupal::service('language_manager')-&gt;getCurrentLanguage()-&gt;getId(); 
$imageUrl = '';
# If current language code is english
if ($currentLangCode == "en") {
    $imageUrl = "English image url"
} elseif ($currentLangCode == "fr") {
    $imageUrl = "French image url"
}

$build = [
  '#theme' =&gt; 'modulename_block_template',
  '#description' =&gt; '...',
  '#image' =&gt; $imageUrl,
  '#current_language' =&gt; $currentLangCode,
];