Search code examples
drupalmodulecontrollerdrupal-7global

$language->language get the de default language not current language [Drupal 7]


I have a custom module, and on its controller, when I try to get the current interface language. I get the default language 'en'.

Someone can tell me why

<?php
    function module_controller_my_function(){ 
        global $language;
        var_dump($language->language);
    }
?>

it give the default language instead the current interface language


Solution

  • I have the same issue. Nodes are translated correctly but the global $language in custom module is always the site default language.

    You can get past this with some code. For example reading the language from the $_SERVER variable. Example:

    function _my_module_get_language() {
      $referer_part_list = explode('/', $_SERVER['HTTP_REFERER']);
    
      return $referer_part_list[3];
    }

    Note that most likely there is a better way of doing this (Drupal way).