class PluralTranslatableMarkup

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php \Drupal\Core\StringTranslation\PluralTranslatableMarkup
  2. 10 core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php \Drupal\Core\StringTranslation\PluralTranslatableMarkup
  3. 9 core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php \Drupal\Core\StringTranslation\PluralTranslatableMarkup
  4. 8.9.x core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php \Drupal\Core\StringTranslation\PluralTranslatableMarkup

A class to hold plural translatable markup.

Hierarchy

Expanded class hierarchy of PluralTranslatableMarkup

15 files declare their use of PluralTranslatableMarkup
ByteSizeMarkupTest.php in core/tests/Drupal/Tests/Core/StringTranslation/ByteSizeMarkupTest.php
FieldStorageReuseForm.php in core/modules/field_ui/src/Form/FieldStorageReuseForm.php
LocalePluralFormatTest.php in core/modules/locale/tests/src/Functional/LocalePluralFormatTest.php
MigrateUpgradeImportBatch.php in core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php
ModulesEnabledTrait.php in core/modules/system/src/Form/ModulesEnabledTrait.php

... See full list

File

core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php, line 10

Namespace

Drupal\Core\StringTranslation
View source
class PluralTranslatableMarkup extends TranslatableMarkup {
  
  /**
   * The item count to display.
   *
   * @var numeric
   */
  protected $count;
  
  /**
   * The already translated string.
   *
   * @var string
   */
  protected $translatedString;
  
  /**
   * Constructs a new PluralTranslatableMarkup object.
   *
   * Parses values passed into this class through the format_plural() function
   * in Drupal and handles an optional context for the string.
   *
   * @param numeric $count
   *   The item count to display.
   * @param string $singular
   *   The string for the singular case. Make sure it is clear this is singular,
   *   to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not
   *   use @count in the singular string.
   * @param string $plural
   *   The string for the plural case. Make sure it is clear this is plural, to
   *   ease translation. Use @count in place of the item count, as in
   *   "@count new comments".
   * @param array $args
   *   (optional) An array with placeholder replacements, keyed by placeholder.
   *   See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
   *   additional information about placeholders. Note that you do not need to
   *   include @count in this array; this replacement is done automatically
   *   for the plural cases.
   * @param array $options
   *   (optional) An associative array of additional options. See t() for
   *   allowed keys.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   (optional) The string translation service.
   *
   * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
   */
  public function __construct($count, $singular, $plural, array $args = [], array $options = [], ?TranslationInterface $string_translation = NULL) {
    $this->count = $count;
    $translatable_string = implode(PoItem::DELIMITER, [
      $singular,
      $plural,
    ]);
    parent::__construct($translatable_string, $args, $options, $string_translation);
  }
  
  /**
   * Constructs a new class instance from already translated markup.
   *
   * This method ensures that the string is pluralized correctly. As opposed
   * to the __construct() method, this method is designed to be invoked with
   * a string already translated (such as with configuration translation).
   *
   * @param numeric $count
   *   The item count to display.
   * @param string $translated_string
   *   The already translated string.
   * @param array $args
   *   An associative array of replacements to make after translation. Instances
   *   of any key in this array are replaced with the corresponding value.
   *   Based on the first character of the key, the value is escaped and/or
   *   themed. See \Drupal\Component\Render\FormattableMarkup. Note that you
   *   do not need to include @count in this array; this replacement is done
   *   automatically for the plural cases.
   * @param array $options
   *   An associative array of additional options. See t() for allowed keys.
   *
   * @return static
   *   A PluralTranslatableMarkup object.
   */
  public static function createFromTranslatedString($count, $translated_string, array $args = [], array $options = []) {
    $plural = new static($count, '', '', $args, $options);
    $plural->translatedString = $translated_string;
    return $plural;
  }
  
  /**
   * {@inheritdoc}
   */
  protected function translateString() : string {
    $this->translatedString ??= parent::translateString();
    return $this->getStringTranslation()
      ->selectPluralForm($this->count, $this->translatedString, $this->getOptions()['langcode'] ?? NULL);
  }
  
  /**
   * {@inheritdoc}
   */
  public function getArguments() : array {
    return parent::getArguments() + [
      '@count' => $this->count,
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function __sleep() : array {
    return array_merge(parent::__sleep(), [
      'count',
    ]);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FormattableMarkup::$arguments protected property The arguments to replace placeholders with.
FormattableMarkup::$string protected property The string containing placeholders.
FormattableMarkup::jsonSerialize public function Returns a representation of the object for use in JSON serialization.
FormattableMarkup::placeholderEscape protected static function Escapes a placeholder replacement value if needed.
FormattableMarkup::placeholderFormat protected static function Replaces placeholders in a string with values.
PluralTranslatableMarkup::$count protected property The item count to display.
PluralTranslatableMarkup::$translatedString protected property The already translated string.
PluralTranslatableMarkup::createFromTranslatedString public static function Constructs a new class instance from already translated markup.
PluralTranslatableMarkup::getArguments public function Gets all arguments from this translated string. Overrides TranslatableMarkup::getArguments
PluralTranslatableMarkup::translateString protected function Translates the string. Overrides TranslatableMarkup::translateString
PluralTranslatableMarkup::__construct public function Constructs a new PluralTranslatableMarkup object. Overrides TranslatableMarkup::__construct
PluralTranslatableMarkup::__sleep public function Magic __sleep() method to avoid serializing the string translator. Overrides TranslatableMarkup::__sleep
TranslatableMarkup::$options protected property The translation options.
TranslatableMarkup::$stringTranslation protected property The string translation service.
TranslatableMarkup::$translatedMarkup protected property The translated markup without placeholder replacements.
TranslatableMarkup::count public function Returns the string length. Overrides FormattableMarkup::count
TranslatableMarkup::getOption public function Gets a specific option from this translated string.
TranslatableMarkup::getOptions public function Gets all options from this translated string.
TranslatableMarkup::getStringTranslation protected function Gets the string translation service.
TranslatableMarkup::getUntranslatedString public function Gets the untranslated string value stored in this translated string.
TranslatableMarkup::render public function Renders the object as a string.
TranslatableMarkup::__toString public function Returns the string representation of this object. Overrides FormattableMarkup::__toString

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.