encode static method

JsonEncodeResult encode(
  1. ArbFile arb, {
  2. required bool stripPlurals,
})

Encode a (namespace-filtered) arb to backend JSON.

When stripPlurals is true the output is flat-json — ICU expressions collapse to their other branch. When false the output is icu-json — values pass through verbatim.

Returns the encoded bytes plus the keys whose ICU was collapsed, so dialect sync can surface the lossy-event hint required by the flat-json spec. For icu-json the collapsed list is always empty.

Throws FormatException if a flat-json value has a plural / select expression with no other branch.

Implementation

static JsonEncodeResult encode(ArbFile arb, {required bool stripPlurals}) {
  final map = <String, String>{};
  final collapsedKeys = <String>[];

  for (final entry in arb.entries) {
    var value = entry.value;
    if (stripPlurals) {
      final hadExpression = IcuMessage.hasPluralOrSelect(value);
      value = IcuMessage.flattenToOther(value, keyHint: entry.key);
      if (hadExpression) collapsedKeys.add(entry.key);
    }
    map[entry.key] = value;
  }

  return JsonEncodeResult(
    content: _encodeFlat(map),
    collapsedKeys: collapsedKeys,
  );
}