You are here

class Language in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Language/Language.php \Drupal\Core\Language\Language
  2. 10 core/modules/ckeditor/src/Plugin/CKEditorPlugin/Language.php \Drupal\ckeditor\Plugin\CKEditorPlugin\Language
  3. 10 core/modules/language/src/Plugin/Condition/Language.php \Drupal\language\Plugin\Condition\Language
  4. 10 core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Language.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Language
  5. 10 core/lib/Drupal/Core/TypedData/Plugin/DataType/Language.php \Drupal\Core\TypedData\Plugin\DataType\Language
  6. 10 core/modules/language/src/Plugin/migrate/source/Language.php \Drupal\language\Plugin\migrate\source\Language
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Language/Language.php \Drupal\Core\Language\Language
  2. 9 core/lib/Drupal/Core/Language/Language.php \Drupal\Core\Language\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()

48 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
Contains \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest.
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
Contains \Drupal\Tests\config_translation\Unit\ConfigNamesMapperTest.

... See full list

46 string references to 'Language'
ckeditor.schema.yml in core/modules/ckeditor/config/schema/ckeditor.schema.yml
core/modules/ckeditor/config/schema/ckeditor.schema.yml
ckeditor5.ckeditor5.yml in core/modules/ckeditor5/ckeditor5.ckeditor5.yml
core/modules/ckeditor5/ckeditor5.ckeditor5.yml
ckeditor5.schema.yml in core/modules/ckeditor5/config/schema/ckeditor5.schema.yml
core/modules/ckeditor5/config/schema/ckeditor5.schema.yml
ConfigTestForm::form in core/modules/config/tests/config_test/src/ConfigTestForm.php
ConfigTranslationController::itemPage in core/modules/config_translation/src/Controller/ConfigTranslationController.php
Language translations overview page for a configuration name.

... See full list

File

core/lib/Drupal/Core/Language/Language.php, line 12

Namespace

Drupal\Core\Language
View 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