Drupal 8 formatPlural(): Formatting count sentances with Singulars and Plural

In English grammar, there is importance to display words with proper nouns. The Singular and Plural will help to display sentences with numeric count components. Singular means only one. Plural means more than one. In Drupal 8 we can format both singular and plural values by formatPlural() function of Transulation Modules.

The below is the syntax and example of the usage formatPlural() function.

Syntax: 

\Drupal::translation()->formatPlural(COUNT, SINGULAR_MESSAGE, PLURAL_MESSAGE, REPLACEMENT_ARGUMENTS);

Example: 

$title = FFFFFFFFFFFFff;
$messageSingular = "Purchase @count more :title and save an additional 10.00$ per item!";
$messagePlural = "Purchase @count more :title and save an additional 10.00$ per item!";
 
// Singular Example.
$count = 1;
print \Drupal::translation()->formatPlural($count, $messageSingular, $messagePlural, array(':title' => $title));

singular.png

// Plural Example.
$count = 4;
print \Drupal::translation()->formatPlural($count, $messageSingular, $messagePlural, array(':title' => $title));

plural.png

Category: