protected TranslationModule FromWorksheet(string project, WorksheetEntry worksheet) { // Define the URL to request the list feed of the worksheet. AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null); // Fetch the list feed of the worksheet. ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString()); ListFeed listFeed = this.Service.Query(listQuery); Console.WriteLine("Results {0}", listFeed.Entries.Count); List <string> languages = new List <string>(); var dicts = new Dictionary <string, Dictionary <string, string> >(); var comments = new Dictionary <string, string>(); //language information is in first row var firstRow = (ListEntry)listFeed.Entries[0]; for (int column = 1; column < firstRow.Elements.Count; column++) { var lang = firstRow.Elements[column].LocalName; languages.Add(lang); dicts.Add(lang, new Dictionary <string, string>()); } var tp = new TranslationModule(project, "en", languages.ToArray()); for (int rowIdx = 1; rowIdx < listFeed.Entries.Count; rowIdx++) { ListEntry row = (ListEntry)listFeed.Entries[rowIdx]; string comment = null; string key = row.Elements[0].Value; // first column is the key // Update the row's data. for (int column = 1; column < row.Elements.Count; column++) { var element = row.Elements[column]; //in list based feeds localname always correponds to first row if (element.LocalName.ToLower() == "comment") { comment = element.Value; } else { tp.Add(new Segment(element.LocalName, key, element.Value)); } } foreach (var seg in tp.ByKey[key]) { seg.Comment = comment; } } return(tp); }
/// <summary> /// Based on the matched keys, exchanges keys in the given translaton project /// </summary> /// <param name="tp"></param> public void ChangeKeys(TranslationModule tp, bool concatMultipleMatches = false) { int counter = 0; var byLanguage = tp.ByLanguage; foreach (var kvp in Matches.Where(kvp => kvp.Key != kvp.Value)) { foreach (var lang in byLanguage) { var newKey = lang.Where(s => s.Key == kvp.Key); var existingKey = lang.Where(s => s.Key == kvp.Value); if (newKey.Any() && !existingKey.Any()) { //rename keys foreach (var s in newKey) { s.Key = kvp.Key; } } else if (!existingKey.Any()) { //add empty value tp.Add(new Segment(lang.Key, kvp.Value, "")); } else if (concatMultipleMatches) { //multiple matches found //in this case we apply the special rule for lucca which consist in concetenating the strings throw new NotImplementedException(); } /* * //var lang = tp.Dicts[langKey]; * if(lang) * if (lang.ContainsKey(kvp.Key) && !lang.ContainsKey(kvp.Value)) * { * lang.Add(kvp.Value, lang[kvp.Key]); * lang.Remove(kvp.Key); * counter++; * } * else if (!lang.ContainsKey(kvp.Key)) * { * lang.Add(kvp.Value, ""); * } * else if(concatMultipleMatches) * { * //multiple matches found * //in this case we apply the special rule for lucca which consist in concetenating the strings * lang[kvp.Value] += " <br /><br />" + lang[kvp.Key]; * lang.Remove(kvp.Key); * }*/ } } }
public static TranslationModule FromResX(string dir, string project, string masterLanguage) { var tp = new TranslationModule(project, masterLanguage); List <DateTime> fileDates = new List <DateTime>(); var masterFile = dir + project + ".resx"; tp.Add(Segment.FromDict(masterLanguage, GetDictFromResX(masterFile))); fileDates.Add(System.IO.File.GetLastWriteTimeUtc(masterFile)); foreach (var l in tp.Languages) { if (l == masterLanguage) { continue; //we skip master language since we treated it already as a special case } var file = dir + project + "." + l + ".resx"; tp.Add(Segment.FromDict(l, GetDictFromResX(file))); fileDates.Add(System.IO.File.GetLastWriteTimeUtc(file)); } tp.LastModified = fileDates.Max(); return(tp); }
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); }