checkLocalizationKeys function
Implementation
Future<void> checkLocalizationKeys(
String folderPath, String templateArbFile) async {
var errors = <String>[];
try {
final directory = Directory(folderPath);
final arbFiles =
directory.listSync().where((file) => file.path.endsWith('.arb'));
// Read the template file and extract all keys
final templateContent =
await File(path.normalize('$folderPath/$templateArbFile'))
.readAsString();
final templateJson = jsonDecode(templateContent) as Map<String, dynamic>;
final templateKeys = templateJson.keys.toSet();
final missingKeys = <String, List<String>>{};
for (final file in arbFiles) {
final content = await File(file.path).readAsString();
final json = jsonDecode(content) as Map<String, dynamic>;
// Check if all keys from the template file exist in the current file
for (final key in templateKeys) {
if (!json.containsKey(key)) {
if (!missingKeys.containsKey(key)) {
missingKeys[key] = [];
}
missingKeys[key]!.add(file.path);
}
}
}
if (missingKeys.isNotEmpty) {
for (final entry in missingKeys.entries) {
errors.add(
'Key "${entry.key}" was not found in the following files: ${entry.value.join(', ')}');
}
throw KeyNotFoundException();
}
} on KeyNotFoundException catch (e) {
errors.add(e.toString());
} catch (e) {
errors.add(e.toString());
}
if (errors.isNotEmpty) {
throw Exception(errors.join('\n'));
}
}