Translations.fromJson constructor

Translations.fromJson(
  1. Map<String, dynamic> contexts
)

Factory to get translations from generic json structure

final translations = Translations.fromJson({
   "myContext": {
       "hello": {
          "msgstr": ["hola"]
       }
   }
})

Implementation

factory Translations.fromJson(Map<String, dynamic> contexts) {
  return Translations(
    contexts.map((key, value) {
      final values = <String, Translation>{};

      if (value is Map<String, dynamic>) {
        value.forEach((msgid, json) {
          if (json is Map && json['msgstr'] is List) {
            values[msgid] = Translation(
              (json['msgstr'] as List).map((row) => row.toString()).cast<String>().toList(),
              comments: json['comments'] as Map<String, dynamic>?,
            );
          }
        });
      }

      return MapEntry<String, Map<String, Translation>>(key, values);
    }),
  );
}