build static method

Bundle build(
  1. DialectProject project,
  2. PublishEnvConfig env, {
  3. required String createdAt,
  4. required String generator,
})

Build the bundle for env. createdAt (ISO-8601 UTC) and generator (dialect <version>) are informational manifest fields and are deliberately not inputs to the version hash, so re-publishing identical content is deterministic.

Implementation

static Bundle build(
  DialectProject project,
  PublishEnvConfig env, {
  required String createdAt,
  required String generator,
}) {
  final stripPlurals = env.format == 'flat-json';
  // A throwaway platform config so the bundle reuses the exact same
  // namespace filter + metadata strip as `dialect sync`.
  final filter = PlatformConfig(
    name: env.name,
    output: '',
    format: env.format,
    namespaces: env.namespaces,
  );

  final files = <BundleLocale>[];
  void add(String locale, ArbFile arb, {required bool isSource}) {
    final prepared = ArbAdapter.prepare(
      arb,
      platform: filter,
      isSource: isSource,
      source: isSource ? null : project.source,
    );
    final content = JsonAdapter.encode(
      prepared.arb,
      stripPlurals: stripPlurals,
    ).content;
    files.add(
      BundleLocale(
        locale: locale,
        file: '$locale.json',
        content: content,
        sha256: sha256.convert(utf8.encode(content)).toString(),
        keys: prepared.arb.entries.length,
      ),
    );
  }

  add(project.config.sourceLocale, project.source, isSource: true);
  for (final locale in project.config.targetLocales) {
    final arb =
        project.translations[locale] ??
        ArbFile(locale: locale, entries: const []);
    add(locale, arb, isSource: false);
  }
  files.sort((a, b) => a.locale.compareTo(b.locale));

  final version = _deriveVersion(
    env.format,
    project.config.sourceLocale,
    files,
  );

  return Bundle(
    version: version,
    format: env.format,
    sourceLocale: project.config.sourceLocale,
    locales: files,
    createdAt: createdAt,
    generator: generator,
  );
}