6.x bootstrap.inc language_list($field = 'language', $reset = FALSE)
7.x bootstrap.inc language_list($field = 'language')

Returns a list of installed languages, indexed by the specified key.

Parameters

$field: (optional) The field to index the list with.

Return value

An associative array, keyed on the values of $field.

  • If $field is 'weight' or 'enabled', the array is nested, with the outer array's values each being associative arrays with language codes as keys and language objects as values.
  • For all other values of $field, the array is only one level deep, and the array's values are language objects.
39 calls to language_list()
field_content_languages in modules/field/field.multilingual.inc
Returns available content languages.
language_provider_invoke in includes/language.inc
Helper function used to cache the language negotiation providers results.
LocaleConfigurationTest::testLanguageConfiguration in modules/locale/locale.test
Functional tests for adding, editing and deleting languages.
LocaleUILanguageNegotiationTest::testLanguageDomain in modules/locale/locale.test
Tests url() when separate domains are used for multiple languages.
LocaleUILanguageNegotiationTest::testUILanguageNegotiation in modules/locale/locale.test
Tests for language switching by URL path.

... See full list

14 string references to 'language_list'
LocaleCommentLanguageFunctionalTest::testCommentLanguage in modules/locale/locale.test
Test that comment language is properly set.
LocaleConfigurationTest::testLanguageConfiguration in modules/locale/locale.test
Functional tests for adding, editing and deleting languages.
LocaleTranslationFunctionalTest::testJavaScriptTranslation in modules/locale/locale.test
LocaleUILanguageNegotiationTest::testUILanguageNegotiation in modules/locale/locale.test
Tests for language switching by URL path.
LocaleUrlRewritingTest::setUp in modules/locale/locale.test
Sets up a Drupal site for running functional and integration tests.

... See full list

File

includes/bootstrap.inc, line 3024
Functions that need to be loaded on every Drupal request.

Code

function language_list($field = 'language') {
  $languages =& drupal_static(__FUNCTION__);

  // Init language list
  if (!isset($languages)) {
    if (drupal_multilingual() || module_exists('locale')) {
      $languages['language'] = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC')
        ->fetchAllAssoc('language');

      // Users cannot uninstall the native English language. However, we allow
      // it to be hidden from the installed languages. Therefore, at least one
      // other language must be enabled then.
      if (!$languages['language']['en']->enabled && !variable_get('language_native_enabled', TRUE)) {
        unset($languages['language']['en']);
      }
    }
    else {

      // No locale module, so use the default language only.
      $default = language_default();
      $languages['language'][$default->language] = $default;
    }
  }

  // Return the array indexed by the right field
  if (!isset($languages[$field])) {
    $languages[$field] = array();
    foreach ($languages['language'] as $lang) {

      // Some values should be collected into an array
      if (in_array($field, array(
        'enabled',
        'weight',
      ))) {
        $languages[$field][$lang->{$field}][$lang->language] = $lang;
      }
      else {
        $languages[$field][$lang->{$field}] = $lang;
      }
    }
  }
  return $languages[$field];
}

Comments

Pierre Paul Lefebvre’s picture

When calling:

$languages = language_list('enabled');

You can expect the return value to be:

$languages = array(
    '1' => 
        array( 
            'en' => array('language'=>'en', 'name'=>'English', ... ), 
            'fr' => array('language'=> 'fr', 'name' => 'Francais', ... ),
        )
    );

The index being 1 because it's the value of the 'enabled' column.

Pasqualle’s picture

When you have the locale module enabled, you can use locale_language_list():

$languages = locale_language_list('name');

which returns a much simpler list:

$languages = array(
  'en' => 'English',
  'fr' => 'Francais',
);
stefan.r’s picture

Note that:

$languages = locale_language_list('name');

will only return enabled languages, as opposed to language_list(), which will return all installed languages.

rhclayto’s picture

But $languages = locale_language_list('name', TRUE); will return all the languages, just like language_list(); (because it's just calling it internally), but formatted in the locale_language_list() fashion.

Flimmer1970’s picture

return $languages = locale_language_list('name');

the result is array. I want some variables like $spanish is enabled and so on.. please help!

kingandy’s picture

This function is designed to return a list of all the languages in the system, keyed by the property you pass in. The information you want should still be available, though! If you want to check if (e.g.) Spanish is enabled I would suggest something like this:

$languages = language_list();
return $languages['es']->status;
edurenye’s picture

In Drupal 8 we can use \Drupal::languageManager()->getLanguages();