private static bool AddOrUpdateTerm(Guid currentUserId, Localization model, LocalizationTerm loadedTerm) { bool termsUpdated; if (loadedTerm.UserId == Guid.Empty) { loadedTerm.UserId = currentUserId; } LocalizationTerm modelTerm = model.Terms.FirstOrDefault(x => x.Key.Equals(loadedTerm.Key.Replace("\n", string.Empty))); if (modelTerm == null) { model.Terms.Add(loadedTerm); termsUpdated = true; } else { loadedTerm.Id = modelTerm.Id; loadedTerm.CreateDate = modelTerm.CreateDate; loadedTerm.UserId = modelTerm.UserId; modelTerm.Value = loadedTerm.Value; termsUpdated = true; } return(termsUpdated); }
private static void FillTerms(DataTable dataTable, List <LocalizationTerm> terms) { foreach (DataRow row in dataTable.Rows) { object term = row.ItemArray[0]; object termValue = row.ItemArray[1]; if (term == null || string.IsNullOrWhiteSpace(term.ToString()) || termValue == null || string.IsNullOrWhiteSpace(termValue.ToString())) { continue; } bool termExists = terms.Any(x => x.Key.Equals(term)); if (!termExists) { LocalizationTerm newTerm = new LocalizationTerm { Key = term.ToString().Trim().Replace("\n", "_"), Value = termValue.ToString().Trim() }; newTerm.Key = SanitizeKey(newTerm.Key); terms.Add(newTerm); } } }
public async Task <bool> AddTerm(Guid translationProjectId, LocalizationTerm term) { term.Id = Guid.NewGuid(); FilterDefinition <Localization> filter = Builders <Localization> .Filter.Where(x => x.Id == translationProjectId); UpdateDefinition <Localization> add = Builders <Localization> .Update.AddToSet(c => c.Terms, term); UpdateResult result = await DbSet.UpdateOneAsync(filter, add); return(result.IsAcknowledged && result.MatchedCount > 0); }
public async Task <bool> UpdateTerm(Guid translationProjectId, LocalizationTerm term) { FilterDefinition <Localization> filter = Builders <Localization> .Filter.And( Builders <Localization> .Filter.Eq(x => x.Id, translationProjectId), Builders <Localization> .Filter.ElemMatch(x => x.Terms, x => x.Id == term.Id)); UpdateDefinition <Localization> update = Builders <Localization> .Update .Set(c => c.Terms[-1].Key, term.Key) .Set(c => c.Terms[-1].Value, term.Value) .Set(c => c.Terms[-1].Obs, term.Obs); UpdateResult result = await DbSet.UpdateOneAsync(filter, update); return(result.IsAcknowledged && result.ModifiedCount > 0); }
private static string GenerateLanguageXml(Localization project, SupportedLanguage language, bool fillGaps) { StringBuilder sb = new StringBuilder(); sb.AppendLine("<resources>"); sb.AppendLine(); sb.AppendLine(String.Format("<string id=\"lang_name\">{0}</string>", language.ToDisplayName())); sb.AppendLine(String.Format("<string id=\"lang_index\">{0}</string>", (int)language)); sb.AppendLine(); for (int i = 0; i < project.Terms.Count; i++) { string langValue = null; LocalizationTerm term = project.Terms.ElementAt(i); IOrderedEnumerable <LocalizationEntry> entries = project.Entries.Where(x => x.TermId == term.Id && x.Language == language).OrderBy(x => x.Accepted); if (entries.Any()) { LocalizationEntry lastAccepted = entries.LastOrDefault(x => !x.Accepted.HasValue || x.Accepted == true); if (lastAccepted != null) { langValue = lastAccepted.Value; } } else { if (fillGaps) { langValue = term.Value; } } if (!string.IsNullOrWhiteSpace(langValue)) { sb.AppendLine(String.Format("<string id=\"{0}\">{1}</string>", term.Key, langValue)); } } sb.AppendLine(); sb.AppendLine("</resources>"); return(sb.ToString()); }
public void SetTerms(Guid projectId, IEnumerable <LocalizationTerm> terms) { List <LocalizationTerm> existingTerms = repository.GetTerms(projectId).ToList(); foreach (LocalizationTerm term in terms) { LocalizationTerm existing = existingTerms.FirstOrDefault(x => x.Id == term.Id); if (existing == null) { repository.AddTerm(projectId, term); } else { existing.Key = term.Key; existing.Value = term.Value; existing.Obs = term.Obs; existing.LastUpdateDate = DateTime.Now; repository.UpdateTerm(projectId, existing); } } IEnumerable <LocalizationTerm> deleteTerms = existingTerms.Where(x => !terms.Contains(x)); if (deleteTerms.Any()) { List <LocalizationEntry> existingEntries = repository.GetEntries(projectId).ToList(); foreach (LocalizationTerm term in deleteTerms) { IEnumerable <LocalizationEntry> entries = existingEntries.Where(x => x.TermId == term.Id); repository.RemoveTerm(projectId, term.Id); foreach (LocalizationEntry entry in entries) { repository.RemoveEntry(projectId, entry.Id); } } } }
private static void FillEntries(DataTable dataTable, List <LocalizationTerm> terms, List <LocalizationEntry> entries, IEnumerable <KeyValuePair <int, SupportedLanguage> > columns) { foreach (DataRow row in dataTable.Rows) { object term = row.ItemArray[0]; foreach (KeyValuePair <int, SupportedLanguage> col in columns) { int colNumber = col.Key - 1; if (col.Key > row.ItemArray.Length) { break; } object translation = row.ItemArray[colNumber]; if (term == null || string.IsNullOrWhiteSpace(term.ToString()) || translation == null || string.IsNullOrWhiteSpace(translation.ToString())) { continue; } LocalizationTerm existingTerm = terms.FirstOrDefault(x => SanitizeKey(x.Key).Equals(SanitizeKey(term.ToString()))); if (existingTerm != null) { LocalizationEntry newEntry = new LocalizationEntry { TermId = existingTerm.Id, Value = translation.ToString().Trim(), Language = col.Value }; entries.Add(newEntry); } } } }