private void CheckAndAdd(ref ImportExportDictionaryItems entries, ImportExportDictionaryItem entry) { if (entries[entry.Key] == null) { entries.Items.Add(entry); } }
public JsonResult Create(AuditDictionaryItem item) { var languages = localizationService.GetAllLanguages(); var mainLanguage = localizationService.GetDefaultLanguageIsoCode(); var ok = false; var mainLanguageDictionary = new ImportExportDictionaryItem { Key = item.Key, ParentKey = item.ParentKey, Value = string.Empty, LanguageCode = mainLanguage }; mainLanguageDictionary.Translations = new Dictionary <string, string>(); if (!languageDictionaryService.ParentExists(item.ParentKey)) { var parentDictionaryItem = new ImportExportDictionaryItem { Key = item.ParentKey, Value = item.Key, LanguageCode = mainLanguage, Translations = new Dictionary <string, string>() }; languageDictionaryService.CreateDictionaryItem(parentDictionaryItem); } foreach (var language in languages) { if (language.IsoCode != mainLanguage) { mainLanguageDictionary.Translations.Add(language.IsoCode, string.Empty); } } var result = languageDictionaryService.CreateDictionaryItem(mainLanguageDictionary); if (result.Item2) { ok = true; } else { ok = false; } return(base.Json(new { Created = ok }, JsonRequestBehavior.DenyGet)); }
public (IDictionaryItem, bool) CreateDictionaryItem(ImportExportDictionaryItem item) { var dicItem = localizationService.GetDictionaryItemByKey(item.Key) ?? new DictionaryItem(item.Key); var lang = localizationService.GetLanguageByIsoCode(item.LanguageCode); if (lang == null) { lang = new Language(item.LanguageCode); localizationService.Save(lang); } List <IDictionaryTranslation> translations = new List <IDictionaryTranslation>(); var mainLanguage = localizationService.GetLanguageByIsoCode(item.LanguageCode); var mainTranslation = dicItem.Translations.FirstOrDefault(x => x.Language.Equals(mainLanguage)); if (mainTranslation != null) { if (!string.IsNullOrEmpty(item.Value)) { mainTranslation.Value = item.Value; translations.Add(mainTranslation); } } else { translations.Add(new DictionaryTranslation(mainLanguage, item.Value ?? string.Empty)); } foreach (var translation in item.Translations ?? new Dictionary <string, string>()) { var translationLanguage = localizationService.GetLanguageByIsoCode(translation.Key); if (translationLanguage == null) { translationLanguage = new Language(translation.Key); localizationService.Save(translationLanguage); } var t = dicItem.Translations.FirstOrDefault(x => x.Language.IsoCode.Equals(translation.Key)); if (t != null) { t.Value = translation.Value ?? string.Empty; translations.Add(t); } else { translations.Add(new DictionaryTranslation(translationLanguage, translation.Value ?? string.Empty)); } } dicItem.Translations = translations; if (!string.IsNullOrEmpty(item.ParentKey)) { var parentGuid = localizationService.GetDictionaryItemByKey(item.ParentKey)?.GetUdi().Guid; if (parentGuid != null && !dicItem.ParentId.Equals(parentGuid)) { dicItem.ParentId = parentGuid; } } try { localizationService.Save(dicItem); return(dicItem, true); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return(dicItem, false); } }
public JsonResult ImportDictionaries(HttpPostedFileBase file, bool overrideExisting = false) { var entries = new ImportExportDictionaryItems(); List <ImportResult> results = new List <ImportResult>(); if (file?.ContentLength > 0) { const int RowStart = 2; // ignore headers ImportDigest excelDigest = null; var import = new List <ExcelCell>(); bool importSuccessful = false; excelDigest = new ImportDigest(file.InputStream); import = excelDigest.Import(); importSuccessful = excelDigest.ErrorList.Errors.Count == 0; //var languages = localizationService.GetAllLanguages(); if (importSuccessful) { for (int i = RowStart; i <= excelDigest.RowsImported; i++) { var current = new ImportExportDictionaryItem { ParentKey = import.Find(x => x.CellName == $"A{i}").CellValue?.ToString(), Key = import.Find(x => x.CellName == $"B{i}").CellValue?.ToString(), Value = import.Find(x => x.CellName == $"C{i}").CellValue != null?import.Find(x => x.CellName == $"C{i}").CellValue .ToString() .Trim() .Replace("\n", string.Empty) .Replace("\t", string.Empty) : string.Empty, LanguageCode = import.Find(x => x.CellName == $"D{i}").CellValue?.ToString() }; var previous = entries.Items.Count > 0 ? entries[current.Key] : null; if (previous != null) { if (current.ParentKey.Equals(previous.Key) || current.ParentKey.Equals(previous.ParentKey)) { if (current.Key.Equals(previous.Key)) { if (current.LanguageCode.Equals(previous.LanguageCode)) { if (!string.IsNullOrEmpty(current.Value) && !string.IsNullOrEmpty(previous.Value)) { if (!current.Value.Equals(previous.Value)) { previous.Value = current.Value; } } else if (!string.IsNullOrEmpty(current.Value) && string.IsNullOrEmpty(previous.Value)) { previous.Value = current.Value; } else if (string.IsNullOrEmpty(current.Value) && string.IsNullOrEmpty(previous.Value)) { previous.Value = string.Empty; } entries[current.Key] = current; } else { previous.Translations = previous.Translations ?? new Dictionary <string, string>(); var translation = previous.Translations.SingleOrDefault(x => x.Key.Equals(current.LanguageCode)); if (translation.Equals(default(KeyValuePair <string, string>))) { previous.Translations.Add(current.LanguageCode, current.Value); } else if (string.IsNullOrEmpty(translation.Value)) { previous.Translations[current.LanguageCode] = current.Value ?? string.Empty; } entries[current.Key] = previous; } } else { CheckAndAdd(ref entries, current); } } else { CheckAndAdd(ref entries, current); } } else { CheckAndAdd(ref entries, current); } } } results = languageDictionaryService.ImportDictionaryItems(entries.Items, overrideExisting); } return(base.Json(new { Success = results.Count > 0, count = results.Count, results }, JsonRequestBehavior.DenyGet)); }