generateOnlyModuleTranslations function
Future<void>
generateOnlyModuleTranslations(
{ - required String modulePath,
- required String outputFolder,
- required String templateArbFile,
- required bool nullableGetter,
- 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'));
}
}