flatten static method
Flatten nested JSON keys for O(1) lookup.
Public so a caller building its own TranslationLoader (see
MagicTest.loadTranslations) can flatten with the exact same rule the
translator's own loader uses, rather than growing a second copy that
silently drifts from this one.
Example:
JsonAssetLoader.flatten({'auth': {'failed': 'Error'}}); // {'auth.failed': 'Error'}
Implementation
static Map<String, dynamic> flatten(
Map<String, dynamic> json, [
String prefix = '',
]) {
final result = <String, dynamic>{};
for (final entry in json.entries) {
final key = prefix.isEmpty ? entry.key : '$prefix.${entry.key}';
if (entry.value is Map<String, dynamic>) {
result.addAll(flatten(entry.value as Map<String, dynamic>, key));
} else {
result[key] = entry.value;
}
}
return result;
}