class PluralTranslation in Drupal 8
Same name and namespace in other branches
- 9 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 AnnotationInterface
- class \Drupal\Core\Annotation\PluralTranslation
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AnnotationBase:: |
protected | property | The class used for this annotated class. | |
AnnotationBase:: |
public | property | The annotated class ID. | 1 |
AnnotationBase:: |
protected | property | The provider of the annotated class. | |
AnnotationBase:: |
public | function |
Gets the class of the annotated class. Overrides AnnotationInterface:: |
|
AnnotationBase:: |
public | function |
Gets the unique ID for this annotated class. Overrides AnnotationInterface:: |
1 |
AnnotationBase:: |
public | function |
Gets the name of the provider of the annotated class. Overrides AnnotationInterface:: |
|
AnnotationBase:: |
public | function |
Sets the class of the annotated class. Overrides AnnotationInterface:: |
|
AnnotationBase:: |
public | function |
Sets the name of the provider of the annotated class. Overrides AnnotationInterface:: |
|
PluralTranslation:: |
protected | property | The context the source strings belong to. | |
PluralTranslation:: |
protected | property | The string for the plural case. | |
PluralTranslation:: |
protected | property | The string for the singular case. | |
PluralTranslation:: |
public | function |
Gets the value of an annotation. Overrides AnnotationInterface:: |
|
PluralTranslation:: |
public | function | Constructs a new class instance. |