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 int $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; } /** * Renders the object as a string. * * @return string * The translated string. */ public function render() { if (!$this->translatedString) { $this->translatedString = $this->getStringTranslation()->translateString($this); } if ($this->translatedString === '') { return ''; } $arguments = $this->getArguments(); $arguments['@count'] = $this->count; $translated_array = explode(PoItem::DELIMITER, $this->translatedString); $index = $this->getPluralIndex(); if ($this->count == 1 || $index == 0 || count($translated_array) == 1) { // Singular form. $return = $translated_array[0]; } else { // Nth plural form, fallback to second plural form. $return = $translated_array[$index] ?? $translated_array[1]; } return $this->placeholderFormat($return, $arguments); } /** * Gets the plural index through the gettext formula. * * @return int */ protected function getPluralIndex() { // We have to test both if the function and the service exist since in // certain situations it is possible that locale code might be loaded but // the service does not exist. For example, where the parent test site has // locale installed but the child site does not. // @todo Refactor in https://www.drupal.org/node/2660338 so this code does // not depend on knowing that the Locale module exists. if (function_exists('locale_get_plural') && \Drupal::hasService('locale.plural.formula')) { return locale_get_plural($this->count, $this->getOption('langcode')); } return -1; } /** * {@inheritdoc} */ public function __sleep(): array { return array_merge(parent::__sleep(), ['count']); } }