encode static method
Implementation
static String encode(ArbFile arb) {
final buf = StringBuffer();
buf.writeln('{');
// @@locale always first.
buf.write(' "@@locale": ');
buf.write(jsonEncode(arb.locale));
// Other file-level @@-metadata in sorted order — preserve, do not
// silently strip user data (e.g. Flutter gen_l10n's @@last_modified).
final extraFileKeys = arb.fileMetadata.keys.toList()..sort();
for (final k in extraFileKeys) {
buf.writeln(',');
buf.write(' ');
buf.write(jsonEncode(k));
buf.write(': ');
buf.write(jsonEncode(arb.fileMetadata[k]));
}
final sorted = [...arb.entries]..sort((a, b) => a.key.compareTo(b.key));
if (sorted.isEmpty) {
buf.writeln();
buf.writeln('}');
return buf.toString();
}
buf.writeln(',');
for (var i = 0; i < sorted.length; i++) {
final entry = sorted[i];
final isLast = i == sorted.length - 1;
// Blank line before each group.
buf.writeln();
// Key/value line.
buf.write(' ');
buf.write(jsonEncode(entry.key));
buf.write(': ');
buf.write(jsonEncode(entry.value));
if (entry.metadata != null) {
buf.writeln(',');
// @key metadata block.
buf.write(' ');
buf.write(jsonEncode('@${entry.key}'));
buf.write(': ');
_writeMetadata(buf, entry.metadata!);
}
if (isLast) {
buf.writeln();
} else {
buf.writeln(',');
}
}
buf.writeln('}');
return buf.toString();
}