Пример #1
0
        public static void CreateMultiLingualMap <TMultiLingualEntity, TMultiLingualEntityPrimaryKey, TTranslation, TDestination>(
            this IMapperConfigurationExpression configuration, MultiLingualMapContext multiLingualMapContext)
            where TTranslation : class, IEntityTranslation <TMultiLingualEntity, TMultiLingualEntityPrimaryKey>
            where TMultiLingualEntity : IMultiLingualEntity <TTranslation>
        {
            configuration.CreateMap <TTranslation, TDestination>();
            configuration.CreateMap <TMultiLingualEntity, TDestination>().AfterMap((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);
                }
            });
        }
Пример #2
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);
        }
Пример #3
0
 public static void CreateMultiLingualMap <TMultiLingualEntity, TTranslation, TDestination>(this IMapperConfigurationExpression configuration, MultiLingualMapContext multiLingualMapContext)
     where TTranslation : class, IEntity, IEntityTranslation <TMultiLingualEntity, int>
     where TMultiLingualEntity : IMultiLingualEntity <TTranslation>
 {
     configuration.CreateMultiLingualMap <TMultiLingualEntity, int, TTranslation, TDestination>(multiLingualMapContext);
 }
Пример #4
0
 public static CreateMultiLingualMapResult <TMultiLingualEntity, TTranslation, TDestination> CreateMultiLingualMap <TMultiLingualEntity, TTranslation, TDestination>(this IMapperConfigurationExpression configuration,
                                                                                                                                                                     MultiLingualMapContext multiLingualMapContext,
                                                                                                                                                                     bool fallbackToParentCultures = false)
     where TTranslation : class, IEntity, IEntityTranslation <TMultiLingualEntity, int>
     where TMultiLingualEntity : IMultiLingualEntity <TTranslation>
 {
     return(configuration.CreateMultiLingualMap <TMultiLingualEntity, int, TTranslation, TDestination>(multiLingualMapContext, fallbackToParentCultures));
 }