Example #1
0
 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;
 }
Example #2
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 = 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 = $"Line {lineCounter}: a key and a value are required."
                        });

                        continue;
                    }

                    var key = keyValuePair[0];

                    if (string.IsNullOrEmpty(key))
                    {
                        // Ignore empty keys
                        continue;
                    }
                    key = key.Trim();

                    var value = keyValuePair[1];

                    var resourceKey = GetResourceKey(key);

                    if (language == null)
                    {
                        throw new ApplicationException("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 =
                                $"A new key named '{key}' has been created, and will require a value in all languages."
                        });
                    }

                    // 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
                        };
                        Add(stringResource);
                    }
                }
            }
            catch (Exception ex)
            {
                report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message });
            }

            _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName);
            return report;
        }
Example #3
0
 public LocaleResourceKey SanitizeLocaleResourceKey(LocaleResourceKey localeResourceKey)
 {
     localeResourceKey.Name = StringUtils.SafePlainText(localeResourceKey.Name);
     localeResourceKey.Notes = StringUtils.SafePlainText(localeResourceKey.Notes);
     return localeResourceKey;
 }
Example #4
0
        /// <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);
            }
        }
Example #5
0
        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);
        }
Example #6
0
 public void Delete(LocaleResourceKey item)
 {
     _context.LocaleResourceKey.Remove(item);
 }
Example #7
0
        /// <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 = GetResourceKey(newLocaleResourceKey.Name);

            if (existingResourceKey != null)
            {
                throw new ApplicationException(
                    $"The resource key with name '{newLocaleResourceKey.Name}' already exists.");
            }

            newLocaleResourceKey.DateAdded = DateTime.UtcNow;

            // Now add an empty value for each language
            newLocaleResourceKey.LocaleStringResources = new List<LocaleStringResource>();
            foreach (var language in 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 = _context.LocaleResourceKey.Add(newLocaleResourceKey);

            // Clear hard cache for Languages
            _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName);

            return result;
        }
Example #8
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 = 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          = $"Line {lineCounter}: a key and a value are required."
                        });

                        continue;
                    }

                    var key = keyValuePair[0];

                    if (string.IsNullOrWhiteSpace(key))
                    {
                        // Ignore empty keys
                        continue;
                    }
                    key = key.Trim();

                    var value = keyValuePair[1];

                    var resourceKey = GetResourceKey(key);

                    if (language == null)
                    {
                        throw new ApplicationException("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          =
                                $"A new key named '{key}' has been created, and will require a value in all languages."
                        });
                    }

                    // 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
                        };
                        Add(stringResource);
                    }
                }
            }
            catch (Exception ex)
            {
                report.Errors.Add(new CsvErrorWarning {
                    ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message
                });
            }

            _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName);
            return(report);
        }
Example #9
0
 public LocaleResourceKey SanitizeLocaleResourceKey(LocaleResourceKey localeResourceKey)
 {
     localeResourceKey.Name  = StringUtils.SafePlainText(localeResourceKey.Name);
     localeResourceKey.Notes = StringUtils.SafePlainText(localeResourceKey.Notes);
     return(localeResourceKey);
 }
Example #10
0
 public void Delete(LocaleResourceKey item)
 {
     _context.LocaleResourceKey.Remove(item);
 }