Same name and namespace in other branches
  1. 4.6.x includes/common.inc \format_plural()
  2. 4.7.x includes/common.inc \format_plural()
  3. 6.x includes/common.inc \format_plural()
  4. 7.x includes/common.inc \format_plural()

Format 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.

Parameters

$count: The item count to display.

$singular: The string for the singular case. Please make sure it is clear this is singular, to ease translation (e.g. use "1 new comment" instead of "1 new").

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

Return value

A translated string.

Related topics

17 calls to format_plural()
aggregator_view in modules/aggregator/aggregator.module
comment_link in modules/comment/comment.module
Implementation of hook_link().
format_interval in includes/common.inc
Format a time interval with the requested granularity.
format_size in includes/common.inc
Generate a string representation for the given byte count.
hook_link in developer/hooks/core.php
Define internal Drupal links.

... See full list

File

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

Code

function format_plural($count, $singular, $plural) {
  if ($count == 1) {
    return t($singular, array(
      "@count" => $count,
    ));
  }

  // get the plural index through the gettext formula
  $index = function_exists('locale_get_plural') ? locale_get_plural($count) : -1;
  if ($index < 0) {

    // backward compatibility
    return t($plural, array(
      "@count" => $count,
    ));
  }
  else {
    switch ($index) {
      case "0":
        return t($singular, array(
          "@count" => $count,
        ));
      case "1":
        return t($plural, array(
          "@count" => $count,
        ));
      default:
        return t(strtr($plural, array(
          "@count" => '@count[' . $index . ']',
        )), array(
          '@count[' . $index . ']' => $count,
        ));
    }
  }
}