1 bootstrap.inc language_list($only_enabled = FALSE, $option_list = FALSE)

Returns a list of configured languages.

Parameters

$only_enabled: (optional) Whether to return only enabled languages.

$option_list: (optional) Whether to return a list of options instead of objects. This list is suitable for use in a select list #options array. This list is ordered by weight and then language name.

Return value

(stdClass|string)[] An associative array of languages, keyed by the language code.

File

core/includes/bootstrap.inc, line 3505
Functions that need to be loaded on every Backdrop request.

Code

function language_list($only_enabled = FALSE, $option_list = FALSE) {
  $languages = &backdrop_static(__FUNCTION__);
  // Initialize master language list.
  if (!isset($languages)) {
    // Initialize local language list caches.
    $languages = array('all' => array(), 'enabled' => array());

    // Fill in master language list based on current configuration. This may be
    // in the event of an early bootstrap error, so fallback to defaults.
    try {
      $default_langcode = config_get('system.core', 'language_default');
      $config_languages = config_get('language.settings', 'languages');
    }
    catch (ConfigException $e) {
      $default_langcode = 'en';
      $config_languages = array();
    }

    if ($config_languages) {
      foreach ($config_languages as $langcode => $language) {
        $language += array(
          'enabled' => TRUE,
          'weight' => 0,
        );
        $languages['all'][$langcode] = (object) $language;
      }
    }
    // No languages available, so only English is supported.
    else {
      $default_langcode = 'en';
      $languages['all']['en'] = (object) array(
        'langcode' => 'en',
        'name' => 'English',
        'direction' => 0,
        'enabled' => TRUE,
        'weight' => 0,
        'default' => TRUE,
      );
    }

    // Initialize default property so callers have an easy reference and can
    // save the same object without data loss. Also fill in the filtered list
    // of enabled languages only.
    foreach ($languages['all'] as $langcode => $language) {
      $languages['all'][$langcode]->default = ($langcode == $default_langcode);
      if ($language->enabled) {
        $languages['enabled'][$langcode] = $languages['all'][$langcode];
      }
    }
  }

  if ($option_list) {
    $list = array();
    $this_list = $only_enabled ? $languages['enabled'] : $languages['all'];
    // backdrop_sort() needed for ordering, but common.inc may not be loaded
    // in very early bootstrap. Include only if returning an option list.
    require_once __DIR__ . '/common.inc';
    backdrop_sort($this_list, array('weight' => SORT_NUMERIC, 'name' => SORT_STRING));
    foreach ($this_list as $language) {
      $list[$language->langcode] = $language->name;
    }
    return $list;
  }

  return $only_enabled ? $languages['enabled'] : $languages['all'];
}