pluralLogic<T> method Null safety

T pluralLogic<T>(
  1. num howMany,
  2. {T? zero,
  3. T? one,
  4. T? two,
  5. T? few,
  6. T? many,
  7. required T other,
  8. String? locale,
  9. int? precision,
  10. String? meaning}
)

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

Implementation

static T pluralLogic<T>(num howMany,
    {T? zero,
    T? one,
    T? two,
    T? few,
    T? many,
    required T other,
    String? locale,
    int? precision,
    String? meaning}) {
  ArgumentError.checkNotNull(other, 'other');
  ArgumentError.checkNotNull(howMany, 'howMany');
  // If we haven't specified precision and we have a float that is an integer
  // value, turn it into an integer. This gives us the behavior that 1.0 and 1
  // produce the same output, e.g. 1 dollar.
  var truncated = howMany.truncate();
  if (precision == null && truncated == howMany) {
    howMany = truncated;
  }

  // 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;
    default:
      throw ArgumentError.value(
          howMany, 'howMany', 'Invalid plural argument');
  }
}