多语言网站中本地化解析器的自动映射

时间:2012-10-26 18:14:54

标签: asp.net-mvc localization automapper cultureinfo

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

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

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

// MODELS

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; }
}

// AUTOMAPPER PROFILE

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;
        }   
    }
}

感谢。

1 个答案:

答案 0 :(得分:2)

您可以提取如下界面:

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;
    }   
}