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

Returns a list of configured languages.

@since 1.18.2 Added $native_names parameter.

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.

bool $native_names: (optional) Whether the option_list labels should use the native name instead of the English language name. Only applies if $option_list is TRUE.

Return value

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

File

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

Code

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

    // Fill in primary 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',
        'native' => '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) {
      if ($native_names && isset($language->native)) {
        $label = $language->native;
      }
      else {
        $label = $language->name;
      }
      $list[$language->langcode] = $label;
    }
    return $list;
  }

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