Same name and namespace in other branches
  1. 4.6.x modules/locale.module \locale_get_plural()
  2. 4.7.x modules/locale.module \locale_get_plural()
  3. 5.x modules/locale/locale.module \locale_get_plural()
  4. 6.x modules/locale/locale.module \locale_get_plural()
  5. 7.x modules/locale/locale.module \locale_get_plural()
  6. 8.9.x core/modules/locale/locale.module \locale_get_plural()
  7. 9 core/modules/locale/locale.module \locale_get_plural()

Returns plural form index for a specific number.

The index is computed from the formula of this language.

Parameters

int $count: Number to return plural for.

string|null $langcode: (optional) Language code to translate to a language other than what is used to display the page, or NULL for current language. Defaults to NULL.

Return value

int The numeric index of the plural variant to use for this $langcode and $count combination or -1 if the language was not found or does not have a plural formula.

2 calls to locale_get_plural()
LocalePluralFormatTest::testGetPluralFormat in core/modules/locale/tests/src/Functional/LocalePluralFormatTest.php
Tests locale_get_plural() and \Drupal::translation()->formatPlural().
PluralTranslatableMarkup::getPluralIndex in core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php
Gets the plural index through the gettext formula.
2 string references to 'locale_get_plural'
LocalePluralFormatTest::testGetPluralFormat in core/modules/locale/tests/src/Functional/LocalePluralFormatTest.php
Tests locale_get_plural() and \Drupal::translation()->formatPlural().
PluralTranslatableMarkup::getPluralIndex in core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php
Gets the plural index through the gettext formula.

File

core/modules/locale/locale.module, line 283
Enables the translation of the user interface to languages other than English.

Code

function locale_get_plural($count, $langcode = NULL) {
  $language_interface = \Drupal::languageManager()
    ->getCurrentLanguage();

  // Used to store precomputed plural indexes corresponding to numbers
  // individually for each language.
  $plural_indexes =& drupal_static(__FUNCTION__ . ':plurals', []);
  $langcode = $langcode ? $langcode : $language_interface
    ->getId();
  if (!isset($plural_indexes[$langcode][$count])) {

    // Retrieve and statically cache the plural formulas for all languages.
    $plural_formulas = \Drupal::service('locale.plural.formula')
      ->getFormula($langcode);

    // If there is a plural formula for the language, evaluate it for the given
    // $count and statically cache the result for the combination of language
    // and count, since the result will always be identical.
    if (!empty($plural_formulas)) {

      // Plural formulas are stored as an array for 0-199. 100 is the highest
      // modulo used but storing 0-99 is not enough because below 100 we often
      // find exceptions (1, 2, etc).
      $index = $count > 199 ? 100 + $count % 100 : $count;
      $plural_indexes[$langcode][$count] = $plural_formulas[$index] ?? $plural_formulas['default'];
    }
    elseif ($langcode == 'en') {
      $plural_indexes[$langcode][$count] = (int) ($count != 1);
    }
    else {
      $plural_indexes[$langcode][$count] = -1;
    }
  }
  return $plural_indexes[$langcode][$count];
}