static void WriteBlock(POBlock block, StreamWriter writer, POFile po, bool isMessages) { foreach (var item in block.Metadata) { writer.WriteLine(item); } foreach (var line in LineWrap(block.Id.Replace("\r\n", "\n"), "msgid")) { writer.WriteLine(line); } if (block.IdPlural != null) { foreach (var line in LineWrap(block.IdPlural.Replace("\r\n", "\n"), "msgid_plural")) { writer.WriteLine(line); } for (int i = 0; i < po.NPlurals; ++i) { var translatedPlural = block.GetTranslatedPlural(i); if (translatedPlural == null) { continue; } string value = isMessages ? "" : translatedPlural.Replace("\r\n", "\n"); foreach (var line in LineWrap(value, $"msgstr[{i}]")) { writer.WriteLine(line); } } } else { string value = isMessages ? "" : block.TranslatedString.Replace("\r\n", "\n"); foreach (var line in LineWrap(value, "msgstr")) { writer.WriteLine(line); } } }
static POBlock ReadBlock(ref string line, StreamReader reader, POFile po) { var map = new HashSet <string> { { "# " }, { "#. " }, { "#: " }, { "#, " }, { "#| msgid " }, }; var block = new POBlock(); bool init = false; do { foreach (var mapping in map) { if (line.StartsWith(mapping, StringComparison.Ordinal)) { block.Metadata.Add(line); goto read_next; } } string actual; bool readNext; if ((actual = ReadString(ref line, reader, "msgid ", out readNext, po)) != null) { block.Id = actual; init = true; if (!readNext) { continue; } } if ((actual = ReadString(ref line, reader, "msgid_plural ", out readNext, po)) != null) { block.IdPlural = actual; if (!readNext) { continue; } } if ((actual = ReadString(ref line, reader, "msgstr ", out readNext, po)) != null) { block.TranslatedString = actual; if (!readNext) { continue; } } if ((actual = ReadString(ref line, reader, "msgstr[0] ", out readNext, po)) != null) { block.SetTranslatedPlural(0, actual); if (!readNext) { continue; } } if ((actual = ReadString(ref line, reader, "msgstr[1] ", out readNext, po)) != null) { block.SetTranslatedPlural(1, actual); if (!readNext) { continue; } } if ((actual = ReadString(ref line, reader, "msgstr[2] ", out readNext, po)) != null) { block.SetTranslatedPlural(2, actual); if (!readNext) { continue; } } read_next: line = reader.ReadLine(); } while (!string.IsNullOrEmpty(line)); if (init) { return(block); } return(null); }