ASP.NET MVC6可本地化的DisplayAttribute [英] ASP.NET MVC6 localizable DisplayAttribute

查看:116
本文介绍了ASP.NET MVC6可本地化的DisplayAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能直接将ASP.NET MVC6中的IHtmlLocalizer与POCO类一起使用?当前,我很少有使用DisplayAttribute的viewmodels在视图和验证器中显示转换后的字符串,但是它需要使用定义的每个static属性创建其他静态类(不幸的是,C#中无法使用静态索引器).有没有更好的方法可以做到这一点?

I wonder if there is possibility to use IHtmlLocalizer from ASP.NET MVC6 directly with POCO classes? Currently I have few viewmodels that uses DisplayAttribute in order to display translated string in views and validator, but it requires to create additional static class with each static property defined (unfortunately the static indexers are not possible in C#). Is there any better way to get this done?

我当前的代码:

[Display(Name = "TrackingDevice", ResourceType = typeof(TestResource))]
public string TrackingDevice { get; set; }

public class TestResource
{
    public static string TrackingDevice
    {
        get
        {
            //Here I call IHtmlLocalizer via IServiceLocator
            return "Field name";
        }
    }
}

推荐答案

我有些挣扎,终于成功地为这个问题找到了可行的解决方案.感谢@Szymon Sasin的回答,尽管它不适用于最新版本,并且其配置是局部的,但它帮助我构建了此解决方案.

I have struggled a bit and finally succeeded in compiling a working solution to this question. Thanks to @Szymon Sasin for his answer, although it is not working against the latest version and his configuration is partial, it helped me build this solution.

首先,在Startup.cs中配置本地化:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        //...
        services.AddLocalization(options => options.ResourcesPath = "Resources");
        services
            .AddMvc(mvcOptions =>
            {
                IServiceProvider provider = services.BuildServiceProvider();
                IStringLocalizer localizer = provider.GetService<IStringLocalizer<DisplayResources>>();
                mvcOptions.ModelMetadataDetailsProviders.Add(new DisplayAttributeLocalizationProvider(localizer));
            });
        //...
    }
}

第二,根据配置的ResourcePath验证文件夹结构.这里重要的是自定义资源类型的路径及其resx文件的路径应该是相对的.示例:

<root_proj_dir>/Resources/Resources_Common/DisplayResources.en.resx
<root_proj_dir>/Resources/Resources_Common/DisplayResources.bg.resx
<root_proj_dir>/Resources_Common/DisplayResources.cs

第三,定义您的自定义元数据提供程序:

public sealed class DisplayAttributeLocalizationProvider : IDisplayMetadataProvider
{
    private IStringLocalizer _localizer;

    public DisplayAttributeLocalizationProvider(IStringLocalizer localizer)
    {
        _localizer = localizer;
    }

    public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
    {
        context.PropertyAttributes?
            .Where(attribute => attribute is DisplayAttribute)
            .Cast<DisplayAttribute>().ToList().ForEach(display =>
            {
                display.Name = _localizer[display.Name].Value;
            });
    }
}

第四,像下面这样在视图模型中使用所有这些内容:

public class SomeViewModel
{
    [Display(Name = "Email")]
    public string Email { get; set; }
}

电子邮件"值将成为在DisplayResources.xx.resx文件中查找的关键.

The "Email" value will be the key to look-up for in the DisplayResources.xx.resx files.

希望其他许多人会发现此信息有帮助!

Hope that many others will find this info helpful!

这篇关于ASP.NET MVC6可本地化的DisplayAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆