function TranslationInterface::formatPlural
Same name in other branches
- 8.9.x core/lib/Drupal/Core/StringTranslation/TranslationInterface.php \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
- 10 core/lib/Drupal/Core/StringTranslation/TranslationInterface.php \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
- 11.x core/lib/Drupal/Core/StringTranslation/TranslationInterface.php \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
Formats a string containing a count of items.
This function ensures that the string is pluralized correctly. Since TranslationInterface::translate() is called by this function, make sure not to pass already-localized strings to it. See PluralTranslatableMarkup::createFromTranslatedString() for that.
For example:
$output = $string_translation->formatPlural($node->comment_count, '1 comment', '@count comments');
Example with additional replacements:
$output = $string_translation->formatPlural($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
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 \Drupal\Component\Render\FormattableMarkup. Note that you do not need to include @count in this array; this replacement is done automatically for the plural cases.
array $options: An associative array of additional options. See t() for allowed keys.
Return value
\Drupal\Core\StringTranslation\PluralTranslatableMarkup A translated string.
See also
\Drupal\Core\StringTranslation\TranslationInterface::translate()
t()
\Drupal\Component\Render\FormattableMarkup
\Drupal\Core\StringTranslation\PluralTranslatableMarkup::createFromTranslatedString()
2 methods override TranslationInterface::formatPlural()
- TestTranslationManager::formatPlural in core/
modules/ user/ tests/ src/ Unit/ PermissionHandlerTest.php - Formats a string containing a count of items.
- TranslationManager::formatPlural in core/
lib/ Drupal/ Core/ StringTranslation/ TranslationManager.php - Formats a string containing a count of items.
File
-
core/
lib/ Drupal/ Core/ StringTranslation/ TranslationInterface.php, line 110
Class
- TranslationInterface
- Interface for the translation.manager translation service.
Namespace
Drupal\Core\StringTranslationCode
public function formatPlural($count, $singular, $plural, array $args = [], array $options = []);
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.