generateModulesTranslations function
Future<void>
generateModulesTranslations(
{ - required String modulePath,
- required String outputFolder,
- required String templateArbFile,
- required bool nullableGetter,
})
Implementation
Future<void> generateModulesTranslations({
required String modulePath,
required String outputFolder,
required String templateArbFile,
required bool nullableGetter,
}) async {
var errors = <String>[];
// List of all modules inside the folder
final modulePathNormalized = path.normalize(modulePath);
var dir = Directory(modulePathNormalized);
List<FileSystemEntity> features = dir.listSync();
// Loop through each module
for (var feature in features) {
// Path to the current module
final slash = Platform.isWindows ? '\\' : '/';
String featureName = feature.path.split(slash).last;
String featurePath = path.normalize('$modulePath/$featureName/l10n');
String outputPath =
path.normalize('$modulePath/$featureName/$outputFolder');
try {
// Verify if the module localization folder exists
if (await Directory(featurePath).exists()) {
print('π Generating translations for "$featureName" folder');
await checkLocalizationKeys(featurePath, templateArbFile);
final 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(featureName))}Localizations',
'--template-arb-file',
templateArbFile,
'--output-localization-file',
'${camelCaseToUnderscore(featureName)}_localizations.dart',
if (!nullableGetter) '--no-nullable-getter'
]);
if (result.stdout.toString().isEmpty &&
result.stderr.toString().isEmpty) {
print('β
Generated translations for "$featureName" 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 "$featureName" folder: ${result.stderr.toString()} ${result.stdout}');
} else {
print(
'βΆβΆ Skipped translations for "$featureName" 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 "$featureName" folder');
}
}
if (errors.isNotEmpty) {
throw Exception(errors.join('\n'));
}
}