format_plural

  1. drupal
    1. drupal6
    2. drupal7
Versionen
drupal6 – drupal7 format_plural($count, $singular, $plural, $args = array(), $langcode = NULL)

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.

For example:

<?php

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

?>

Example with additional replacements:

<?php

  $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)));

?>

Übergabeparameter

$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"). Do not use @count in the singular string.

$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".

$args An associative array of replacements to make after translation. Incidences 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:

  • !variable: inserted as is
  • @variable: escape plain text to HTML (check_plain)
  • %variable: escape text and theme as a placeholder for user-submitted content (check_plain + theme_placeholder)

Note that you do not need to include @count in this array. This replacement is done automatically for the plural case.

$langcode Optional language code to translate to a language other than what is used to display the page.

Rückgabewert

A translated string.

Verwandte Themen

▾ 37 functions call format_plural()

actions_synchronize in includes/actions.inc
Synchronize actions that are provided by modules.
aggregator_view in modules/aggregator/aggregator.admin.inc
Displays the aggregator administration page.
comment_node_search_result in modules/comment/comment.module
Implementation of hook_node_search_result().
comment_node_view in modules/comment/comment.module
An implementation of hook_node_view().
do_search in modules/search/search.module
Do a query on the full-text search index for a word or words.
FileHookTestCase::assertFileHookCalled in modules/simpletest/tests/file.test
Assert that a hook_file_* hook was called a certain number of times.
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_node_search_result in modules/node/node.api.php
The node is being displayed as a search result.
LocaleImportFunctionalTest::testStandalonePoFile in modules/locale/locale.test
Test importation of standalone .po files.
MenuTestCase::addCustomMenu in modules/menu/menu.test
Add custom menu.
menu_delete_menu_confirm in modules/menu/menu.admin.inc
Build a confirm form for deletion of a custom menu.
menu_edit_menu_validate in modules/menu/menu.admin.inc
Validates the human and machine-readable names when adding or editing a menu.
node_multiple_delete_confirm in modules/node/node.admin.inc
node_node_type in modules/node/content_types.inc
Implementation of hook_node_type().
node_title_list in modules/node/node.module
Gather a listing of links to nodes.
node_type_delete_confirm in modules/node/content_types.inc
Menu callback; delete a single content type.
poll_page in modules/poll/poll.pages.inc
Menu callback to provide a simple list of all polls available.
search_admin_settings in modules/search/search.admin.inc
Menu callback; displays the search module settings page.
statistics_node_view in modules/statistics/statistics.module
Implementation of hook_node_view().
system_modules_confirm_form in modules/system/system.admin.inc
Display confirmation form for required modules.
tearDown in modules/simpletest/drupal_web_test_case.php
Delete created files and temporary files directory, delete the tables created by setUp(), and reset the database prefix.
template_preprocess_forum_list in modules/forum/forum.module
Process variables to format a forum listing.
template_preprocess_forum_topic_list in modules/forum/forum.module
Preprocess variables to format the topic listing.
theme_field_formatter_number in modules/field/modules/number/number.module
Proxy theme function for number field formatters.
tracker_page in modules/tracker/tracker.pages.inc
Menu callback. Prints a listing of active nodes on the site.
UploadTestCase::testNodeUpload in modules/upload/upload.test
Create node; upload files to node; and edit, and delete uploads.
upload_node_links in modules/upload/upload.module
Inject links into $node for attachments.
upload_node_search_result in modules/upload/upload.module
Implementation of hook_node_search_result().
user_block_view in modules/user/user.module
Implementation of hook_block_view().
_aggregator_items in modules/aggregator/aggregator.module
Helper function for drupal_map_assoc.
_locale_batch_language_finished in includes/locale.inc
Finished callback of language addition locale import batch. Inform the user of translation files imported.
_locale_batch_system_finished in includes/locale.inc
Finished callback of system page locale import batch. Inform the user of translation files imported.
_locale_import_po in includes/locale.inc
Parses Gettext Portable Object file information and inserts into database
_node_characters in modules/node/node.admin.inc
Helper function for teaser length choices.
_node_mass_update_batch_finished in modules/node/node.admin.inc
Node Mass Update Batch 'finished' callback.
_simpletest_format_summary_line in modules/simpletest/simpletest.module

Code

includes/common.inc, line 1689

<?php
function format_plural($count, $singular, $plural, $args = array(), $langcode = NULL) {
  $args['@count'] = $count;
  if ($count == 1) {
    return t($singular, $args, $langcode);
  }

  // Get the plural index through the gettext formula.
  $index = (function_exists('locale_get_plural')) ? locale_get_plural($count, $langcode) : -1;
  // Backwards compatibility.
  if ($index < 0) {
    return t($plural, $args, $langcode);
  }
  else {
    switch ($index) {
      case "0":
        return t($singular, $args, $langcode);
      case "1":
        return t($plural, $args, $langcode);
      default:
        unset($args['@count']);
        $args['@count[' . $index . ']'] = $count;
        return t(strtr($plural, array('@count' => '@count[' . $index . ']')), $args, $langcode);
    }
  }
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen