load method

Future<CatalogState> load({
  1. required CatalogConfig config,
  2. required String projectRootPath,
})

Implementation

Future<CatalogState> load({
  required CatalogConfig config,
  required String projectRootPath,
}) async {
  final statePath = config.resolveStateFilePath(projectRootPath);
  final file = File(statePath);
  if (!file.existsSync()) {
    return CatalogState.empty(
      sourceLocale: config.effectiveSourceLocale,
      format: catalogFileFormatToString(config.format),
    );
  }

  try {
    final decoded = jsonDecode(await file.readAsString());
    if (decoded is! Map) {
      return CatalogState.empty(
        sourceLocale: config.effectiveSourceLocale,
        format: catalogFileFormatToString(config.format),
      );
    }

    final parsed = CatalogState.fromJson(Map<String, dynamic>.from(decoded));
    parsed.sourceLocale = config.effectiveSourceLocale;
    parsed.format = catalogFileFormatToString(config.format);
    return parsed;
  } catch (_) {
    return CatalogState.empty(
      sourceLocale: config.effectiveSourceLocale,
      format: catalogFileFormatToString(config.format),
    );
  }
}