Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(string baseKey, string mainKey, KeyVM model)
        {
            var key = await _context.LocalizationKeys
                      .Include(l => l.Records)
                      .ThenInclude(l => l.Culture)
                      .SingleOrDefaultAsync(k => k.Id == model.Id);

            if (key == null)
            {
                return(NotFound());
            }

            if (model.Comment == LocalizationKey.DEFAULT_COMMENT)
            {
                model.Comment = "";
            }
            key.Comment = model.Comment;
            foreach (var modelRecord in model.Translations)
            {
                var dbRecord = key.Records.SingleOrDefault(r => r.Culture.Id == modelRecord.CultureId);
                if (dbRecord == null)
                {
                    dbRecord = new LocalizationRecord()
                    {
                        Culture         = await _context.SupportedCultures.SingleOrDefaultAsync(c => c.Id == modelRecord.CultureId),
                        LocalizationKey = key,
                        Text            = modelRecord.Translation,
                        Status          = string.IsNullOrWhiteSpace(modelRecord.Translation) ? RecordStatus.New : RecordStatus.HumanTranslated
                    };
                    _context.LocalizationRecords.Add(dbRecord);
                    _context.Entry(dbRecord).State = EntityState.Added;
                }
                else
                {
                    dbRecord.Text   = modelRecord.Translation;
                    dbRecord.Status = string.IsNullOrWhiteSpace(modelRecord.Translation) ? RecordStatus.New : RecordStatus.HumanTranslated;
                    _context.Entry(dbRecord).State = EntityState.Modified;
                }
            }
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Edit), new { baseKey = key.Base, mainKey = key.Key, saveResult = SaveResult.Success }));
        }
        private string _getLocalizationString(LocalizationContext context, CultureInfo culture, string baseKey, string mainKey, bool updateLastUsed)
        {
            var cultureName = culture.Name;
            var dbCulture   = context.SupportedCultures.SingleOrDefault(c => c.Name == cultureName && c.IsSupported);

            if (dbCulture == null)
            {
                return(null);
            }

            var dbKey = context.LocalizationKeys.SingleOrDefault(k => k.Base == baseKey && k.Key == mainKey);

            if (dbKey == null)
            {
                // Key doesn't exist, create!
                dbKey = new LocalizationKey()
                {
                    Base    = baseKey,
                    Key     = mainKey,
                    Comment = LocalizationKey.DEFAULT_COMMENT
                };
                try
                {
                    context.LocalizationKeys.Add(dbKey);
                    context.SaveChanges();
                }
                catch (DbUpdateException ex) when((ex.InnerException as System.Data.SqlClient.SqlException)?.Number == 2601)
                {
                    // Key already exists, (concurrent request probably added it): reload entity.
                    context.Entry(dbKey).State = EntityState.Detached;
                    dbKey = context.LocalizationKeys.SingleOrDefault(k => k.Base == baseKey && k.Key == mainKey);
                }
            }

            var localization = context.LocalizationRecords
                               .Where(r => r.LocalizationKey.Id == dbKey.Id && r.Culture.Id == dbCulture.Id)
                               .SingleOrDefault();

            if (updateLastUsed && localization != null)
            {
                localization.LastUsed = DateTime.Now;
                context.SaveChanges();
            }

            return(localization?.Text);
        }
Ejemplo n.º 3
0
 public virtual bool Edit(T entity)
 {
     _context.Entry(entity).State = EntityState.Modified;
     return(true);
 }