Translation.file constructor

Translation.file({
  1. required File file,
  2. required String key,
})

Load translation from File.

Support json, yaml and toml

Implementation

factory Translation.file({required File file, required String key}) {
  final Map<String, Map<Lang, String>> entries = {};
  final stringContent = file.readAsStringSync();

  final Map<String, dynamic> content = switch (file) {
    File(:final uri) when uri.path.endsWith('.json') =>
      jsonDecode(stringContent),
    // File(:final uri) when uri.path.endsWith('.toml') => TomlDocument.parse(stringContent),
    File(:final uri) when uri.path.endsWith('.yaml') =>
      (loadYaml(stringContent) as YamlMap).toMap(),
    File(:final uri) when uri.path.endsWith('.yml') =>
      (loadYaml(stringContent) as YamlMap).toMap(),
    _ => throw Exception('File type not supported'),
  };

  final Map<String, dynamic> translations = content['commands'][key];

  for (final translation in translations.entries) {
    final Map<Lang, String> map = {};

    for (final MapEntry element in translation.value.entries) {
      final lang = Lang.values.firstWhere((lang) => lang.uid == element.key,
          orElse: () => throw Exception(
              'Lang "${element.key}" not exists is the available languages'));

      map.putIfAbsent(lang, () => element.value);
    }

    entries.putIfAbsent(translation.key, () => map);
  }

  return Translation(entries);
}