annotate core/lib/Drupal/Core/Annotation/PluralTranslation.php @ 19:fa3358dc1485 tip

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