function TranslationManager::selectPluralForm

Same name and namespace in other branches
  1. main core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager::selectPluralForm()

Selects the plural form for a given count.

Parameters

int|float $count: The item count to display.

string $string: The translated string with plural forms delimited by PoItem::DELIMITER.

string|null $langcode: The language code to translate to. If it is NULL then the current language will be used.

Return value

string The plural form to use.

Overrides TranslationInterface::selectPluralForm

File

core/lib/Drupal/Core/StringTranslation/TranslationManager.php, line 114

Class

TranslationManager
Defines a chained translation implementation combining multiple translators.

Namespace

Drupal\Core\StringTranslation

Code

public function selectPluralForm(int|float $count, string $string, ?string $langcode = NULL) : string {
  $translated_array = explode(PoItem::DELIMITER, $string);
  if ($count == 1 || count($translated_array) == 1) {
    // Singular form.
    return $translated_array[0];
  }
  // Plural formulas are defined over integers, so a fractional count is
  // truncated for the lookup only. The count itself is still displayed as
  // given via the @count placeholder.
  $index = $this->getPluralIndex((int) $count, $langcode);
  // Fallback to second plural form.
  return $translated_array[$index] ?? $translated_array[1];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.