Language.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Drupal\Core\Language;
  3. use Drupal\Core\StringTranslation\TranslatableMarkup;
  4. /**
  5. * An object containing the information for an interface language.
  6. *
  7. * @see \Drupal\Core\Language\LanguageManager::getLanguage()
  8. */
  9. class Language implements LanguageInterface {
  10. /**
  11. * The values to use to instantiate the default language.
  12. *
  13. * @var array
  14. */
  15. public static $defaultValues = [
  16. 'id' => 'en',
  17. 'name' => 'English',
  18. 'direction' => self::DIRECTION_LTR,
  19. 'weight' => 0,
  20. 'locked' => FALSE,
  21. ];
  22. // Properties within the Language are set up as the default language.
  23. /**
  24. * The human readable English name.
  25. *
  26. * @var string
  27. */
  28. protected $name = '';
  29. /**
  30. * The ID, langcode.
  31. *
  32. * @var string
  33. */
  34. protected $id = '';
  35. /**
  36. * The direction, left-to-right, or right-to-left.
  37. *
  38. * Defined using constants, either self::DIRECTION_LTR or self::DIRECTION_RTL.
  39. *
  40. * @var int
  41. */
  42. protected $direction = self::DIRECTION_LTR;
  43. /**
  44. * The weight, used for ordering languages in lists, like selects or tables.
  45. *
  46. * @var int
  47. */
  48. protected $weight = 0;
  49. /**
  50. * Locked indicates a language used by the system, not an actual language.
  51. *
  52. * Examples of locked languages are, LANGCODE_NOT_SPECIFIED, und, and
  53. * LANGCODE_NOT_APPLICABLE, zxx, which are usually shown in language selects
  54. * but hidden in places like the Language configuration and cannot be deleted.
  55. *
  56. * @var bool
  57. */
  58. protected $locked = FALSE;
  59. /**
  60. * Constructs a new class instance.
  61. *
  62. * @param array $values
  63. * An array of property values, keyed by property name, used to construct
  64. * the language.
  65. */
  66. public function __construct(array $values = []) {
  67. // Set all the provided properties for the language.
  68. foreach ($values as $key => $value) {
  69. if (property_exists($this, $key)) {
  70. $this->{$key} = $value;
  71. }
  72. }
  73. // If some values were not set, set sane defaults of a predefined language.
  74. if (!isset($values['name']) || !isset($values['direction'])) {
  75. $predefined = LanguageManager::getStandardLanguageList();
  76. if (isset($predefined[$this->id])) {
  77. if (!isset($values['name'])) {
  78. $this->name = $predefined[$this->id][0];
  79. }
  80. if (!isset($values['direction']) && isset($predefined[$this->id][2])) {
  81. $this->direction = $predefined[$this->id][2];
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getName() {
  90. return $this->name;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function getId() {
  96. return $this->id;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function getDirection() {
  102. return $this->direction;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getWeight() {
  108. return $this->weight;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function isDefault() {
  114. return static::getDefaultLangcode() == $this->getId();
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function isLocked() {
  120. return (bool) $this->locked;
  121. }
  122. /**
  123. * Sort language objects.
  124. *
  125. * @param \Drupal\Core\Language\LanguageInterface[] $languages
  126. * The array of language objects keyed by langcode.
  127. */
  128. public static function sort(&$languages) {
  129. uasort($languages, function (LanguageInterface $a, LanguageInterface $b) {
  130. $a_weight = $a->getWeight();
  131. $b_weight = $b->getWeight();
  132. if ($a_weight == $b_weight) {
  133. $a_name = $a->getName();
  134. $b_name = $b->getName();
  135. // If either name is a TranslatableMarkup object it can not be converted
  136. // to a string. This is because translation requires a sorted list of
  137. // languages thereby causing an infinite loop. Determine the order based
  138. // on ID if this is the case.
  139. if ($a_name instanceof TranslatableMarkup || $b_name instanceof TranslatableMarkup) {
  140. $a_name = $a->getId();
  141. $b_name = $b->getId();
  142. }
  143. return strnatcasecmp($a_name, $b_name);
  144. }
  145. return ($a_weight < $b_weight) ? -1 : 1;
  146. });
  147. }
  148. /**
  149. * Gets the default langcode.
  150. *
  151. * @return string
  152. * The current default langcode.
  153. */
  154. protected static function getDefaultLangcode() {
  155. $language = \Drupal::service('language.default')->get();
  156. return $language->getId();
  157. }
  158. }