Пример #1
0
        public static CreateMultiLingualMapResult <TMultiLingualEntity, TTranslation, TDestination> CreateMultiLingualMap <TMultiLingualEntity, TMultiLingualEntityPrimaryKey, TTranslation, TDestination>(
            this IMapperConfigurationExpression configuration, MultiLingualMapContext multiLingualMapContext, bool fallbackToParentCultures = false)
            where TTranslation : class, IEntityTranslation <TMultiLingualEntity, TMultiLingualEntityPrimaryKey>
            where TMultiLingualEntity : IMultiLingualEntity <TTranslation>
        {
            var result = new CreateMultiLingualMapResult <TMultiLingualEntity, TTranslation, TDestination>();

            result.TranslationMap = configuration.CreateMap <TTranslation, TDestination>();
            result.EntityMap      = configuration.CreateMap <TMultiLingualEntity, TDestination>().BeforeMap((source, destination, context) =>
            {
                if (source.Translations.IsNullOrEmpty())
                {
                    return;
                }

                var translation = source.Translations.FirstOrDefault(pt => pt.Language == CultureInfo.CurrentUICulture.Name);
                if (translation != null)
                {
                    context.Mapper.Map(translation, destination);
                    return;
                }

                if (fallbackToParentCultures)
                {
                    translation = GeTranslationBasedOnCulturalRecursive <TMultiLingualEntity, TMultiLingualEntityPrimaryKey, TTranslation>(CultureInfo.CurrentUICulture.Parent, source.Translations, 0);
                    if (translation != null)
                    {
                        context.Mapper.Map(translation, destination);
                        return;
                    }
                }

                var defaultLanguage = multiLingualMapContext.SettingManager.GetSettingValue(LocalizationSettingNames.DefaultLanguage);

                translation = source.Translations.FirstOrDefault(pt => pt.Language == defaultLanguage);
                if (translation != null)
                {
                    context.Mapper.Map(translation, destination);
                    return;
                }

                translation = source.Translations.FirstOrDefault();
                if (translation != null)
                {
                    context.Mapper.Map(translation, destination);
                }
            });

            return(result);
        }
        public static CreateMultiLingualMapResult <TMultiLingualEntity, TTranslation, TDestination> CreateMultiLingualMap <TMultiLingualEntity, TMultiLingualEntityPrimaryKey, TTranslation, TDestination>(
            this IMapperConfigurationExpression configuration, MultiLingualMapContext multiLingualMapContext)
            where TTranslation : class, IEntityTranslation <TMultiLingualEntity, TMultiLingualEntityPrimaryKey>
            where TMultiLingualEntity : IMultiLingualEntity <TTranslation>
        {
            var result = new CreateMultiLingualMapResult <TMultiLingualEntity, TTranslation, TDestination>();

            result.TranslationMap = configuration.CreateMap <TTranslation, TDestination>();
            result.EntityMap      = configuration.CreateMap <TMultiLingualEntity, TDestination>().BeforeMap((source, destination, context) =>
            {
                var translation = source.Translations.FirstOrDefault(pt => pt.Language == CultureInfo.CurrentUICulture.Name);
                if (translation != null)
                {
                    context.Mapper.Map(translation, destination);
                    return;
                }

                var defaultLanguage = multiLingualMapContext.SettingManager
                                      .GetSettingValue(LocalizationSettingNames.DefaultLanguage);

                translation = source.Translations.FirstOrDefault(pt => pt.Language == defaultLanguage);
                if (translation != null)
                {
                    context.Mapper.Map(translation, destination);
                    return;
                }

                translation = source.Translations.FirstOrDefault();
                if (translation != null)
                {
                    context.Mapper.Map(translation, destination);
                }
            });

            return(result);
        }