diff --git a/core/lib/Drupal/Core/Annotation/PluralTranslation.php b/core/lib/Drupal/Core/Annotation/PluralTranslation.php index d242ca4..b2f14bf 100644 --- a/core/lib/Drupal/Core/Annotation/PluralTranslation.php +++ b/core/lib/Drupal/Core/Annotation/PluralTranslation.php @@ -11,45 +11,63 @@ /** * Defines an annotation object for strings that require plural forms. Note that - * this object does not perform the actual translation, so its output needs to - * be passed to format_plural(). + * this object only performs the actual translation on the 'generic' key, so its + * return values for the 'singular' and 'plural' keys needs to be passed to + * format_plural(). * - * For example, the annotation needs to look like this: + * For example, the annotation can to look like this: * @code - * count_labels = @ PluralTranslation(singular = "1 item", plural = "@count items"), + * count_labels = @ PluralTranslation( + * singular = "1 item", + * plural = "@count items", + * generic = "items" + * ), * @endcode * Remove spaces after @ in your actual plugin - these are put into this sample * code so that it is not recognized as annotation. * - * An example of code that uses the annotation above can be: + * Code samples that make use of this annotation class and the definition sample + * above: * @code - * format_plural($count, $info['count_labels']['singular'], $info['count_labels']['plural']); + * // Returns: 1 item + * echo $entity->entityTypeLabel(TRUE, 1); + * + * // Returns: 5 items + * echo $entity->entityTypeLabel(TRUE, 5); + * + * // Returns: items + * echo $entity->entityTypeLabel(TRUE); * @endcode * * @Annotation * + * @see \Drupal\Core\Entity\Entity::entityTypeLabel() + * * @ingroup plugin_translatable */ -class PluralTranslation implements AnnotationInterface { +class PluralTranslation extends Translation implements AnnotationInterface { /** * The string for the singular case. * * @var string */ - public $singular; + protected $singular; /** * The string for the plural case. * * @var string */ - public $plural; + protected $plural; /** * Constructs a PluralTranslation object. */ public function __construct($values) { + $values['value'] = $values['generic']; + parent::__construct($values); + $this->singular = $values['singular']; $this->plural = $values['plural']; } @@ -61,6 +79,7 @@ public function get() { return array( 'singular' => $this->singular, 'plural' => $this->plural, + 'generic' => $this->translation ); } diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index e9b1584..d93d5a1 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -129,14 +129,17 @@ public function entityType() { /** * {@inheritdoc} */ - public function entityTypeLabel($count = NULL) { + public function entityTypeLabel($plural = FALSE, $count = NULL) { $info = $this->entityInfo(); - if (!$count) { + if (!$plural) { // We don't have to use t() here because the string is already translated // by the Translation annotation class. return $info['label']; } - else { + elseif ($plural && !isset($count)) { + return $info['count_labels']['generic']; + } + elseif ($plural && isset($count)) { return format_plural($count, $info['count_labels']['singular'], $info['count_labels']['plural']); } } diff --git a/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php index a099269..a06f749 100644 --- a/core/lib/Drupal/Core/Entity/EntityInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityInterface.php @@ -105,16 +105,17 @@ public function enforceIsNew($value = TRUE); public function entityType(); /** - * Returns the translated label of the entity type. If the $count parameter is - * provided, the 'count_label' is returned. + * Returns the translated label of the entity type. * + * @param bool $plural + * (optional) Whether the generic plural form should be returned. * @param int $count - * (optional) The item count to display. + * (optional) The item count to display if the plural form was requested. * * @return string * The translated label. */ - public function entityTypeLabel($count = NULL); + public function entityTypeLabel($plural = FALSE, $count = NULL); /** * Returns the bundle of the entity. diff --git a/core/modules/node/lib/Drupal/node/Plugin/Core/Entity/Node.php b/core/modules/node/lib/Drupal/node/Plugin/Core/Entity/Node.php index 529594d..2e208b6 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/Core/Entity/Node.php +++ b/core/modules/node/lib/Drupal/node/Plugin/Core/Entity/Node.php @@ -21,7 +21,11 @@ * @EntityType( * id = "node", * label = @Translation("Content"), - * count_labels = @PluralTranslation(singular = "1 content item", plural = "@count content items"), + * count_labels = @PluralTranslation( + * singular = "1 content item", + * plural = "@count content items", + * generic = "content items" + * ), * bundle_label = @Translation("Content type"), * module = "node", * controllers = {