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(LocaleStringResource item) { _context.LocaleStringResource.Remove(item); }
/// <summary> /// Add a new resource key /// </summary> /// <param name="newLocaleStringResource"></param> public LocaleStringResource Add(LocaleStringResource newLocaleStringResource) { _context.LocaleStringResource.Add(newLocaleStringResource); return newLocaleStringResource; }
public void Update(LocaleStringResource item) { // Check there's not an object with same identifier already in context if (_context.LocaleStringResource.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 value /// </summary> /// <param name="resourceValue"></param> public void DeleteResourceValue(LocaleStringResource resourceValue) { _context.LocaleStringResource.Remove(resourceValue); }
/// <summary> /// Create a string resource /// </summary> /// <param name="newLocaleStringResource"></param> /// <returns></returns> private LocaleStringResource Add(LocaleStringResource newLocaleStringResource) { newLocaleStringResource = SanitizeLocaleStringResource(newLocaleStringResource); return _localizationRepository.Add(newLocaleStringResource); }
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; }
public LocaleStringResource SanitizeLocaleStringResource(LocaleStringResource localeStringResource) { localeStringResource.ResourceValue = StringUtils.SafePlainText(localeStringResource.ResourceValue); return localeStringResource; }
/// <summary> /// Add a new language into the system (does NOT set current language) /// </summary> /// <param name="language"></param> public void Add(Language language) { // Does the language already exist by name or language-locale? var existingLanguage = GetLanguageByLanguageCulture(language.LanguageCulture); if (existingLanguage != null) { throw new LanguageOrCultureAlreadyExistsException(string.Format("There is already a language defined for language-culture '{0}'", existingLanguage.LanguageCulture)); } // Make sure that the new language has a set of empty resources language.LocaleStringResources = new List<LocaleStringResource>(); foreach (var localeResourceKey in _localizationRepository.GetAllResourceKeys()) { var localeStringResource = new LocaleStringResource { Language = language, LocaleResourceKey = localeResourceKey, ResourceValue = string.Empty }; language.LocaleStringResources.Add(localeStringResource); } language = SanitizeLanguage(language); _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName); _localizationRepository.Add(language); }
/// <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; }