public void Delete(IEnumerable <string> names, string cultureName, string resourceName) { var resource = string.IsNullOrEmpty(resourceName) ? nameof(LocalizationResourceNames.SharedResource) : resourceName; string computedPath = string.Format(JsonConfiguration.FileName(), CultureInfo.CurrentUICulture.Name, resource); List <LocalizationRecord> records = JsonReader.Read <List <LocalizationRecord> >(computedPath); List <string> isSuccess = new List <string>(); foreach (string item in names) { string computedKey = string.Format(DefaultConfiguration.LocalizationCacheKeyTemplate, CultureInfo.CurrentUICulture.Name, resource, item); LocalizationRecord entity = records.FirstOrDefault(a => a.Name == item && a.CultureName == cultureName && a.ResourceName == computedKey); if (entity != null) { records.Remove(records.FirstOrDefault(a => a.Name == item && a.CultureName == cultureName && a.ResourceName == computedKey)); isSuccess.Add(computedKey); } } JsonReader.Write(records, computedPath); if (_options.CacheDependency == CacheOption.IMemoryCache) { foreach (string item in isSuccess) { _memoryCache.Remove(item); } } else { foreach (string item in isSuccess) { _distributedCache.Remove(item); } } }
public void Insert(string name, string value, string cultureName, string resourceName) { var resource = string.IsNullOrEmpty(resourceName) ? nameof(LocalizationResourceNames.SharedResource) : resourceName; string computedKey = string.Format(DefaultConfiguration.LocalizationCacheKeyTemplate, CultureInfo.CurrentUICulture.Name, resource, name); string computedPath = string.Format(JsonConfiguration.FileName(), CultureInfo.CurrentUICulture.Name, resource); List <LocalizationRecord> records = JsonReader.Read <List <LocalizationRecord> >(computedPath); LocalizationRecord entity = records.FirstOrDefault(a => a.Name == name && a.CultureName == cultureName && a.ResourceName == computedKey); if (entity == null) { entity = new LocalizationRecord { Name = name, Value = value, CultureName = cultureName, ResourceName = computedKey, }; records.Add(entity); JsonReader.Write(records, computedPath); if (_options.CacheDependency == CacheOption.IMemoryCache) { _memoryCache.Set(computedKey, value); } else { _distributedCache.SetString(computedKey, value); } } else { Update(name, value, cultureName, resourceName); } }
public string ExportXml(string cultureName, string resourceName) { var resource = string.IsNullOrEmpty(resourceName) ? nameof(LocalizationResourceNames.SharedResource) : resourceName; string computedPath = string.Format(JsonConfiguration.FileName(), cultureName, resource); List <LocalizationRecord> records = JsonReader.Read <List <LocalizationRecord> >(computedPath); XmlSerializer xmlSerializer = new XmlSerializer(typeof(List <LocalizationRecord>)); MemoryStream memoryStream = new MemoryStream(); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8) { Formatting = Formatting.Indented }; xmlSerializer.Serialize(xmlTextWriter, records); string output = Encoding.UTF8.GetString(memoryStream.ToArray()); string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); if (output.StartsWith(_byteOrderMarkUtf8, StringComparison.Ordinal)) { output = output.Remove(0, _byteOrderMarkUtf8.Length); } return(output); }