generateOnlyModuleTranslations function

Future<void> generateOnlyModuleTranslations({
  1. required String modulePath,
  2. required String outputFolder,
  3. required String templateArbFile,
  4. required bool nullableGetter,
  5. required String generateModule,
})

Implementation

Future<void> generateOnlyModuleTranslations({
  required String modulePath,
  required String outputFolder,
  required String templateArbFile,
  required bool nullableGetter,
  required String generateModule,
}) async {
  var errors = <String>[];
  // Path to the current module
  String featurePath = path.normalize('$modulePath/$generateModule/l10n');

  String outputPath =
      path.normalize('$modulePath/$generateModule/$outputFolder');

  try {
    // Verify if the module localization folder exists
    if (await Directory(featurePath).exists()) {
      print('πŸ”„ Generating translations for "$generateModule" folder');
      await checkLocalizationKeys(featurePath, templateArbFile);

      String flutterPath = await findFlutterExecutable();
      // Execute flutter gen-l10n for the current module
      ProcessResult result = await Process.run(flutterPath, [
        'gen-l10n',
        '--arb-dir',
        featurePath,
        '--output-dir',
        outputPath,
        '--no-synthetic-package',
        '--output-class',
        '${underscoreToCamelCase(capitalize(generateModule))}Localizations',
        '--template-arb-file',
        templateArbFile,
        '--output-localization-file',
        '${camelCaseToUnderscore(generateModule)}_localizations.dart',
        if (!nullableGetter) '--no-nullable-getter'
      ]);

      if (result.stdout.toString().isEmpty &&
          result.stderr.toString().isEmpty) {
        print('βœ… Generated translations for "$generateModule" folder');
        return;
      }

      if (result.stdout.toString().isNotEmpty) print(result.stdout);
      if (result.stderr.toString().isNotEmpty) print(result.stderr);
      throw Exception(
          '❌ Failed to generate translations for "$generateModule": ${result.stderr.toString()} ${result.stdout}');
    } else {
      print(
          'β–Άβ–Ά Skipped translations for "$generateModule" folder because no translations where found in the specified path');
    }
  } on KeyNotFoundException catch (e) {
    errors.add(e.toString());
    print(
        '❌ Failed to generate translations because some keys were missing in the files');
  } catch (e) {
    errors.add(e.toString());
    print(e);
    print('❌ Failed to generate translations for "$generateModule" folder');
  }

  if (errors.isNotEmpty) {
    throw Exception(errors.join('\n'));
  }
}