prepare static method

PreparedArb prepare(
  1. ArbFile arb, {
  2. required PlatformConfig platform,
  3. required bool isSource,
  4. ArbFile? source,
})

Apply the namespace filter and strip translation metadata, returning a PreparedArb ready to hand to ArbWriter (plus diagnostics).

isSource preserves @key metadata when true; false strips it. When isSource is false, source must be provided so the filter can look up each key's namespace from the source ARB.

Implementation

static PreparedArb prepare(
  ArbFile arb, {
  required PlatformConfig platform,
  required bool isSource,
  ArbFile? source,
}) {
  assert(
    isSource || source != null,
    'translation ARBs need the source ARB to resolve namespaces',
  );
  final namespaces = platform.namespaces;
  final keepAll = namespaces.isEmpty;
  final ns = namespaces.toSet();

  final namespaceOf = isSource
      ? null
      : {for (final e in source!.entries) e.key: e.namespace};

  final entries = <ArbEntry>[];
  final missingNamespace = <String>[];
  final excludedByNamespace = <String, List<String>>{};
  for (final entry in arb.entries) {
    if (!keepAll) {
      final entryNs = isSource ? entry.namespace : namespaceOf![entry.key];
      if (entryNs == null) {
        missingNamespace.add(entry.key);
        continue;
      }
      if (!ns.contains(entryNs)) {
        excludedByNamespace.putIfAbsent(entryNs, () => []).add(entry.key);
        continue;
      }
    }
    entries.add(
      isSource ? entry : ArbEntry(key: entry.key, value: entry.value),
    );
  }

  return PreparedArb(
    arb: ArbFile(
      locale: arb.locale,
      entries: entries,
      fileMetadata: arb.fileMetadata,
      // Orphans are never emitted — same contract as the writer.
      orphanMetadata: const {},
      // Line numbers are tied to the source file; meaningless after
      // namespace filtering. Drop them.
      entryLines: const {},
      sourcePath: arb.sourcePath,
    ),
    keysMissingNamespace: missingNamespace,
    keysExcludedByNamespace: excludedByNamespace,
  );
}