function format_plural

Formats a string containing a count of items.

This function ensures that the string is pluralized correctly. Since t() is called by this function, make sure not to pass already-localized strings to it.

For example:

$output = format_plural($node->comment_count, '1 comment', '@count comments');

Example with additional replacements:

$output = format_plural($update_count, 'Changed the content type of 1 post from %old-type to %new-type.', 'Changed the content type of @count posts from %old-type to %new-type.', array(
  '%old-type' => $info->old_type,
  '%new-type' => $info->new_type,
));

Parameters

$count: The item count to display.

$singular: The string for the singular case. Make sure it is clear this is singular, to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not use @count in the singular string.

$plural: The string for the plural case. Make sure it is clear this is plural, to ease translation. Use @count in place of the item count, as in "@count new comments".

$args: An associative array of replacements to make after translation. Instances of any key in this array are replaced with the corresponding value. Based on the first character of the key, the value is escaped and/or themed. See format_string(). Note that you do not need to include @count in this array; this replacement is done automatically for the plural case.

$options: An associative array of additional options. See t() for allowed keys.

Return value

A translated string.

See also

t()

format_string()

Related topics

64 calls to format_plural()
aggregator_view in drupal/core/modules/aggregator/aggregator.admin.inc
Displays the aggregator administration page.
CommentTestBase::performCommentOperation in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php
Performs the specified operation on the specified comment.
comment_multiple_delete_confirm_submit in drupal/core/modules/comment/comment.admin.inc
Form submission handler for comment_multiple_delete_confirm().
comment_node_search_result in drupal/core/modules/comment/comment.module
Implements hook_node_search_result().
comment_node_view in drupal/core/modules/comment/comment.module
Implements hook_node_view().

... See full list

File

drupal/core/includes/common.inc, line 1687
Common functions that many Drupal modules will need to reference.

Code

function format_plural($count, $singular, $plural, array $args = array(), array $options = array()) {
  $args['@count'] = $count;

  // Join both forms to search a translation.
  $tranlatable_string = implode(LOCALE_PLURAL_DELIMITER, array(
    $singular,
    $plural,
  ));

  // Translate as usual.
  $translated_strings = t($tranlatable_string, $args, $options);

  // Split joined translation strings into array.
  $translated_array = explode(LOCALE_PLURAL_DELIMITER, $translated_strings);
  if ($count == 1) {
    return $translated_array[0];
  }

  // Get the plural index through the gettext formula.
  // @todo implement static variable to minimize function_exists() usage.
  $index = function_exists('locale_get_plural') ? locale_get_plural($count, isset($options['langcode']) ? $options['langcode'] : NULL) : -1;
  if ($index == 0) {

    // Singular form.
    return $translated_array[0];
  }
  else {
    if (isset($translated_array[$index])) {

      // N-th plural form.
      return $translated_array[$index];
    }
    else {

      // If the index cannot be computed or there's no translation, use
      // the second plural form as a fallback (which allows for most flexiblity
      // with the replaceable @count value).
      return $translated_array[1];
    }
  }
}