1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| namespace ConsoleApp { class Program { static void Main(string[] args) { generateNewI18NFile(); return; } private static void generateNewI18NFile() { IDictionary<string, string> dict = new Dictionary<string, string>(); string[] allLines = File.ReadAllLines ( "./history.xlf", Encoding.UTF8 ); string lastTransUnitId = string.Empty; string lastTarget = string.Empty; foreach (string line in allLines) { string line_trimed = line.Trim(); if (line_trimed.StartsWith("<trans-unit ")) { int firstQuote = line_trimed.IndexOf("\""), secondQuote = line_trimed.IndexOf("\"", firstQuote + 1); lastTransUnitId = line_trimed.Substring(firstQuote + 1, secondQuote - firstQuote - 1); continue; } if (line_trimed.StartsWith("<target>")) { lastTarget = line; dict.Add(lastTransUnitId, lastTarget); } } List<string> outputLines = new List<string>(); allLines = File.ReadAllLines("./now.xlf", Encoding.Default); string[] prefixOfIgnoreLines = new string[] { "<?xml ", "<xliff ", "<file ", "<body>", "</body>", "</file>", "</xliff>" }; foreach (string line in allLines) { outputLines.Add(line); string line_trimed = line.Trim();
if (prefixOfIgnoreLines.Any(_ => line_trimed.StartsWith(_))) continue;
if (line_trimed.StartsWith("<trans-unit ")) { int firstQuote = line_trimed.IndexOf("\""), secondQuote = line_trimed.IndexOf("\"", firstQuote + 1); lastTransUnitId = line_trimed.Substring(firstQuote + 1, secondQuote - firstQuote - 1); continue; } if (line_trimed.EndsWith("</source>")) { lastTarget = dict.ContainsKey(lastTransUnitId) ? dict[lastTransUnitId] : string.Empty; if (!string.IsNullOrEmpty(lastTarget)) { outputLines.Add(lastTarget); } } } File.WriteAllLines("./output.xlf", outputLines); } } }
|