flatten static method
Flattens a - possibly nested - map of translations into a flat map whose keys are dot-separated paths.
flatten({'pages': {'task_list': {'title': 'Tasks'}}});
// {'pages.task_list.title': 'Tasks'}
A map which is already flat is returned unchanged, which is what makes
nested and dot-separated translation files interchangeable. Values which
are neither a map nor a string are converted with toString().
Implementation
static Map<String, String> flatten(Map<String, dynamic> translations) {
Map<String, String> flat = {};
void visit(String prefix, Map<dynamic, dynamic> map) {
map.forEach((key, value) {
String path = prefix.isEmpty ? '$key' : '$prefix.$key';
if (value is Map) {
visit(path, value);
} else {
flat[path] = '$value';
}
});
}
visit('', translations);
return flat;
}