class PluralTranslation
Same name in other branches
- 9 core/lib/Drupal/Core/Annotation/PluralTranslation.php \Drupal\Core\Annotation\PluralTranslation
- 8.9.x core/lib/Drupal/Core/Annotation/PluralTranslation.php \Drupal\Core\Annotation\PluralTranslation
- 11.x core/lib/Drupal/Core/Annotation/PluralTranslation.php \Drupal\Core\Annotation\PluralTranslation
Defines an annotation object for strings that require plural forms.
Note that the return values for both 'singular' and 'plural' keys needs to be passed to \Drupal\Core\StringTranslation\TranslationInterface::formatPlural().
For example, the annotation can look like this:
label_count = @ PluralTranslation(
singular = "@count item",
plural = "@count items",
context = "cart_items",
),
Remove spaces after @ in your actual plugin - these are put into this sample code so that it is not recognized as annotation.
Code samples that make use of this annotation class and the definition sample above:
// Returns: 1 item
$entity_type->getCountLabel(1);
// Returns: 5 items
$entity_type->getCountLabel(5);
Hierarchy
- class \Drupal\Component\Annotation\AnnotationBase implements \Drupal\Component\Annotation\AnnotationInterface
- class \Drupal\Core\Annotation\PluralTranslation extends \Drupal\Component\Annotation\AnnotationBase
Expanded class hierarchy of PluralTranslation
See also
\Drupal\Core\Entity\EntityType::getSingularLabel()
\Drupal\Core\Entity\EntityType::getPluralLabel()
\Drupal\Core\Entity\EntityType::getCountLabel()
Related topics
1 file declares its use of PluralTranslation
- PluralTranslationTest.php in core/
tests/ Drupal/ Tests/ Core/ Annotation/ PluralTranslationTest.php
File
-
core/
lib/ Drupal/ Core/ Annotation/ PluralTranslation.php, line 43
Namespace
Drupal\Core\AnnotationView source
class PluralTranslation extends AnnotationBase {
/**
* The string for the singular case.
*
* @var string
*/
protected $singular;
/**
* The string for the plural case.
*
* @var string
*/
protected $plural;
/**
* The context the source strings belong to.
*
* @var string
*/
protected $context;
/**
* Constructs a new class instance.
*
* @param array $values
* An associative array with the following keys:
* - singular: The string for the singular case.
* - plural: The string for the plural case.
* - context: The context the source strings belong to.
*
* @throws \InvalidArgumentException
* Thrown when the keys 'singular' or 'plural' are missing from the $values
* array.
*/
public function __construct(array $values) {
if (!isset($values['singular'])) {
throw new \InvalidArgumentException('Missing "singular" value in the PluralTranslation annotation');
}
if (!isset($values['plural'])) {
throw new \InvalidArgumentException('Missing "plural" value in the PluralTranslation annotation');
}
$this->singular = $values['singular'];
$this->plural = $values['plural'];
if (isset($values['context'])) {
$this->context = $values['context'];
}
}
/**
* {@inheritdoc}
*/
public function get() {
return [
'singular' => $this->singular,
'plural' => $this->plural,
'context' => $this->context,
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
AnnotationBase::$class | protected | property | The class used for this annotated class. | ||
AnnotationBase::$id | public | property | The annotated class ID. | 1 | |
AnnotationBase::$provider | protected | property | The provider of the annotated class. | ||
AnnotationBase::getClass | public | function | Gets the class of the annotated class. | Overrides AnnotationInterface::getClass | |
AnnotationBase::getId | public | function | Gets the unique ID for this annotated class. | Overrides AnnotationInterface::getId | 1 |
AnnotationBase::getProvider | public | function | Gets the name of the provider of the annotated class. | Overrides AnnotationInterface::getProvider | |
AnnotationBase::setClass | public | function | Sets the class of the annotated class. | Overrides AnnotationInterface::setClass | |
AnnotationBase::setProvider | public | function | Sets the name of the provider of the annotated class. | Overrides AnnotationInterface::setProvider | |
PluralTranslation::$context | protected | property | The context the source strings belong to. | ||
PluralTranslation::$plural | protected | property | The string for the plural case. | ||
PluralTranslation::$singular | protected | property | The string for the singular case. | ||
PluralTranslation::get | public | function | Gets the value of an annotation. | Overrides AnnotationInterface::get | |
PluralTranslation::__construct | public | function | Constructs a new class instance. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.