updateFallbackLocale method

Future<CatalogConfig> updateFallbackLocale(
  1. String locale
)

Updates the fallback locale in config.

Implementation

Future<CatalogConfig> updateFallbackLocale(String locale) async {
  final normalizedLocale = locale.trim().replaceAll('-', '_');

  final dataset = await _repository.load();
  if (!dataset.locales.contains(normalizedLocale)) {
    throw CatalogOperationException('Locale "$normalizedLocale" does not exist.');
  }

  final updatedConfig = config.copyWith(
    fallbackLocale: normalizedLocale,
    clearSourceLocale: true, // Also clear source_locale so it falls back to fallback
  );

  final configPath = PathUtils.join(projectRootPath, CatalogConfig.defaultConfigPath);
  await CatalogConfig.writeConfig(
    path: configPath,
    config: updatedConfig,
  );

  config = updatedConfig;
  return updatedConfig;
}