Hi there,

Surely this has been done before, but I cannot find an example. The documentation doesn't seem to have the answer. Anyway I'd like to retrieve 'global variables' (not sure if that's proper lingo) such as the currently used interface language. I thought that simply using $language would do the trick, but it doesn't. I do know how to use php in a node. Would anyone know the proper PHP code to get the current interface language?

or how about the username?

Thanks for any pointers (also pointers to documentation of course).

RvK

Comments

carlmcdade’s picture

rvk’s picture

That results in an error (and without the "()" it results in $language being a string):

Fatal error: Call to undefined function i18n_get_lang() in
/public_html/includes/common.inc(1537) : eval()'d code on line 2

I did activate the locale module in my Drupal 6 installation.

carlmcdade’s picture

Sorry, I thought you were using drupal 5 and the internationalization module. In Drupal 6 without the internationalization module try this function. Just set in the property you want like language_default('language').

<?php
 
/**
 * Default language used on the site
 *
 * @param $property
 *   Optional property of the language object to return
 */
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;
}
?>

Though this is core code it is stuppid and ugly. I have no idea why they chose to cast the out put into an object and allow the return of only one property. It would have been better to leave the array alone and allow you the grab all the properties at once then choose them by key.

<?php
 
/**
 * Default language used on the site
 *
 * @param $property
 *   Optional property of the language object to return
 */
function fo_1_language_default($property = NULL) {
 
$language = variable_get('language_default',  array('language' => 'en', 'name' => 'English', 'native' => 'English', 'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => '', 'weight' => 0, 'javascript' => ''));
  return
$language;
}

$lang_props = fo_1_language_default();
$language = $lang_props['language'];
?>

Choose the method you think will suit you over time.

Hiveminds Magazine | FireOrb | Drupal Street | Drupal offline manual

Heine’s picture

You remark regarding language_default (reproduced below):

<?php
/**
* Default language used on the site
*
* @param $property
*   Optional property of the language object to return
*/
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;
}
?>

Though this is core code it is stuppid and ugly. I have no idea why they chose to cast the out put into an object and allow the return of only one property. It would have been better to leave the array alone and allow you the grab all the properties at once then choose them by key.

By simply reading the code:

$language and $language_default are — unsurprisingly — similar objects. The array is just used as an easy / elegant object notation for the variable_get 'default' argument. First building an object on stdclass, then using it as the second argument to variable_get is much messier.

Also, if you do not pass any argument to language_default, $property will be NULL. This causes return $property ? $language->$property : $language; to return the entire object, not just a property.

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

Heine’s picture

Drupal 6 globals are documented on http://api.drupal.org/api/globals/6

<?php
global $language;
print_r($language);
/* This would give you stdClass Object (
  [language] => en
  [name] => English
  [native] => English
  [direction] => 0
  [enabled] => 1
  [plurals] => 0
  [formula] =>
  [domain] =>
  [prefix] =>
  [weight] => 0
  [javascript] =>
)
*/
print $language->name; // prints 'English'
?>

This gives you the current interface locale, not the default.

<?php
// The global $user object holds the name.
global $user;
print
check_plain($user->name);
?>

See also http://www.php.net/global about variable scope, the global keyword and the $GLOBALS superglobal.
--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

rvk’s picture

Thanks Hiveminds and Heine for your tips!

I'll have to improve my knowledge on php a bit to use all of them, but I'm able to do what I wanted now. So my problem is solved: great!

Thanks!