【问题标题】:Automapper Mapping for Localization Resolver in a Multi-Language Website多语言网站中本地化解析器的自动映射器映射
【发布时间】:2012-10-26 18:14:54
【问题描述】:

我找到了一种使用 Automapper 进行基于活动文化的语言映射的方法。

问题是是否可以构建一个通用解析器来映射所有使用解析器的模型。

在这种情况下,要映射的模型始终具有相同的属性,Id 和 Name(包括语言属性 Name_PT、Name_FR 和 Name_EN):

// 模型

public class MakeDto
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
    public string Name_PT { get; set; }
    public string Name_FR { get; set; }
    public string Name_EN { get; set; }
}

public class MakeViewModel
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ModelDto
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
    public string Name_PT { get; set; }
    public string Name_FR { get; set; }
    public string Name_EN { get; set; }
}

public class ModelViewModel
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
}

public class FuelDto
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
    public string Name_PT { get; set; }
    public string Name_FR { get; set; }
    public string Name_EN { get; set; }
}

public class FuelViewModel
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
}

// 自动映射器配置文件

public class DtoToViewModelMappingProfile : Profile
{
    public override string ProfileName
    {
        get { return "DtoToViewModelMappings"; }
    }

    protected override void Configure()
    {
        CreateMaps();
    }

    private static void CreateMaps()
    {
        Mapper.CreateMap<ModelDto, ModelViewModel>();
        Mapper.CreateMap<MakeDto, MakeViewModel>()
            .ForMember(dest => dest.Name, opt => opt.ResolveUsing<CultureResolver>());
        Mapper.CreateMap<FuelDto, FuelViewModel>();
    }

    public class CultureResolver : ValueResolver<MakeDto, string>
    {
        protected override string ResolveCore(MakeDto makeDto)
        {
            switch(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpperInvariant())
            {
                case "FR":
                    return makeDto.Name_FR;
                case "EN":
                    return makeDto.Name_EN;
            }
            return makeDto.Name_PT;
        }   
    }
}

谢谢。

【问题讨论】:

    标签: asp.net-mvc localization automapper cultureinfo


    【解决方案1】:

    您可以提取如下界面:

    public interface ILocalizable
    {
        string Name { get; set; }
        string Name_PT { get; set; }
        string Name_FR { get; set; }
        string Name_EN { get; set; }
    }
    
    public class FuelDto : ILocalizable
    {
        // Primary properties
        public int Id { get; set; }
        public string Name { get; set; }
        public string Name_PT { get; set; }
        public string Name_FR { get; set; }
        public string Name_EN { get; set; }
    }
    

    然后像下面这样调整你的解析器:

    public class CultureResolver : ValueResolver<ILocalizable, string>
    {
        protected override string ResolveCore(ILocalizable dto)
        {
            switch(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpperInvariant())
            {
                case "FR":
                    return dto.Name_FR;
                case "EN":
                    return dto.Name_EN;
            }
            return dto.Name_PT;
        }   
    }
    

    【讨论】:

    • 非常感谢您的回答;)它已经启动并运行了! :) 问候。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多