generateRootTranslations function

Future<void> generateRootTranslations({
  1. required String rootPath,
  2. required String outputFolder,
  3. required String templateArbFile,
  4. required bool nullableGetter,
})

Implementation

Future<void> generateRootTranslations({
  required String rootPath,
  required String outputFolder,
  required String templateArbFile,
  required bool nullableGetter,
}) async {
  var errors = <String>[];

  final outputPath = path.normalize('$rootPath/$outputFolder');
  final rootPathDir = path.normalize('$rootPath/l10n');

  try {
    if (await Directory(rootPathDir).exists()) {
      print('πŸ”„ Generating translations for root folder');

      await checkLocalizationKeys(rootPathDir, templateArbFile);

      String flutterPath = await findFlutterExecutable();
      ProcessResult result = await Process.run(flutterPath, [
        'gen-l10n',
        '--arb-dir',
        rootPathDir,
        '--output-dir',
        outputPath,
        '--no-synthetic-package',
        '--output-class',
        'RootLocalizations',
        '--template-arb-file',
        templateArbFile,
        '--output-localization-file',
        'root_localizations.dart',
        if (!nullableGetter) '--no-nullable-getter'
      ]);

      if (result.stdout.toString().isEmpty &&
          result.stderr.toString().isEmpty) {
        print('βœ… Generated translations for root 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 root folder: ${result.stderr.toString()} ${result.stdout}');
    } else {
      print(
          'β–Άβ–Ά Skipped translations for root 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 root folder');
  }

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