public ActionResult AddResourceKey(LocaleResourceKeyViewModel newResourceKeyViewModel) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { try { var resourceKeyToSave = new LocaleResourceKey { Name = newResourceKeyViewModel.Name, Notes = newResourceKeyViewModel.Notes, DateAdded = newResourceKeyViewModel.DateAdded }; LocalizationService.Add(resourceKeyToSave); unitOfWork.Commit(); ShowSuccess("Resource key created successfully"); var currentLanguage = SettingsService.GetSettings().DefaultLanguage.Id; return RedirectToAction("ManageLanguageResourceValues", new { languageId = currentLanguage }); } catch (Exception ex) { unitOfWork.Rollback(); ShowError(ex.Message); LoggingService.Error(ex); return RedirectToAction("AddResourceKey"); } } }
public void ExportCsv() { var testGuid = Guid.NewGuid(); var resourceKey1 = new LocaleResourceKey { DateAdded = DateTime.UtcNow, Id = Guid.NewGuid(), Name = "testKey1", Notes = "test notes" }; var resourceValue1 = new LocaleStringResource { LocaleResourceKey = resourceKey1, ResourceValue = "testValue1" }; var resourceKey2 = new LocaleResourceKey { DateAdded = DateTime.UtcNow, Id = Guid.NewGuid(), Name = "testKey2", Notes = "test notes" }; var resourceValue2 = new LocaleStringResource { LocaleResourceKey = resourceKey2, ResourceValue = "testValue2" }; var language = new Language {Id = testGuid, LanguageCulture = "en-GB", Name = "TestLanguage"}; _localizationRepositorySub.AllLanguageResources(testGuid).Returns(new List<LocaleStringResource> { resourceValue1, resourceValue2 }); _localizationRepositorySub.Get(testGuid).Returns(language); var csv = _localizationService.ToCsv(language); Assert.AreEqual(csv, "testKey1,testValue1\r\ntestKey2,testValue2\r\n"); }
public void Delete(LocaleResourceKey item) { _context.LocaleResourceKey.Remove(item); }
/// <summary> /// Add a new resource key /// </summary> /// <param name="newLocaleResourceKey"></param> public LocaleResourceKey Add(LocaleResourceKey newLocaleResourceKey) { _context.LocaleResourceKey.Add(newLocaleResourceKey); return newLocaleResourceKey; }
public void Update(LocaleResourceKey item) { // Check there's not an object with same identifier already in context if (_context.LocaleResourceKey.Local.Select(x => x.Id == item.Id).Any()) { throw new ApplicationException("Object already exists in context - you do not need to call Update. Save occurs on Commit"); } _context.Entry(item).State = EntityState.Modified; }
/// <summary> /// Delete a resource key /// </summary> /// <param name="resourceKey"></param> public void DeleteResourceKey(LocaleResourceKey resourceKey) { // Delete all the string resources referencing this key var allValuesForKey = GetAllValuesForKey(resourceKey.Id); var allValuesToDelete = new List<LocaleStringResource>(); allValuesToDelete.AddRange(allValuesForKey); foreach (var valueToDelete in allValuesToDelete) { _context.LocaleStringResource.Remove(valueToDelete); } _context.LocaleResourceKey.Remove(resourceKey); }
public void ImportLanguage() { // Ensure language does not exist _localizationRepositorySub.GetLanguageByLanguageCulture(Arg.Any<string>()).Returns(x => null); var resourceKey1 = new LocaleResourceKey { DateAdded = DateTime.UtcNow, Id = Guid.NewGuid(), Name = "testKey1", Notes = "test notes" }; var resourceValue1 = new LocaleStringResource { LocaleResourceKey = resourceKey1, ResourceValue = "testValue1" }; var resourceKey2 = new LocaleResourceKey { DateAdded = DateTime.UtcNow, Id = Guid.NewGuid(), Name = "testKey2", Notes = "test notes" }; var resourceValue2 = new LocaleStringResource { LocaleResourceKey = resourceKey1, ResourceValue = "testValue2" }; var newLanguage = new Language { LanguageCulture = "fr-FR", Name = "French", LocaleStringResources = new List<LocaleStringResource> { resourceValue1, resourceValue2 }, }; _localizationRepositorySub.GetAllResourceKeys().Returns(new List<LocaleResourceKey> { resourceKey1, resourceKey2 }); _localizationRepositorySub.GetResourceKey("testKey1").Returns(resourceKey1); _localizationRepositorySub.GetResourceKey("testKey2").Returns(resourceKey2); _localizationRepositorySub.GetResourceKey("testKeyNew").Returns(x => null); _localizationRepositorySub.GetAll().Returns(new List<Language> { newLanguage }); var testData = new List<string> { "testKey1,testValue1", "testKey2,testValue2", ",should not import", // Test for ignore of empty key "testKeyNew,testValueNew" }; var report = _localizationService.FromCsv("fr-FR", testData); Assert.AreEqual(report.Warnings.Count, 1); Assert.AreEqual(report.Warnings[0].ErrorWarningType, CsvErrorWarningType.NewKeyCreated); Assert.AreEqual(report.Errors.Count, 0); }
public CsvReport FromCsv(Language language, List<string> allLines) { var commaSeparator = new[] { ',' }; var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No language keys or values found." }); return report; } try { //var allResourceKeys = _localizationRepository.GetAllResourceKeys(); var lineCounter = 0; foreach (var line in allLines) { lineCounter++; //var keyValuePair = line.Split(commaSeparator); var keyValuePair = line.Split(commaSeparator, 2, StringSplitOptions.None); if (keyValuePair.Length < 2) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = string.Format("Line {0}: a key and a value are required.", lineCounter) }); continue; } var key = keyValuePair[0]; if (string.IsNullOrEmpty(key)) { // Ignore empty keys continue; } key = key.Trim(); var value = keyValuePair[1]; var resourceKey = _localizationRepository.GetResourceKey(key); if (language == null) { throw new ApplicationException(string.Format("Unable to create language")); } // If key does not exist, it is a new one to be created if (resourceKey == null) { resourceKey = new LocaleResourceKey { Name = key, DateAdded = DateTime.UtcNow, }; Add(resourceKey); report.Warnings.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.NewKeyCreated, Message = string.Format("A new key named '{0}' has been created, and will require a value in all languages.", key) }); } // In the new language (only) set the value for the resource var stringResource = language.LocaleStringResources.FirstOrDefault(res => res.LocaleResourceKey.Name == resourceKey.Name); if (stringResource != null) { if (!stringResource.ResourceValue.Equals(value)) { stringResource.ResourceValue = value; } } else { // No string resources have been created, so most probably // this is the installer creating the keys so we need to create the // string resource to go with it and add it stringResource = new LocaleStringResource { Language = language, LocaleResourceKey = resourceKey, ResourceValue = value }; _localizationRepository.Add(stringResource); } } } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message }); } _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName); return report; }
/// <summary> /// Delete a resource key - warning: this will delete all the associated resource strings in all languages /// for this key /// </summary> public void DeleteLocaleResourceKey(LocaleResourceKey resourceKey) { try { // Delete the key and its values _localizationRepository.DeleteResourceKey(resourceKey); _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName); } catch (Exception ex) { throw new ApplicationException(string.Format("Unable to delete resource key: {0}", ex.Message), ex); } }
public LocaleResourceKey SanitizeLocaleResourceKey(LocaleResourceKey localeResourceKey) { localeResourceKey.Name = StringUtils.SafePlainText(localeResourceKey.Name); localeResourceKey.Notes = StringUtils.SafePlainText(localeResourceKey.Notes); return localeResourceKey; }
/// <summary> /// Add a new resource key /// </summary> /// <param name="newLocaleResourceKey"></param> public LocaleResourceKey Add(LocaleResourceKey newLocaleResourceKey) { // Trim name to stop any issues newLocaleResourceKey.Name = newLocaleResourceKey.Name.Trim(); // Check to see if a respource key of this name already exists var existingResourceKey = _localizationRepository.GetResourceKey(newLocaleResourceKey.Name); if (existingResourceKey != null) { throw new ApplicationException(string.Format("The resource key with name '{0}' already exists.", newLocaleResourceKey.Name)); } newLocaleResourceKey.DateAdded = DateTime.UtcNow; // Now add an empty value for each language newLocaleResourceKey.LocaleStringResources = new List<LocaleStringResource>(); foreach (var language in _localizationRepository.GetAll()) { var resourceValue = new LocaleStringResource { Language = language, LocaleResourceKey = newLocaleResourceKey, ResourceValue = string.Empty }; language.LocaleStringResources.Add(resourceValue); } //Sanitze newLocaleResourceKey = SanitizeLocaleResourceKey(newLocaleResourceKey); // Add the key var result = _localizationRepository.Add(newLocaleResourceKey); // Clear hard cache for Languages _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName); return result; }
/// <summary> /// Import a language from CSV /// </summary> /// <param name="langKey"> </param> /// <param name="allLines"></param> /// <returns>A report on the import</returns> public CsvReport FromCsv(string langKey, List<string> allLines) { var commaSeparator = new[] { ',' }; var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No language keys or values found." }); return report; } // Look up the language and culture Language language; try { var cultureInfo = LanguageUtils.GetCulture(langKey); if (cultureInfo == null) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.DoesNotExist, Message = string.Format("The language culture '{0}' does not exist.", langKey) }); return report; } // See if this language exists already, and if not then create it language = GetLanguageByLanguageCulture(langKey) ?? Add(cultureInfo); } catch (LanguageOrCultureAlreadyExistsException ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = ex.Message }); return report; } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.ItemBad, Message = ex.Message }); return report; } try { var lineCounter = 0; foreach (var line in allLines) { lineCounter++; var keyValuePair = line.Split(commaSeparator); if (keyValuePair.Length < 2) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = string.Format("Line {0}: a key and a value are required.", lineCounter) }); continue; } var key = keyValuePair[0]; if (string.IsNullOrEmpty(key)) { // Ignore empty keys continue; } key = key.Trim(); var value = keyValuePair[1]; var resourceKey = _localizationRepository.GetResourceKey(key); if (language == null) { throw new ApplicationException(string.Format("Unable to create language")); } // If key does not exist, it is a new one to be created if (resourceKey == null) { resourceKey = new LocaleResourceKey { Name = key, DateAdded = DateTime.UtcNow, }; Add(resourceKey); report.Warnings.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.NewKeyCreated, Message = string.Format("A new key named '{0}' has been created, and will require a value in all languages.", key) }); } // In the new language (only) set the value for the resource var stringResources = language.LocaleStringResources.Where(res => res.LocaleResourceKey.Name == resourceKey.Name).ToList(); if (stringResources.Any()) { foreach (var res in stringResources) { res.ResourceValue = value; break; } } else { // No string resources have been created, so most probably // this is the installer creating the keys so we need to create the // string resource to go with it and add it var stringResource = new LocaleStringResource { Language = language, LocaleResourceKey = resourceKey, ResourceValue = value }; _localizationRepository.Add(stringResource); } } } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message }); } return report; }
/// <summary> /// Delete a resource key - warning: this will delete all the associated resource strings in all languages /// for this key /// </summary> public void DeleteLocaleResourceKey(LocaleResourceKey resourceKey) { try { // Delete the key and its values _localizationRepository.DeleteResourceKey(resourceKey); } catch (Exception ex) { throw new ApplicationException(string.Format("Unable to delete resource key: {0}", ex.Message), ex); } }
/// <summary> /// Delete a resource key - warning: this will delete all the associated resource strings in all languages /// for this key /// </summary> public void DeleteLocaleResourceKey(LocaleResourceKey resourceKey) { try { // Delete the key and its values DeleteResourceKey(resourceKey); _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName); } catch (Exception ex) { throw new ApplicationException($"Unable to delete resource key: {ex.Message}", ex); } }