public static TranslationModule FromIWorksheet(string project, string masterLanguage, IWorksheet worksheet) { // open the file "data.csv" which is a CSV file with headers TranslationModule tp = null; bool createMissingKeys = false; Dictionary <int, string> languages = new Dictionary <int, string>(); string currentNS = project; IEnumerable <string> labels = Enumerable.Empty <string>(); int commentColumn = -1; for (int c = 1; c < worksheet.Columns; c++) { string language = ((string)worksheet[0, c]).ToLower(); if (language == "Comment") { commentColumn = c; continue; } languages.Add(c, language); } tp = new TranslationModule(project, masterLanguage, languages.Values.ToArray()); for (int r = 1; r < worksheet.Rows; r++) { string key = ((string)worksheet[r, 0]); if (string.IsNullOrWhiteSpace(key)) { continue; } key = key.Trim(); bool isSpecialColumn = false; if (key.Contains("ns:")) { currentNS = key.Split(':')[1]; isSpecialColumn = true; } if (key.StartsWith("#")) { isSpecialColumn = true; labels = key.Split(',').Select(label => label.Trim()); } if (currentNS == project && !isSpecialColumn) { if (string.IsNullOrWhiteSpace(key) && createMissingKeys) { string keyInspiration = commentColumn != 1 ? (string)worksheet[r, 1] : (string)worksheet[r, 2]; key = tp.KeyProposal(keyInspiration); } if (!string.IsNullOrWhiteSpace(key)) { for (int c = 1; c < languages.Count + 1; c++) { var segment = new Segment(languages[c], key, (string)worksheet[r, c]); segment.Tags = labels; tp.Add(segment); /* * if (c != commentColumn) * { * tp.Dicts[languages[c].ToLower()].Add(key, (string) worksheet[r, c]); * } * else * { * tp.Comments.Add(key, (string) worksheet[r, c]); * }*/ } } } } return(tp); }
public static TranslationModule FromCSV(string file, string project, string masterLanguage, bool createMissingKeys = true) { // open the file "data.csv" which is a CSV file with headers TranslationModule tp = null; List <string> languages = new List <string>(); using (CsvReader csv = new CsvReader(new StreamReader(file), true)) { int fieldCount = csv.FieldCount; string currentNS = ""; int commentColumn = -1; string[] headers = csv.GetFieldHeaders(); for (int c = 1; c < headers.Length; c++) { string language = headers[c].ToLower(); if (language == "Comment") { commentColumn = c; continue; } languages.Add(language); } tp = new TranslationModule(project, masterLanguage, languages.ToArray()); while (csv.ReadNextRecord()) { string key = csv[0]; if (key.Contains("ns:")) { currentNS = key.Split(':')[1]; } if (currentNS == project && !key.Contains("ns:")) { if (string.IsNullOrWhiteSpace(key) && createMissingKeys) { string keyInspiration = commentColumn != 1 ? csv[1] : csv[2]; key = tp.KeyProposal(keyInspiration); } if (!string.IsNullOrWhiteSpace(key)) { for (int i = 1; i < fieldCount; i++) { if (i != commentColumn) { //tp.Dicts[headers[i].ToLower()].Add(key, csv[i]); tp.Add(new Segment(headers[i].ToLower(), key, csv[i])); } } if (commentColumn != -1) { foreach (var seg in tp.ByKey[key]) { seg.Comment = csv[commentColumn]; } } } } } } return(tp); }