pluralLogic<T> method

T pluralLogic <T>(num howMany, { T zero, T one, T two, T few, T many, T other, String locale int precision, String meaning })

Internal: Implements the logic for plural selection - use plural for normal messages.


static T pluralLogic<T>(num howMany,
    {T zero,
    T one,
    T two,
    T few,
    T many,
    T other,
    String locale,
    int precision,
    String meaning}) {
  if (other == null) {
    throw new ArgumentError("The 'other' named argument must be provided");
  if (howMany == null) {
    throw new ArgumentError("The howMany argument to plural cannot be null");

  // This is for backward compatibility.
  // We interpret the presence of [precision] parameter as an "opt-in" to
  // the new behavior, since [precision] did not exist before.
  // For an English example: if the precision is 2 then the formatted string
  // would not map to 'one' (for example "1.00 miles")
  if (precision == null || precision == 0) {
    // If there's an explicit case for the exact number, we use it. This is
    // not strictly in accord with the CLDR rules, but it seems to be the
    // expectation. At least I see e.g. Russian translations that have a zero
    // case defined. The rule for that locale will never produce a zero, and
    // treats it as other. But it seems reasonable that, even if the language
    // rules treat zero as other, we might want a special message for zero.
    if (howMany == 0 && zero != null) return zero;
    if (howMany == 1 && one != null) return one;
    if (howMany == 2 && two != null) return two;

  var pluralRule = _pluralRule(locale, howMany, precision);
  var pluralCase = pluralRule();
  switch (pluralCase) {
    case plural_rules.PluralCase.ZERO:
      return zero ?? other;
    case plural_rules.PluralCase.ONE:
      return one ?? other;
    case plural_rules.PluralCase.TWO:
      return two ?? few ?? other;
    case plural_rules.PluralCase.FEW:
      return few ?? other;
    case plural_rules.PluralCase.MANY:
      return many ?? other;
    case plural_rules.PluralCase.OTHER:
      return other;
      throw new ArgumentError.value(
          howMany, "howMany", "Invalid plural argument");