generateSmartArb method
Smart ARB generation with intelligent merging
Implementation
Future<void> generateSmartArb(
List<NonLocalizedString> nonLocalizedStrings) async {
if (verbose) {
print('🔧 Generating smart ARB files with intelligent merging...');
}
final arbPath = '$outputDirectory/app_en.arb';
final arbFile = File(arbPath);
// Load existing ARB content if it exists
Map<String, dynamic> existingArb = {};
if (await arbFile.exists()) {
try {
final content = await arbFile.readAsString();
existingArb = json.decode(content) as Map<String, dynamic>;
if (verbose) {
print('📖 Loaded existing ARB with ${existingArb.length} entries');
}
} catch (e) {
if (verbose) {
print('⚠️ Error reading existing ARB: $e. Creating new one.');
}
}
}
// Process new strings and merge intelligently
final newEntries = <String, dynamic>{};
final skippedEntries = <String>[];
for (final nonLocalizedString in nonLocalizedStrings) {
final key = _generateKey(nonLocalizedString.content);
final value = nonLocalizedString.content;
// Check if key or value already exists
if (_entryExists(existingArb, key, value)) {
skippedEntries.add(key);
if (verbose) {
print('⏭️ Skipped existing: "$key" = "$value"');
}
continue;
}
// Add new entry with metadata
newEntries[key] = value;
final metadata = <String, dynamic>{
'description': _generateDescription(nonLocalizedString),
'source_file': nonLocalizedString.filePath,
'source_line': nonLocalizedString.lineNumber,
'pattern': _detectPattern(nonLocalizedString.context),
'added_by': 'loc_checker_auto',
'added_at': DateTime.now().toIso8601String(),
};
if (nonLocalizedString.parentNode != null) {
metadata['widget_context'] = nonLocalizedString.parentNode;
}
if (nonLocalizedString.variables.isNotEmpty) {
final placeholders = <String, dynamic>{};
for (int i = 0; i < nonLocalizedString.variables.length; i++) {
placeholders['param$i'] = {
'type': 'String',
'example': nonLocalizedString.variables[i],
};
}
metadata['placeholders'] = placeholders;
}
newEntries['@$key'] = metadata;
}
if (newEntries.isEmpty) {
if (verbose) {
print('✅ No new strings to add. All strings already exist in ARB.');
}
return;
}
// Merge and sort alphabetically
final mergedArb = _mergeAndSort(existingArb, newEntries);
// Write back to file with beautiful formatting
await _writeFormattedArb(arbFile, mergedArb);
if (verbose) {
print('✅ Smart ARB generation completed:');
print(
' - New entries added: ${newEntries.length ~/ 2}'); // Divide by 2 because of metadata
print(' - Existing entries skipped: ${skippedEntries.length}');
print(' - Total entries: ${mergedArb.length ~/ 2}');
print(' - File: $arbPath');
}
}