class PluralTranslatableMarkup in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php \Drupal\Core\StringTranslation\PluralTranslatableMarkup
A class to hold plural translatable markup.
Hierarchy
- class \Drupal\Component\Render\FormattableMarkup implements \Drupal\Component\Render\Countable, MarkupInterface
- class \Drupal\Core\StringTranslation\TranslatableMarkup uses ToStringTrait
- class \Drupal\Core\StringTranslation\PluralTranslatableMarkup
- class \Drupal\Core\StringTranslation\TranslatableMarkup uses ToStringTrait
Expanded class hierarchy of PluralTranslatableMarkup
5 files declare their use of PluralTranslatableMarkup
- common.inc in core/
includes/ common.inc - Common functions that many Drupal modules will need to reference.
- LocalePluralFormatTest.php in core/
modules/ locale/ src/ Tests/ LocalePluralFormatTest.php - Contains \Drupal\locale\Tests\LocalePluralFormatTest.
- NumericField.php in core/
modules/ views/ src/ Plugin/ views/ field/ NumericField.php - Contains \Drupal\views\Plugin\views\field\NumericField.
- PermissionHandlerTest.php in core/
modules/ user/ tests/ src/ Unit/ PermissionHandlerTest.php - Contains \Drupal\Tests\user\Unit\PermissionHandlerTest.
- UnitTestCase.php in core/
tests/ Drupal/ Tests/ UnitTestCase.php - Contains \Drupal\Tests\UnitTestCase.
File
- core/
lib/ Drupal/ Core/ StringTranslation/ PluralTranslatableMarkup.php, line 13 - Contains \Drupal\Core\StringTranslation\PluralTranslatableMarkup.
Namespace
Drupal\Core\StringTranslationView source
class PluralTranslatableMarkup extends TranslatableMarkup {
/**
* The delimiter used to split plural strings.
*
* This is the ETX (End of text) character and is used as a minimal means to
* separate singular and plural variants in source and translation text. It
* was found to be the most compatible delimiter for the supported databases.
*/
const DELIMITER = "\3";
/**
* The item count to display.
*
* @var int
*/
protected $count;
/**
* The already translated string.
*
* @var string
*/
protected $translatedString;
/**
* A bool that statically caches whether locale_get_plural() exists.
*
* @var bool
*/
protected static $localeEnabled;
/**
* 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 int $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(static::DELIMITER, array(
$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\Utility\SafeMarkup::format(). 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 \Drupal\Core\StringTranslation\PluralTranslatableMarkup
* 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(static::DELIMITER, $this->translatedString);
if ($this->count == 1) {
return $this
->placeholderFormat($translated_array[0], $arguments);
}
$index = $this
->getPluralIndex();
if ($index == 0) {
// Singular form.
$return = $translated_array[0];
}
else {
if (isset($translated_array[$index])) {
// N-th plural form.
$return = $translated_array[$index];
}
else {
// If the index cannot be computed or there's no translation, use the
// second plural form as a fallback (which allows for most flexibility
// with the replaceable @count value).
$return = $translated_array[1];
}
}
return $this
->placeholderFormat($return, $arguments);
}
/**
* Gets the plural index through the gettext formula.
*
* @return int
*/
protected function getPluralIndex() {
if (!isset(static::$localeEnabled)) {
static::$localeEnabled = function_exists('locale_get_plural');
}
if (function_exists('locale_get_plural')) {
return locale_get_plural($this->count, $this
->getOption('langcode'));
}
return -1;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormattableMarkup:: |
protected | property | The arguments to replace placeholders with. | |
FormattableMarkup:: |
public | function | Returns a representation of the object for use in JSON serialization. | |
FormattableMarkup:: |
protected static | function | Escapes a placeholder replacement value if needed. | |
FormattableMarkup:: |
protected static | function | Replaces placeholders in a string with values. | |
PluralTranslatableMarkup:: |
protected | property | The item count to display. | |
PluralTranslatableMarkup:: |
protected static | property | A bool that statically caches whether locale_get_plural() exists. | |
PluralTranslatableMarkup:: |
protected | property | The already translated string. | |
PluralTranslatableMarkup:: |
public static | function | Constructs a new class instance from already translated markup. | |
PluralTranslatableMarkup:: |
constant | The delimiter used to split plural strings. | ||
PluralTranslatableMarkup:: |
protected | function | Gets the plural index through the gettext formula. | |
PluralTranslatableMarkup:: |
public | function |
Renders the object as a string. Overrides TranslatableMarkup:: |
|
PluralTranslatableMarkup:: |
public | function |
Constructs a new PluralTranslatableMarkup object. Overrides TranslatableMarkup:: |
|
ToStringTrait:: |
protected | function | For test purposes, wrap die() in an overridable method. | |
ToStringTrait:: |
public | function | Implements the magic __toString() method. | |
TranslatableMarkup:: |
protected | property | The translation options. | |
TranslatableMarkup:: |
protected | property | The string to be translated. | |
TranslatableMarkup:: |
protected | property | The string translation service. | |
TranslatableMarkup:: |
protected | property | The translated markup without placeholder replacements. | |
TranslatableMarkup:: |
public | function |
Returns the string length. Overrides FormattableMarkup:: |
|
TranslatableMarkup:: |
public | function | Gets all argments from this translated string. | |
TranslatableMarkup:: |
public | function | Gets a specific option from this translated string. | |
TranslatableMarkup:: |
public | function | Gets all options from this translated string. | |
TranslatableMarkup:: |
protected | function | Gets the string translation service. | |
TranslatableMarkup:: |
public | function | Gets the untranslated string value stored in this translated string. | |
TranslatableMarkup:: |
public | function | Magic __sleep() method to avoid serializing the string translator. |