1. 8.2.x core/lib/Drupal/Core/Annotation/PluralTranslation.php
  2. 8.1.x core/lib/Drupal/Core/Annotation/PluralTranslation.php
  3. 8.3.x core/lib/Drupal/Core/Annotation/PluralTranslation.php

Namespace

Drupal\Core\Annotation

File

core/lib/Drupal/Core/Annotation/PluralTranslation.php
View source
  1. <?php
  2. namespace Drupal\Core\Annotation;
  3. use Drupal\Component\Annotation\AnnotationBase;
  4. /**
  5. * Defines an annotation object for strings that require plural forms.
  6. *
  7. * Note that the return values for both 'singular' and 'plural' keys needs to be
  8. * passed to
  9. * \Drupal\Core\StringTranslation\TranslationInterface::formatPlural().
  10. *
  11. * For example, the annotation can look like this:
  12. * @code
  13. * label_count = @ PluralTranslation(
  14. * singular = "@count item",
  15. * plural = "@count items",
  16. * context = "cart_items",
  17. * ),
  18. * @endcode
  19. * Remove spaces after @ in your actual plugin - these are put into this sample
  20. * code so that it is not recognized as annotation.
  21. *
  22. * Code samples that make use of this annotation class and the definition sample
  23. * above:
  24. * @code
  25. * // Returns: 1 item
  26. * $entity_type->getCountLabel(1);
  27. *
  28. * // Returns: 5 items
  29. * $entity_type->getCountLabel(5);
  30. * @endcode
  31. *
  32. * @see \Drupal\Core\Entity\EntityType::getSingularLabel()
  33. * @see \Drupal\Core\Entity\EntityType::getPluralLabel()
  34. * @see \Drupal\Core\Entity\EntityType::getCountLabel()
  35. *
  36. * @ingroup plugin_translatable
  37. *
  38. * @Annotation
  39. */
  40. class PluralTranslation extends AnnotationBase {
  41. /**
  42. * The string for the singular case.
  43. *
  44. * @var string
  45. */
  46. protected $singular;
  47. /**
  48. * The string for the plural case.
  49. *
  50. * @var string
  51. */
  52. protected $plural;
  53. /**
  54. * The context the source strings belong to.
  55. *
  56. * @var string
  57. */
  58. protected $context;
  59. /**
  60. * Constructs a new class instance.
  61. *
  62. * @param array $values
  63. * An associative array with the following keys:
  64. * - singular: The string for the singular case.
  65. * - plural: The string for the plural case.
  66. * - context: The context the source strings belong to.
  67. *
  68. * @throws \InvalidArgumentException
  69. * Thrown when the keys 'singular' or 'plural' are missing from the $values
  70. * array.
  71. */
  72. public function __construct(array $values) {
  73. if (!isset($values['singular'])) {
  74. throw new \InvalidArgumentException('Missing "singular" value in the PluralTranslation annotation');
  75. }
  76. if (!isset($values['plural'])) {
  77. throw new \InvalidArgumentException('Missing "plural" value in the PluralTranslation annotation');
  78. }
  79. $this->singular = $values['singular'];
  80. $this->plural = $values['plural'];
  81. if (isset($values['context'])) {
  82. $this->context = $values['context'];
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function get() {
  89. return [
  90. 'singular' => $this->singular,
  91. 'plural' => $this->plural,
  92. 'context' => $this->context,
  93. ];
  94. }
  95. }

Classes

Namesort descending Description
PluralTranslation Defines an annotation object for strings that require plural forms.