class TranslationManager

Same name in this branch
  1. 11.x core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
  2. 9 core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
  3. 8.9.x core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager

Plural index implementation.

Hierarchy

Expanded class hierarchy of TranslationManager

File

core/modules/locale/src/TranslationManager.php, line 14

Namespace

Drupal\locale
View source
class TranslationManager extends CoreTranslationManager {
  use DependencySerializationTrait;
  public function __construct(LanguageDefault $default_language, protected PluralFormulaInterface $pluralFormula) {
    parent::__construct($default_language);
  }
  
  /**
   * Returns plural form index for a specific number.
   *
   * The index is computed from the formula of this language.
   *
   * @param int $count
   *   Number to return plural for.
   * @param string|null $langcode
   *   (optional) Language code to translate to a language other than what is
   *   used to display the page.
   *
   * @return 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.
   */
  protected function getPluralIndex(int $count, ?string $langcode = NULL) : int {
    $langcode = $langcode ?: $this->defaultLangcode;
    // Retrieve the plural formulas for all languages.
    $plural_formulas = $this->pluralFormula
      ->getFormula($langcode);
    // If there is a plural formula for the language, evaluate it for the
    // given $count.
    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;
      return $plural_formulas[$index] ?? $plural_formulas['default'];
    }
    elseif ($langcode == 'en') {
      return (int) ($count != 1);
    }
    else {
      return -1;
    }
  }

}

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