You are here

function _node_revision_delete_drush_plural in Node Revision Delete 8

Same name and namespace in other branches
  1. 7.3 node_revision_delete.drush.inc \_node_revision_delete_drush_plural()

Formats a plural string containing a count of items.

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

For example:

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

Parameters

int $count: The item count to display.

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

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

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

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

Return value

string A translated string.

See also

dt()

1 call to _node_revision_delete_drush_plural()
drush_node_revision_delete_validate in ./node_revision_delete.drush.inc
Implements drush_hook_COMMAND_validate().

File

./node_revision_delete.drush.inc, line 510
Drush commands related to the Node Revision Delete module.

Code

function _node_revision_delete_drush_plural($count, $singular, $plural, array $args = [], array $options = []) {
  $args['@count'] = $count;
  if ($count == 1) {
    return dt($singular, $args, $options);
  }

  // Get the plural index through the gettext formula.
  $index = function_exists('locale_get_plural') ? locale_get_plural($count, isset($options['langcode']) ? $options['langcode'] : NULL) : -1;

  // If the index cannot be computed, use the plural as a fallback (which
  // allows for most flexiblity with the replaceable @count value).
  if ($index < 0) {
    return dt($plural, $args, $options);
  }
  else {
    switch ($index) {
      case "0":
        return dt($singular, $args, $options);
      case "1":
        return dt($plural, $args, $options);
      default:
        unset($args['@count']);
        $args['@count[' . $index . ']'] = $count;
        return dt(strtr($plural, [
          '@count' => '@count[' . $index . ']',
        ]), $args, $options);
    }
  }
}