generateOrMerge static method
void
generateOrMerge({})
Generate or merge ARB file, avoid key duplication, suggest meaningful keys, support custom key format
Implementation
static void generateOrMerge({
required Map<String, String> newStrings,
required String filePath,
String locale = 'en',
bool suggestMeaningfulKeys = true,
String keyFormat = 'camelCase', // Options: snake_case, camelCase, dot.case
}) {
final file = File(filePath);
// Create directory if it doesn't exist
final directory = file.parent;
if (!directory.existsSync()) {
directory.createSync(recursive: true);
}
Map<String, dynamic> arbData = {};
bool fileExists = file.existsSync();
if (fileExists) {
try {
arbData = jsonDecode(file.readAsStringSync());
print('đ Merging with existing ARB file: ${file.path}');
} catch (e) {
print('â ī¸ Error reading existing ARB file: $e');
print('Creating a new ARB file instead.');
fileExists = false;
}
}
int newAdded = 0;
for (final entry in newStrings.entries) {
final value = entry.value;
// Always respect the original key if provided
String key = entry.key;
print('đ Processing value: $value for key: $key');
// Convert Dart interpolation to ICU format first
String icuValue = _convertToIcuInterpolation(value);
bool needsPluralization =
icuValue.contains('(s)') || icuValue.contains('{count}');
dynamic processedValue = needsPluralization
? _generatePluralOrGenderValue(icuValue)
: (_needsPluralOrGenderSupport(icuValue)
? _generatePluralOrGenderValue(icuValue)
: icuValue);
print('âī¸ Generated value: $processedValue');
// Always write the value since we know it's new or an update
arbData[key] = processedValue;
newAdded++;
}
print('đĻ Final ARB data: $arbData');
// Add context notes and save
arbData = addContextNotes(arbData);
var arbContent = JsonEncoder.withIndent(' ').convert(arbData);
file.writeAsStringSync(arbContent);
if (newAdded > 0) {
print('â
Added $newAdded new strings to ${file.path}');
} else {
print('âšī¸ No new strings added to ${file.path}');
}
}