3 namespace Drupal\Core\Annotation;
5 use Drupal\Component\Annotation\AnnotationBase;
8 * Defines an annotation object for strings that require plural forms.
10 * Note that the return values for both 'singular' and 'plural' keys needs to be
12 * \Drupal\Core\StringTranslation\TranslationInterface::formatPlural().
14 * For example, the annotation can look like this:
16 * label_count = @ PluralTranslation(
17 * singular = "@count item",
18 * plural = "@count items",
19 * context = "cart_items",
22 * Remove spaces after @ in your actual plugin - these are put into this sample
23 * code so that it is not recognized as annotation.
25 * Code samples that make use of this annotation class and the definition sample
29 * $entity_type->getCountLabel(1);
32 * $entity_type->getCountLabel(5);
35 * @see \Drupal\Core\Entity\EntityType::getSingularLabel()
36 * @see \Drupal\Core\Entity\EntityType::getPluralLabel()
37 * @see \Drupal\Core\Entity\EntityType::getCountLabel()
39 * @ingroup plugin_translatable
43 class PluralTranslation extends AnnotationBase {
46 * The string for the singular case.
53 * The string for the plural case.
60 * The context the source strings belong to.
67 * Constructs a new class instance.
69 * @param array $values
70 * An associative array with the following keys:
71 * - singular: The string for the singular case.
72 * - plural: The string for the plural case.
73 * - context: The context the source strings belong to.
75 * @throws \InvalidArgumentException
76 * Thrown when the keys 'singular' or 'plural' are missing from the $values
79 public function __construct(array $values) {
80 if (!isset($values['singular'])) {
81 throw new \InvalidArgumentException('Missing "singular" value in the PluralTranslation annotation');
83 if (!isset($values['plural'])) {
84 throw new \InvalidArgumentException('Missing "plural" value in the PluralTranslation annotation');
87 $this->singular = $values['singular'];
88 $this->plural = $values['plural'];
89 if (isset($values['context'])) {
90 $this->context = $values['context'];
97 public function get() {
99 'singular' => $this->singular,
100 'plural' => $this->plural,
101 'context' => $this->context,