translateWithMap method

String translateWithMap(
  1. Symbol key, {
  2. Map<String, dynamic>? map,
  3. Map<String, MustacheFilter>? filters,
  4. num? count,
  5. JsonIntlGender? gender,
  6. int precision = 0,
  7. String? locale,
  8. bool? strict,
})

Implementation

String translateWithMap(
  Symbol key, {
  Map<String, dynamic>? map,
  Map<String, MustacheFilter>? filters,
  num? count,
  JsonIntlGender? gender,
  int precision = 0,
  String? locale,
  bool? strict,
}) {
  map ??= <String, dynamic>{'count': count};
  final strictValue = strict ?? true;

  final mustache = Mustache(
    map: map,
    filters: filters ?? const <String, MustacheFilter>{},
    debug: _debug,
  );
  final message = _localizedValues[key];
  if (message == null) {
    if (_debug) {
      var debug = false;
      assert(() {
        debug = true;
        return true;
      }());
      if (debug) {
        return '[$key]!!';
      }
    }

    throw JsonIntlException('The translation key [$key] was not found');
  }

  var plural = JsonIntlPlural.other;
  JsonIntlPlural? direct;

  if (count != null) {
    if (count == 0) {
      direct = JsonIntlPlural.zero;
    } else if (count == 1) {
      direct = JsonIntlPlural.one;
    } else if (count == 2) {
      direct = JsonIntlPlural.two;
    }

    final pluralRule = _pluralRule(locale, count, precision);
    if (pluralRule != null) {
      switch (pluralRule.call()) {
        case plural_rules.PluralCase.ZERO:
          plural = JsonIntlPlural.zero;
          break;
        case plural_rules.PluralCase.ONE:
          plural = JsonIntlPlural.one;
          break;
        case plural_rules.PluralCase.TWO:
          plural = JsonIntlPlural.two;
          break;
        case plural_rules.PluralCase.FEW:
          plural = JsonIntlPlural.few;
          break;
        case plural_rules.PluralCase.MANY:
          plural = JsonIntlPlural.many;
          break;
        case plural_rules.PluralCase.OTHER:
          plural = JsonIntlPlural.other;
          break;
      }
    }
  }

  final value = message.get(
    gender,
    plural,
    strictValue ? null : direct,
  );
  if (value == null) {
    if (_debug) {
      var debug = false;
      assert(() {
        debug = true;
        return true;
      }());
      if (debug) {
        return '[$key]!!';
      }
    }
    throw JsonIntlException(
        'Unable to build a translation for [$key]\n  Gender: $gender\n  Plural: $plural\n  Direct: $direct\n  Count: $count\n  Map: $map');
  }

  var result = mustache.convert(value);

  assert(() {
    if (_debug) {
      result = '[$key]($result)';
    }
    return true;
  }());

  return result;
}