Entry.fromString constructor

Entry.fromString(
  1. ExtractSettings settings,
  2. String value
)

Parses the string representation of Entry

This method never fails. It tries to create an instance of Entry from any string. Therefore, it is not good to use it for validation.

Implementation

factory Entry.fromString(ExtractSettings settings, String value) {
  final entry = Entry(settings: settings, msgstrList: []);
  final lines = value.trim().split("\n");

  for (int i = 0; i < lines.length; i++) {
    final isLast = (i == (lines.length - 1));
    final line = lines[i].trim();

    // For all the comments.
    if (line.startsWith("#")) {
      // Parse reference.
      if (line.startsWith("#:")) {
        final references = line
            .replaceFirst("#:", "")
            .trim()
            .split(" ")
            .map((part) => Reference.fromString(part))
            .where((reference) => reference != null)
            .map((reference) => reference as Reference);

        entry.references.addAll(references);
        continue;
      }

      // Parse flags.
      if (line.startsWith("#,")) {
        final flagList = line.replaceFirst("#,", "").trim().split(",");
        entry.flags.addAll(flagList.map((flag) => flag.trim()));
        continue;
      }

      // Parse extracted comments.
      if (line.startsWith("#.")) {
        entry.extractedComment.add(line.replaceFirst("#.", "").trim());
        continue;
      }

      // Parse translator's comments.
      entry.translatorComment.add(line.replaceFirst("#", "").trim());
      continue;
    }

    // For all the prefixed values.

    // Parse plural msgid.
    if (line.startsWith("msgid_plural")) {
      // Get the first line.
      entry.msgidPlural =
          removeQuotes(line.replaceFirst("msgid_plural", "").trim());

      // If the next line starts with quotes.
      if (!isLast && lines[i + 1].trim().startsWith('"')) {
        // Iterate through the next lines starting with quotes.
        for (int j = i + 1;
            j < lines.length && lines[j].trim().startsWith('"');
            j++) {
          // Add the content to the original string;
          entry.msgidPlural = entry.msgidPlural! + removeQuotes(lines[j]);
        }
      }

      continue;
    }

    // Parse msgid.
    if (line.startsWith("msgid")) {
      // Get the first line.
      entry.msgid = removeQuotes(line.replaceFirst("msgid", "").trim());

      // If the next line starts with quotes.
      if (!isLast && lines[i + 1].trim().startsWith('"')) {
        // Iterate through the next lines starting with quotes.
        for (int j = i + 1;
            j < lines.length && lines[j].trim().startsWith('"');
            j++) {
          entry.msgid = entry.msgid + removeQuotes(lines[j]);
        }
      }

      continue;
    }

    // Parse indexed msgstr.
    if (line.startsWith("msgstr[")) {
      // Get the first line. We don't care about the index, just the position.
      var content = removeQuotes(
        line.replaceFirst("${line.split("]")[0]}]", "").trim(),
      );

      // If the next line starts with quotes.
      if (!isLast && lines[i + 1].trim().startsWith('"')) {
        // Iterate through the next lines starting with quotes.
        for (int j = i + 1;
            j < lines.length && lines[j].trim().startsWith('"');
            j++) {
          // Append the next line to the string.
          content += removeQuotes(lines[j]);
        }
      }

      // Add the string to the list.
      entry.msgstrList.add(content);

      continue;
    }

    // Parse msgstr.
    if (line.startsWith("msgstr")) {
      // Get the first line.
      entry.msgstr = removeQuotes(line.replaceFirst("msgstr", "").trim());

      // If the next line starts with quotes.
      if (!isLast && lines[i + 1].trim().startsWith('"')) {
        // Iterate through the next lines starting with quotes.
        for (int j = i + 1;
            j < lines.length && lines[j].trim().startsWith('"');
            j++) {
          entry.msgstr = entry.msgstr + removeQuotes(lines[j]);
        }
      }

      continue;
    }

    // Parse msgctxt.
    if (line.startsWith("msgctxt")) {
      // Get the first line.
      entry.msgctxt = removeQuotes(line.replaceFirst("msgctxt", "").trim());

      // If the next line starts with quotes.
      if (!isLast && lines[i + 1].trim().startsWith('"')) {
        // Iterate through the next lines starting with quotes.
        for (int j = i + 1;
            j < lines.length && lines[j].trim().startsWith('"');
            j++) {
          entry.msgctxt = entry.msgctxt! + removeQuotes(lines[j]);
        }
      }

      continue;
    }
  }

  // Clear msgstr list if it's empty.
  bool empty = true;
  for (var element in entry.msgstrList) {
    if (element.trim().isNotEmpty) {
      empty = false;
      break;
    }
  }
  if (empty) entry.msgstrList.clear();

  return entry;
}