public EntityTransaltion[] Find(System.Globalization.CultureInfo culture, params EntityKey[] keys)
        {
            var result = new EntityTransaltion[keys.Length];

            using (var db = new MultilingualDbContext(InstanceName))
            {
                for (var i = 0; i < keys.Length; i++)
                {
                    var key   = keys[i];
                    var entry = db.Translations.Find(GetUnderlyingEntityKey(culture.Name, key));

                    if (entry != null)
                    {
                        var translation = new EntityTransaltion(culture.Name, new EntityKey(key.EntityType, key.Value))
                        {
                            IsOutOfDate          = entry.IsOutOfDate,
                            PropertyTranslations = JsonConvert.DeserializeObject <List <PropertyTranslation> >(entry.Properties)
                        };

                        result[i] = translation;
                    }
                }
            }

            return(result);
        }
Пример #2
0
        public EntityTransaltion[] Find(System.Globalization.CultureInfo culture, params EntityKey[] keys)
        {
            var result = new EntityTransaltion[keys.Length];
            // Key: entity key, Value: the index in the result array
            var uncachedKeys = new List<KeyValuePair<EntityKey, int>>();

            _lock.EnterReadLock();

            try
            {
                if (_cache.ContainsKey(culture))
                {
                    var cache = _cache[culture];

                    for (var i = 0; i < keys.Length; i++)
                    {
                        EntityTransaltion translation;
                        if (cache.TryGetValue(keys[i], out translation))
                        {
                            result[i] = translation == null ? null : translation.Clone();
                        }
                        else
                        {
                            uncachedKeys.Add(new KeyValuePair<EntityKey, int>(keys[i], i));
                        }
                    }
                }
                else
                {
                    for (var i = 0; i < keys.Length; i++)
                    {
                        uncachedKeys.Add(new KeyValuePair<EntityKey, int>(keys[i], i));
                    }
                }
            }
            finally
            {
                _lock.ExitReadLock();
            }

            // 对于不在缓存中的Key,要从底下的真实存储中查询
            if (uncachedKeys.Count > 0)
            {
                var translations = FindInUnderlyingStore(culture, uncachedKeys.Select(it => it.Key).ToArray());
                for (var i = 0; i < translations.Length; i++)
                {
                    var translation = translations[i];
                    var resultIndex = uncachedKeys[i].Value;
                    result[resultIndex] = IsEmptyTranslation(translation) ? null : translation.Clone();
                }

                UpdateCache(culture, translations);
            }

            return result;
        }
Пример #3
0
        public EntityTransaltion[] Find(System.Globalization.CultureInfo culture, params EntityKey[] keys)
        {
            var result = new EntityTransaltion[keys.Length];
            // Key: entity key, Value: the index in the result array
            var uncachedKeys = new List <KeyValuePair <EntityKey, int> >();

            _lock.EnterReadLock();

            try
            {
                if (_cache.ContainsKey(culture))
                {
                    var cache = _cache[culture];

                    for (var i = 0; i < keys.Length; i++)
                    {
                        EntityTransaltion translation;
                        if (cache.TryGetValue(keys[i], out translation))
                        {
                            result[i] = translation == null ? null : translation.Clone();
                        }
                        else
                        {
                            uncachedKeys.Add(new KeyValuePair <EntityKey, int>(keys[i], i));
                        }
                    }
                }
                else
                {
                    for (var i = 0; i < keys.Length; i++)
                    {
                        uncachedKeys.Add(new KeyValuePair <EntityKey, int>(keys[i], i));
                    }
                }
            }
            finally
            {
                _lock.ExitReadLock();
            }

            // 对于不在缓存中的Key,要从底下的真实存储中查询
            if (uncachedKeys.Count > 0)
            {
                var translations = FindInUnderlyingStore(culture, uncachedKeys.Select(it => it.Key).ToArray());
                for (var i = 0; i < translations.Length; i++)
                {
                    var translation = translations[i];
                    var resultIndex = uncachedKeys[i].Value;
                    result[resultIndex] = IsEmptyTranslation(translation) ? null : translation.Clone();
                }

                UpdateCache(culture, translations);
            }

            return(result);
        }
        public Pagination <EntityTransaltion> FindOutOfDate(CultureInfo culture, Type entityType, int pageInex, int pageSize)
        {
            using (var db = new MultilingualDbContext(InstanceName))
            {
                return(db.Translations
                       .Where(t => t.Culture == culture.Name && t.EntityType == entityType.Name && t.IsOutOfDate)
                       .OrderBy(t => t.EntityKey)
                       .Paginate(pageInex, pageSize)
                       .Transform(entry =>
                {
                    var keyType = EntityKey.GetKeyProperty(entityType).PropertyType;
                    var translation = new EntityTransaltion(culture.Name, new EntityKey(entityType, Convert.ChangeType(entry.EntityKey, keyType)))
                    {
                        IsOutOfDate = true,
                        PropertyTranslations = JsonConvert.DeserializeObject <List <PropertyTranslation> >(entry.Properties)
                    };

                    return translation;
                }));
            }
        }
Пример #5
0
        public EntityTransaltion[] Find(System.Globalization.CultureInfo culture, params EntityKey[] keys)
        {
            var result = new EntityTransaltion[keys.Length];

            using (var db = new MultilingualDbContext(InstanceName))
            {
                for (var i = 0; i < keys.Length; i++)
                {
                    var key = keys[i];
                    var entry = db.Translations.Find(GetUnderlyingEntityKey(culture.Name, key));

                    if (entry != null)
                    {
                        var translation = new EntityTransaltion(culture.Name, new EntityKey(key.EntityType, key.Value))
                        {
                            IsOutOfDate = entry.IsOutOfDate,
                            PropertyTranslations = JsonConvert.DeserializeObject<List<PropertyTranslation>>(entry.Properties)
                        };

                        result[i] = translation;
                    }
                }
            }

            return result;
        }
Пример #6
0
        public Pagination<EntityTransaltion> FindOutOfDate(CultureInfo culture, Type entityType, int pageInex, int pageSize)
        {
            using (var db = new MultilingualDbContext(InstanceName))
            {
                return db.Translations
                         .Where(t => t.Culture == culture.Name && t.EntityType == entityType.Name && t.IsOutOfDate)
                         .OrderBy(t => t.EntityKey)
                         .Paginate(pageInex, pageSize)
                         .Transform(entry =>
                         {
                             var keyType = EntityKey.GetKeyProperty(entityType).PropertyType;
                             var translation = new EntityTransaltion(culture.Name, new EntityKey(entityType, Convert.ChangeType(entry.EntityKey, keyType)))
                             {
                                 IsOutOfDate = true,
                                 PropertyTranslations = JsonConvert.DeserializeObject<List<PropertyTranslation>>(entry.Properties)
                             };

                             return translation;
                         });
            }
        }
Пример #7
0
 private bool IsEmptyTranslation(EntityTransaltion translation)
 {
     return translation == null || translation is EmptyEntityTranslation;
 }
        private CustomFieldDefinitionModel LoadFieldDiff(CustomFieldDefinitionModel compared, string prefix, EntityTransaltion translation)
        {
            prefix = prefix + "[" + compared.Name + "].";

            var diff = new CustomFieldDefinitionModel
            {
                Id          = compared.Id,
                Name        = compared.Name,
                ControlType = compared.ControlType
            };

            if (translation != null)
            {
                diff.Label        = DiffHelper.GetDiffHtml(translation.GetOriginalText(prefix + "Label"), compared.Label);
                diff.DefaultValue = DiffHelper.GetDiffHtml(translation.GetOriginalText(prefix + "DefaultValue"), compared.DefaultValue);
            }

            foreach (var item in compared.SelectionItems)
            {
                var itemDiff = new SelectionItem {
                    Value = item.Value
                };
                if (translation != null)
                {
                    itemDiff.Text = DiffHelper.GetDiffHtml(translation.GetOriginalText(prefix + "SelectionItems[" + item.Value + "]"), item.Text);
                }
                diff.SelectionItems.Add(itemDiff);
            }

            return(diff);
        }
        private CustomFieldDefinitionModel LoadFieldTranslation(CustomFieldDefinitionModel original, string prefix, EntityTransaltion translation)
        {
            prefix = prefix + "[" + original.Name + "].";

            var translated = new CustomFieldDefinitionModel
            {
                Id          = original.Id,
                Name        = original.Name,
                ControlType = original.ControlType
            };

            if (translation != null)
            {
                translated.Label        = translation.GetTranslatedText(prefix + "Label");
                translated.DefaultValue = translation.GetTranslatedText(prefix + "DefaultValue");
            }

            foreach (var item in original.SelectionItems)
            {
                var translatedItem = new SelectionItem {
                    Value = item.Value
                };
                if (translation != null)
                {
                    translatedItem.Text = translation.GetTranslatedText(prefix + "SelectionItems[" + item.Value + "]");
                }
                translated.SelectionItems.Add(item);
            }

            return(translated);
        }
Пример #10
0
 private bool IsEmptyTranslation(EntityTransaltion translation)
 {
     return(translation == null || translation is EmptyEntityTranslation);
 }
Пример #11
0
        private CustomFieldDefinitionModel LoadFieldTranslation(CustomFieldDefinitionModel original, string prefix, EntityTransaltion translation)
        {
            prefix = prefix + "[" + original.Name + "].";

            var translated = new CustomFieldDefinitionModel
            {
                Id = original.Id,
                Name = original.Name,
                ControlType = original.ControlType
            };

            if (translation != null)
            {
                translated.Label = translation.GetTranslatedText(prefix + "Label");
                translated.DefaultValue = translation.GetTranslatedText(prefix + "DefaultValue");
            }

            foreach (var item in original.SelectionItems)
            {
                var translatedItem = new SelectionItem { Value = item.Value };
                if (translation != null)
                {
                    translatedItem.Text = translation.GetTranslatedText(prefix + "SelectionItems[" + item.Value + "]");
                }
                translated.SelectionItems.Add(item);
            }

            return translated;
        }
Пример #12
0
        private CustomFieldDefinitionModel LoadFieldDiff(CustomFieldDefinitionModel compared, string prefix, EntityTransaltion translation)
        {
            prefix = prefix + "[" + compared.Name + "].";

            var diff = new CustomFieldDefinitionModel
            {
                Id = compared.Id,
                Name = compared.Name,
                ControlType = compared.ControlType
            };

            if (translation != null)
            {
                diff.Label = DiffHelper.GetDiffHtml(translation.GetOriginalText(prefix + "Label"), compared.Label);
                diff.DefaultValue = DiffHelper.GetDiffHtml(translation.GetOriginalText(prefix + "DefaultValue"), compared.DefaultValue);
            }

            foreach (var item in compared.SelectionItems)
            {
                var itemDiff = new SelectionItem { Value = item.Value };
                if (translation != null)
                {
                    itemDiff.Text = DiffHelper.GetDiffHtml(translation.GetOriginalText(prefix + "SelectionItems[" + item.Value + "]"), item.Text);
                }
                diff.SelectionItems.Add(itemDiff);
            }

            return diff;
        }