parse method

  1. @override
ParsingResult<Iterable<List>, CsvLocalizationToken> parse({
  1. required Iterable<List> input,
  2. required String name,
})
override

Implementation

@override
ParsingResult<Iterable<List>, CsvLocalizationToken> parse({
  required Iterable<List> input,
  required String name,
}) {
  final fields = input.toList();

  final index = fields[0]
      .cast<String>()
      .map(_uniformizeKey)
      .takeWhile((x) => x.isNotEmpty)
      .toList();

  // Getting language codes
  final supportedLanguageCodes = index.where(_isLanguageKey).toList();

  var section = Section(
    key: '',
    children: [],
    labels: [],
  );

  final tokens = const <CsvLocalizationToken>[];

  // Parsing entries
  for (var r = 1; r < fields.length; r++) {
    final rowValues = fields[r];

    /// Creating a map
    final row = Map<String, String>.fromEntries(
      rowValues
          .asMap()
          .entries
          .where(
            (e) => e.key < index.length,
          )
          .map(
            (e) => MapEntry(index[e.key], e.value),
          ),
    );
    final key = row[keyKey];
    var condition = row[conditionKey];
    if (key != null && key.isNotEmpty) {
      var path = key.trim();
      if (path.isNotEmpty) {
        final translations = row.entries
            .where((e) => _isLanguageKey(e.key))
            .map((e) => Translation(e.key, e.value))
            .toList();

        // We can also have a condition at the end of the key in parenthesis
        final startCondition = path.indexOf('(');
        final endCondition = path.indexOf(')');
        if (startCondition >= 0 && endCondition >= 0) {
          condition = path.substring(startCondition + 1, endCondition);
          path = path.substring(0, startCondition);
        }
        section = section.withTranslations(path, condition, translations);
      }
    }
  }

  return ParsingResult(
    input: input,
    result: Localizations.fromSection(
      name: name,
      supportedLanguageCodes: supportedLanguageCodes,
      section: section,
    ),
    tokens: tokens,
  );
}