function format_plural

You are here

7 common.inc format_plural($count, $singular, $plural, array $args = array(), array $options = array())
4.6 common.inc format_plural($count, $singular, $plural)
4.7 common.inc format_plural($count, $singular, $plural)
5 common.inc format_plural($count, $singular, $plural)
6 common.inc format_plural($count, $singular, $plural, $args = array(), $langcode = NULL)
8 common.inc format_plural($count, $singular, $plural, array $args = array(), array $options = array())

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.

Deprecated

in Drupal 8.x-dev, will be removed before Drupal 8.0. Use \Drupal::translation()->formatPlural().

See also

t()

format_string()

\Drupal\Core\StringTranslation\TranslationManager->formatPlural()

Related topics

64 calls to format_plural()
AggregatorController::adminOverview in core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php
Displays the aggregator administration page.
CommentNonNodeTest::performCommentOperation in core/modules/comment/lib/Drupal/comment/Tests/CommentNonNodeTest.php
Performs the specified operation on the specified comment.
CommentTestBase::performCommentOperation in core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php
Performs the specified operation on the specified comment.
comment_node_links_alter in core/modules/comment/comment.module
Implements hook_node_links_alter().
comment_node_search_result in core/modules/comment/comment.module
Implements hook_node_search_result().

... See full list

4 string references to 'format_plural'
Numeric::buildOptionsForm in core/modules/views/lib/Drupal/views/Plugin/views/field/Numeric.php
Default options form that provides the label widget that all fields should have.
Numeric::defineOptions in core/modules/views/lib/Drupal/views/Plugin/views/field/Numeric.php
Information about options for all kinds of purposes will be held here. @code 'option_name' => array(
Numeric::render in core/modules/views/lib/Drupal/views/Plugin/views/field/Numeric.php
Renders the field.
TwigNodeTrans::compile in core/lib/Drupal/Core/Template/TwigNodeTrans.php
Compiles the node to PHP.

File

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

Code

function format_plural($count, $singular, $plural, array $args = array(), array $options = array()) {
  return \Drupal::translation()->formatPlural($count, $singular, $plural, $args, $options);
}