migrateArb function
Implementation
Map<String, dynamic> migrateArb(String raw, [bool verbose = true]) {
final sourceMap = json.decode(raw);
final resultMap = <String, dynamic>{};
if (sourceMap is! Map<String, dynamic>) {
throw 'ARB files must be a JSON object';
}
List<Set<String>> detectedContexts = [];
List<String> detectedContextNames = []; // index matches with detectedContexts
sourceMap.forEach((key, value) {
if (key.startsWith('@@')) {
// add without modifications
MapUtils.addStringToMap(
map: resultMap,
destinationPath: key,
leafContent: value.toString(),
);
return;
}
final bool isMeta = key.startsWith('@');
if (isMeta) {
key = key.substring(1);
}
final keyParts = <String>[];
key.getWords().forEach((part) {
final subPathInt = int.tryParse(part);
if (subPathInt == null) {
// add normal key part
keyParts.add(part.toLowerCase());
} else {
// this key part is a number
if (keyParts.isEmpty) {
throw 'Keys cannot start with a number: $key';
}
// add number to last part as suffix
keyParts[keyParts.length - 1] = '${keyParts.last}$subPathInt';
}
});
if (isMeta) {
_digestMeta(
keyParts,
value is Map<String, dynamic> ? value : {},
resultMap,
);
} else {
final contextResult = _digestEntry(keyParts, value, resultMap);
contextResult.forEach((c) {
if (detectedContexts
.every((c2) => !_setEquality.equals(c2, c.contextEnum))) {
// detected new context
detectedContexts.add(c.contextEnum);
detectedContextNames.add(c.contextName);
}
});
}
});
if (verbose && detectedContexts.isNotEmpty) {
print('');
print('Detected contexts (please define them in build.yaml):');
const suffixes = ['Context', 'Type'];
for (int i = 0; i < detectedContexts.length; i++) {
final contextName = detectedContextNames[i].toCase(CaseStyle.pascal);
final contextNameLower = contextName.toLowerCase();
final hasSuffix = suffixes
.any((suffix) => contextNameLower.contains(suffix.toLowerCase()));
final additionalNames = hasSuffix
? []
: suffixes
.where((suffix) => !contextNameLower.contains(suffix))
.map((suffix) => _contextNameWithSuffix(contextName, suffix))
.toList();
print('');
if (additionalNames.isEmpty) {
print('[$contextName]');
} else {
print('[$contextName] ... or ${additionalNames.join(', ')}');
}
detectedContexts[i].forEach((enumValue) {
print(' - $enumValue');
});
}
}
return resultMap;
}