public ActionResult Translations(Guid id, bool? afterSave) { var word = _wordRepository.Find(id); var activeDict = _authService.CurrentAccount.ActiveDictionary; var model = new TranslationsModel { Word = word }; var currentWordTranslations = word.Translations.ToList(); model.Translations = currentWordTranslations; if (afterSave.HasValue && afterSave.Value && currentWordTranslations.Count == 0) { try { var translationsFromTranslator = _translationService.TranslateText(activeDict.From, activeDict.To, word.Original); if (translationsFromTranslator.Length > 0) { foreach (var translationFromTranslator in translationsFromTranslator) { var newTranslation = new Translation { Word = word, Value = translationFromTranslator }; currentWordTranslations.Add(newTranslation); } } else { var newTranslation = new Translation { Word = word }; currentWordTranslations.Add(newTranslation); } } catch (TranslatorException) { var newTranslation = new Translation { Word = word }; currentWordTranslations.Add(newTranslation); } } return View(model); }
public ActionResult SaveAndAddTranslation(Guid id, List<Translation> translations, string isSave) { var word = _wordRepository.Find(id); ModelState.Clear(); var model = new TranslationsModel { Word = word, Translations = translations }; if (translations == null) model.Translations = word.Translations.ToList(); if (translations != null && translations.Any(t => t.ID == Guid.Empty) && word.Translations.Any(t => translations.Single(tt => tt.ID == Guid.Empty).Value.ToLower() == t.Value.ToLower())) { model.Translations = word.Translations.ToList(); var newTranslation = new Translation { Word = word }; model.Translations.Add(newTranslation); ModelState.AddModelError(String.Empty, "This translation already exists."); } if (!ModelState.IsValid) { ModelState.AddModelError(String.Empty, Messages.FieldsInvalid); return View("Translations", model); } if (translations != null) { var translationsWithValues = translations.Where(t => t.Value != "__IGNORE").ToArray(); bool isTranslationsAdded = false; foreach (var translation in translationsWithValues) { var doesTranslationNotExist = !word.Translations.Any(t => t.ID == translation.ID); if (doesTranslationNotExist) { word.Translations.Add(translation); isTranslationsAdded = true; } } if (isTranslationsAdded) { _wordRepository.SaveChanges(); } model.Translations = word.Translations.ToList(); } if (isSave == "1") { return RedirectToAction("List", new { dictionaryId = word.DictionaryID }); } else { var newTranslation = new Translation { Word = word }; model.Translations.Add(newTranslation); return View("Translations", model); } }