format_plural

Versions
4.6 – 5
format_plural($count, $singular, $plural)
6
format_plural($count, $singular, $plural, $args = array(), $langcode = NULL)
7
format_plural($count, $singular, $plural, array $args = array(), array $options = array())

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

▾ 18 functions call format_plural()

aggregator_view in modules/aggregator.module
archive_calendar in modules/archive.module
Generates a monthly calendar, for display in the archive block.
comment_link in modules/comment.module
Implementation of hook_link().
comment_nodeapi in modules/comment.module
Implementation of hook_nodeapi().
format_interval in includes/common.inc
Format a time interval with the requested granularity.
node_title_list in modules/node.module
Gather a listing of links to nodes.
poll_page in modules/poll.module
poll_view_results in modules/poll.module
Generates a graphical representation of the results of a poll.
search_settings in modules/search.module
Menu callback; displays the search module settings page.
statistics_link in modules/statistics.module
Implementation of hook_link().
theme_forum_list in modules/forum.module
Format the forum listing.
theme_forum_topic_list in modules/forum.module
Format the topic listing.
throttle_exit in modules/throttle.module
Implementation of hook_exit().
tracker_page in modules/tracker.module
Menu callback. Prints a listing of active nodes on the site.
upload_link in modules/upload.module
Implementation of hook_link().
upload_nodeapi in modules/upload.module
Implementation of hook_nodeapi().
user_block in modules/user.module
Implementation of hook_block().
_aggregator_items in modules/aggregator.module
Helper function for drupal_map_assoc.

Code

includes/common.inc, line 821

<?php
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));
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.