You are here

function _onlyone_drush_plural in Allow a content type only once (Only One) 8

Same name and namespace in other branches
  1. 7 onlyone.drush.inc \_onlyone_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 = _onlyone_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()

4 calls to _onlyone_drush_plural()
drush_onlyone_disable in ./onlyone.drush.inc
Callback for the onlyone-disable command.
drush_onlyone_enable in ./onlyone.drush.inc
Callback for the onlyone-enable command.
drush_onlyone_enable_validate in ./onlyone.drush.inc
Implements drush_hook_COMMAND_validate().
_drush_onlyone_validate_content_type in ./onlyone.drush.inc
Helper function to validate the content type names.

File

./onlyone.drush.inc, line 439
Drush commands related to the Only One module.

Code

function _onlyone_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);
    }
  }
}