1

I have a custom module which I want to check the language code of the current interface language, So I added a helper function to the mymodule.module file.

It didn't work in mymodule when I call it, It returned nothing. But If I added this function to my template.php in the mytheme directory it works like a charm and return the language code for the current interface.

I am guesting that its related to the current weight of the module in the system table, What do you think?

function language_code(){
  global $language;
  return $language->language;
}

For additional debugging I used dpm($language); after defining global $language; in mymodule it didn't return the language array.

  • Do you unistall/install locale module?try it. – Yusef Sep 13 '12 at 17:50
1

drupal has a function that return default language language_default with the below code

    function language_default($property = NULL) {
  $language = variable_get('language_default', (object) array('language' => 'en', 'name' => 'English', 'native' => 'English', 'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => '', 'weight' => 0, 'javascript' => ''));
  return $property ? $language->$property : $language;
}

I suggest you in hook_init(maybe your fuction not called or problem realated to your fuction) call this function

function yourmodulename_init() {
  dsm(language_default());
  global $language;
  dsm($language);
}

According drupal documentation , $language is global variable , you problem is some strange .

I thnik problem is in your function and method of calling function. by default if you put a function in module and your module enabled(after clear cache) ,the function globaly exist and you can use it everywhere (even in views on anywhere you think).

If dsm in hook_init return $language value I suggest you test your function

function my_language_code(){
  global $language;
  return $language->language;

}

in very where you want

$lang=my_language_code();
dsm($lang);

test this please

  • I will check and come back to you. – Jonathan Hindi Sep 16 '12 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.