public void Delete(string name, 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)
            {
                records.Remove(records.FirstOrDefault(a => a.Name == name && a.CultureName == cultureName &&
                                                      a.ResourceName == computedKey));

                JsonReader.Write(records, computedPath);

                if (_options.CacheDependency == CacheOption.IMemoryCache)
                {
                    _memoryCache.Remove(computedKey);
                }
                else
                {
                    _distributedCache.Remove(computedKey);
                }
            }
        }
        public string ExportJson(string cultureName, string resourceName)
        {
            var    resource     = string.IsNullOrEmpty(resourceName) ? nameof(LocalizationResourceNames.SharedResource) : resourceName;
            string computedPath = string.Format(JsonConfiguration.FileName(), cultureName, resource);

            return(File.ReadAllText(computedPath));
        }
示例#3
0
        private void Connect()
        {
            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);

            Parallel.ForEach(records, (record) => {
                _cache.Set(record.ResourceName, record.Value);
            });
        }
        public void Insert(IEnumerable <KeyValuePair <string, string> > keyValue, 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 <KeyValuePair <string, string> > isSuccess = new List <KeyValuePair <string, string> >();

            foreach (KeyValuePair <string, string> item in keyValue)
            {
                string             computedKey = string.Format(DefaultConfiguration.LocalizationCacheKeyTemplate, CultureInfo.CurrentUICulture.Name, resource, item.Key);
                LocalizationRecord entity      = records.FirstOrDefault(a => a.Name == item.Key && a.CultureName == cultureName &&
                                                                        a.ResourceName == computedKey);

                if (entity == null)
                {
                    entity = new LocalizationRecord
                    {
                        Name         = item.Key,
                        Value        = item.Value,
                        CultureName  = cultureName,
                        ResourceName = computedKey,
                    };

                    records.Add(entity);
                    isSuccess.Add(new KeyValuePair <string, string>(computedKey, item.Value));
                }
            }

            JsonReader.Write(records, computedPath);

            if (_options.CacheDependency == CacheOption.IMemoryCache)
            {
                foreach (KeyValuePair <string, string> item in isSuccess)
                {
                    _memoryCache.Set(item.Key, item.Value);
                }
            }
            else
            {
                foreach (KeyValuePair <string, string> item in isSuccess)
                {
                    _distributedCache.SetString(item.Key, item.Value);
                }
            }
        }
        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);
        }