\Drupal\Core\StringTranslation\StringTranslationTrait::getNumberOfPlurals() is deprecated and will be removed in Drupal 13.0.0.
The method was a hidden dependency on the Locale module, which provides the plural formulas. Code that needs the number of plural forms for a language should call the Locale module’s locale.plural.formula service (\Drupal\locale\PluralFormulaInterface) directly.
If your module depends on Locale, inject the service, or call it directly where injection isn’t possible.
Before:
$plurals = $this->getNumberOfPlurals($langcode);
After:
use Drupal\locale\PluralFormulaInterface;
public function __construct(
protected PluralFormulaInterface $pluralFormula,
) {}
$plurals = $this->pluralFormula->getNumberOfPlurals($langcode);
If your module does not depend on Locale, add code to check that the service exists. Without Locale, two plural forms (singular and plural) should be assumed, which is what the deprecated method returned.
$plurals = 2;
if (\Drupal::hasService('locale.plural.formula')) {
$plurals = \Drupal::service('locale.plural.formula')->getNumberOfPlurals($langcode);
}