PluralTranslation.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }