function language

Returns the language object for a given language type.

The 'language_manager' service is only available within the scope of a kernel request. When it's not available, we return a default language object, regardless of the type passed in.

Parameters

string $type: The type of language object needed, e.g. LANGUAGE_TYPE_INTERFACE.

bool $reset: TRUE to reset the statically cached language object for the type, or for all types if $type is NULL.

See also

Drupal\Core\Language\LanguageManager

57 calls to language()
AccountFormController::form in drupal/core/modules/user/lib/Drupal/user/AccountFormController.php
Overrides Drupal\Core\Entity\EntityFormController::form().
CascadingStylesheetsTest::testAlter in drupal/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php
Tests Locale module's CSS Alter to include RTL overrides.
CommentFormController::form in drupal/core/modules/comment/lib/Drupal/comment/CommentFormController.php
Overrides Drupal\Core\Entity\EntityFormController::form().
CommentTokenReplaceTest::testCommentTokenReplacement in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
Creates a comment, then tests the tokens generated from it.
contact_personal_form_submit in drupal/core/modules/contact/contact.pages.inc
Form submission handler for contact_personal_form().

... See full list

51 string references to 'language'
block_add_block_form_submit in drupal/core/modules/block/block.admin.inc
Form submission handler for block_add_block_form().
block_admin_configure in drupal/core/modules/block/block.admin.inc
Form constructor for the block configuration form.
block_admin_configure_submit in drupal/core/modules/block/block.admin.inc
Form submission handler for block_admin_configure().
comment_update_8000 in drupal/core/modules/comment/comment.install
Renames {comment}.language to {comment}.langcode.
comment_views_data in drupal/core/modules/comment/comment.views.inc
Implements hook_views_data().

... See full list

File

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

Code

function language($type, $reset = FALSE) {

  // We don't use drupal_static() here because resetting is not a simple case of
  // drupal_static_reset().
  static $languages = array();

  // Reset the language manager's cache and our own.
  if ($reset) {
    if (drupal_container()
      ->isScopeActive('request')) {
      drupal_container()
        ->get('language_manager')
        ->reset($type);
    }
    if (!isset($type)) {
      $languages = array();
    }
    elseif (isset($languages[$type])) {
      unset($languages[$type]);
    }
  }

  // If no type is passed (most likely when resetting all types), return.
  if (!isset($type)) {
    return;
  }

  // When the language_manager service exists (is both defined and the 'request'
  // scope is active in the container), use it to get the language. Otherwise
  // return the default language.
  if (drupal_container()
    ->isScopeActive('request')) {
    $language_manager = drupal_container()
      ->get('language_manager', Container::NULL_ON_INVALID_REFERENCE);
  }
  if (isset($language_manager)) {
    return $language_manager
      ->getLanguage($type);
  }
  else {
    if (!isset($languages[$type])) {
      $languages[$type] = language_default();
    }
    return $languages[$type];
  }
}