Chris@0: 'en', Chris@0: 'name' => 'English', Chris@0: 'direction' => self::DIRECTION_LTR, Chris@0: 'weight' => 0, Chris@0: 'locked' => FALSE, Chris@0: ]; Chris@0: Chris@0: // Properties within the Language are set up as the default language. Chris@0: Chris@0: /** Chris@0: * The human readable English name. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $name = ''; Chris@0: Chris@0: /** Chris@0: * The ID, langcode. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $id = ''; Chris@0: Chris@0: /** Chris@0: * The direction, left-to-right, or right-to-left. Chris@0: * Chris@0: * Defined using constants, either self::DIRECTION_LTR or self::DIRECTION_RTL. Chris@0: * Chris@0: * @var int Chris@0: */ Chris@0: protected $direction = self::DIRECTION_LTR; Chris@0: Chris@0: /** Chris@0: * The weight, used for ordering languages in lists, like selects or tables. Chris@0: * Chris@0: * @var int Chris@0: */ Chris@0: protected $weight = 0; Chris@0: Chris@0: /** Chris@0: * Locked indicates a language used by the system, not an actual language. Chris@0: * Chris@0: * Examples of locked languages are, LANGCODE_NOT_SPECIFIED, und, and Chris@0: * LANGCODE_NOT_APPLICABLE, zxx, which are usually shown in language selects Chris@0: * but hidden in places like the Language configuration and cannot be deleted. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $locked = FALSE; Chris@0: Chris@0: /** Chris@0: * Constructs a new class instance. Chris@0: * Chris@0: * @param array $values Chris@0: * An array of property values, keyed by property name, used to construct Chris@0: * the language. Chris@0: */ Chris@0: public function __construct(array $values = []) { Chris@0: // Set all the provided properties for the language. Chris@0: foreach ($values as $key => $value) { Chris@0: if (property_exists($this, $key)) { Chris@0: $this->{$key} = $value; Chris@0: } Chris@0: } Chris@0: // If some values were not set, set sane defaults of a predefined language. Chris@0: if (!isset($values['name']) || !isset($values['direction'])) { Chris@0: $predefined = LanguageManager::getStandardLanguageList(); Chris@0: if (isset($predefined[$this->id])) { Chris@0: if (!isset($values['name'])) { Chris@0: $this->name = $predefined[$this->id][0]; Chris@0: } Chris@0: if (!isset($values['direction']) && isset($predefined[$this->id][2])) { Chris@0: $this->direction = $predefined[$this->id][2]; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() { Chris@0: return $this->name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getId() { Chris@0: return $this->id; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDirection() { Chris@0: return $this->direction; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getWeight() { Chris@0: return $this->weight; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isDefault() { Chris@0: return static::getDefaultLangcode() == $this->getId(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isLocked() { Chris@0: return (bool) $this->locked; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sort language objects. Chris@0: * Chris@0: * @param \Drupal\Core\Language\LanguageInterface[] $languages Chris@0: * The array of language objects keyed by langcode. Chris@0: */ Chris@0: public static function sort(&$languages) { Chris@0: uasort($languages, function (LanguageInterface $a, LanguageInterface $b) { Chris@0: $a_weight = $a->getWeight(); Chris@0: $b_weight = $b->getWeight(); Chris@0: if ($a_weight == $b_weight) { Chris@0: $a_name = $a->getName(); Chris@0: $b_name = $b->getName(); Chris@0: // If either name is a TranslatableMarkup object it can not be converted Chris@0: // to a string. This is because translation requires a sorted list of Chris@0: // languages thereby causing an infinite loop. Determine the order based Chris@0: // on ID if this is the case. Chris@0: if ($a_name instanceof TranslatableMarkup || $b_name instanceof TranslatableMarkup) { Chris@0: $a_name = $a->getId(); Chris@0: $b_name = $b->getId(); Chris@0: } Chris@0: return strnatcasecmp($a_name, $b_name); Chris@0: } Chris@0: return ($a_weight < $b_weight) ? -1 : 1; Chris@0: }); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the default langcode. Chris@0: * Chris@0: * @return string Chris@0: * The current default langcode. Chris@0: */ Chris@0: protected static function getDefaultLangcode() { Chris@0: $language = \Drupal::service('language.default')->get(); Chris@0: return $language->getId(); Chris@0: } Chris@0: Chris@0: }