class Language
An object containing the information for an interface language.
Hierarchy
- class \Drupal\Core\Language\Language implements \Drupal\Core\Language\LanguageInterface
 
Expanded class hierarchy of Language
See also
\Drupal\Core\Language\LanguageManager::getLanguage()
60 files declare their use of Language
- AliasManagerTest.php in core/
modules/ path_alias/ tests/ src/ Unit/ AliasManagerTest.php  - ConfigEntityBaseUnitTest.php in core/
tests/ Drupal/ Tests/ Core/ Config/ Entity/ ConfigEntityBaseUnitTest.php  - ConfigEntityStorageTest.php in core/
tests/ Drupal/ Tests/ Core/ Config/ Entity/ ConfigEntityStorageTest.php  - ConfigMapperManagerTest.php in core/
modules/ config_translation/ tests/ src/ Unit/ ConfigMapperManagerTest.php  - ConfigNamesMapperTest.php in core/
modules/ config_translation/ tests/ src/ Unit/ ConfigNamesMapperTest.php  
396 string references to 'Language'
- AccountSettingsForm::buildForm in core/
modules/ user/ src/ AccountSettingsForm.php  - Form constructor.
 - AssetControllerBase::deliver in core/
modules/ system/ src/ Controller/ AssetControllerBase.php  - Generates an aggregate, given a filename.
 - block.block.umami_languageswitcher.yml in core/
profiles/ demo_umami/ config/ install/ block.block.umami_languageswitcher.yml  - core/profiles/demo_umami/config/install/block.block.umami_languageswitcher.yml
 - BlockContentTypeForm::form in core/
modules/ block_content/ src/ BlockContentTypeForm.php  - Gets the actual form array to be built.
 - BlockForm::buildVisibilityInterface in core/
modules/ block/ src/ BlockForm.php  - Helper function for building the visibility UI form.
 
File
- 
              core/
lib/ Drupal/ Core/ Language/ Language.php, line 12  
Namespace
Drupal\Core\LanguageView source
class Language implements LanguageInterface {
  
  /**
   * The values to use to instantiate the default language.
   *
   * @var array
   */
  public static $defaultValues = [
    'id' => 'en',
    'name' => 'English',
    'direction' => self::DIRECTION_LTR,
    'weight' => 0,
    'locked' => FALSE,
  ];
  // Properties within the Language are set up as the default language.
  
  /**
   * The human readable English name.
   *
   * @var string
   */
  protected $name = '';
  
  /**
   * The ID, langcode.
   *
   * @var string
   */
  protected $id = '';
  
  /**
   * The direction, left-to-right, or right-to-left.
   *
   * Defined using constants, either self::DIRECTION_LTR or self::DIRECTION_RTL.
   *
   * @var int
   */
  protected $direction = self::DIRECTION_LTR;
  
  /**
   * The weight, used for ordering languages in lists, like selects or tables.
   *
   * @var int
   */
  protected $weight = 0;
  
  /**
   * Locked indicates a language used by the system, not an actual language.
   *
   * Examples of locked languages are, LANGCODE_NOT_SPECIFIED, und, and
   * LANGCODE_NOT_APPLICABLE, zxx, which are usually shown in language selects
   * but hidden in places like the Language configuration and cannot be deleted.
   *
   * @var bool
   */
  protected $locked = FALSE;
  
  /**
   * Constructs a new class instance.
   *
   * @param array $values
   *   An array of property values, keyed by property name, used to construct
   *   the language.
   */
  public function __construct(array $values = []) {
    // Set all the provided properties for the language.
    foreach ($values as $key => $value) {
      if (property_exists($this, $key)) {
        $this->{$key} = $value;
      }
    }
    // If some values were not set, set sane defaults of a predefined language.
    if (!isset($values['name']) || !isset($values['direction'])) {
      $predefined = LanguageManager::getStandardLanguageList();
      if (isset($predefined[$this->id])) {
        if (!isset($values['name'])) {
          $this->name = $predefined[$this->id][0];
        }
        if (!isset($values['direction']) && isset($predefined[$this->id][2])) {
          $this->direction = $predefined[$this->id][2];
        }
      }
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->name;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->id;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getDirection() {
    return $this->direction;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getWeight() {
    return $this->weight;
  }
  
  /**
   * {@inheritdoc}
   */
  public function isDefault() {
    return static::getDefaultLangcode() == $this->getId();
  }
  
  /**
   * {@inheritdoc}
   */
  public function isLocked() {
    return (bool) $this->locked;
  }
  
  /**
   * Sort language objects.
   *
   * @param \Drupal\Core\Language\LanguageInterface[] $languages
   *   The array of language objects keyed by langcode.
   */
  public static function sort(&$languages) {
    uasort($languages, function (LanguageInterface $a, LanguageInterface $b) {
      $a_weight = $a->getWeight();
      $b_weight = $b->getWeight();
      if ($a_weight == $b_weight) {
        $a_name = $a->getName();
        $b_name = $b->getName();
        // If either name is a TranslatableMarkup object it can not be converted
        // to a string. This is because translation requires a sorted list of
        // languages thereby causing an infinite loop. Determine the order based
        // on ID if this is the case.
        if ($a_name instanceof TranslatableMarkup || $b_name instanceof TranslatableMarkup) {
          $a_name = $a->getId();
          $b_name = $b->getId();
        }
        return strnatcasecmp($a_name, $b_name);
      }
      return $a_weight <=> $b_weight;
    });
  }
  
  /**
   * Gets the default langcode.
   *
   * @return string
   *   The current default langcode.
   */
  protected static function getDefaultLangcode() {
    $language = \Drupal::service('language.default')->get();
    return $language->getId();
  }
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | 
|---|---|---|---|---|
| Language::$defaultValues | public static | property | The values to use to instantiate the default language. | |
| Language::$direction | protected | property | The direction, left-to-right, or right-to-left. | |
| Language::$id | protected | property | The ID, langcode. | |
| Language::$locked | protected | property | Locked indicates a language used by the system, not an actual language. | |
| Language::$name | protected | property | The human readable English name. | |
| Language::$weight | protected | property | The weight, used for ordering languages in lists, like selects or tables. | |
| Language::getDefaultLangcode | protected static | function | Gets the default langcode. | |
| Language::getDirection | public | function | Gets the text direction (left-to-right or right-to-left). | Overrides LanguageInterface::getDirection | 
| Language::getId | public | function | Gets the ID (language code). | Overrides LanguageInterface::getId | 
| Language::getName | public | function | Gets the name of the language. | Overrides LanguageInterface::getName | 
| Language::getWeight | public | function | Gets the weight of the language. | Overrides LanguageInterface::getWeight | 
| Language::isDefault | public | function | Returns whether this language is the default language. | Overrides LanguageInterface::isDefault | 
| Language::isLocked | public | function | Returns whether this language is locked. | Overrides LanguageInterface::isLocked | 
| Language::sort | public static | function | Sort language objects. | |
| Language::__construct | public | function | Constructs a new class instance. | |
| LanguageInterface::DIRECTION_LTR | constant | Language written left to right. Possible value of $language->direction. | ||
| LanguageInterface::DIRECTION_RTL | constant | Language written right to left. Possible value of $language->direction. | ||
| LanguageInterface::LANGCODE_DEFAULT | constant | Language code referring to the default language of data, e.g. of an entity. | ||
| LanguageInterface::LANGCODE_NOT_APPLICABLE | constant | The language code used when the marked object has no linguistic content. | ||
| LanguageInterface::LANGCODE_NOT_SPECIFIED | constant | The language code used when no language is explicitly assigned (yet). | ||
| LanguageInterface::LANGCODE_SITE_DEFAULT | constant | Language code referring to site's default language. | ||
| LanguageInterface::LANGCODE_SYSTEM | constant | Special system language code (only applicable to UI language). | ||
| LanguageInterface::STATE_ALL | constant | The language state used when referring to all languages. | ||
| LanguageInterface::STATE_CONFIGURABLE | constant | The language state when referring to configurable languages. | ||
| LanguageInterface::STATE_LOCKED | constant | The language state when referring to locked languages. | ||
| LanguageInterface::STATE_SITE_DEFAULT | constant | The language state used when referring to the site's default language. | ||
| LanguageInterface::TYPE_CONTENT | constant | The type of language used to define the content language. | ||
| LanguageInterface::TYPE_INTERFACE | constant | The type of language used to select the user interface. | ||
| LanguageInterface::TYPE_URL | constant | The type of language used for URLs. | ||
| LanguageInterface::VALID_LANGCODE_REGEX | constant | A regex for validating language codes according to W3C specifications. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.