如何在Attribute ASP.NET MVC中获得应用程序的UI文化

时间:2015-12-11 12:12:29

标签: c# asp.net-mvc

我有这个资源可本地化的属性:

    public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(string resourceId)
        : base(GetMessageFromResource(resourceId))
    {

    }

    private static string GetMessageFromResource(string resourceId)
    {
        var ioc = Abp.Dependency.IocManager.Instance;
        var localizationManager = ioc.IocContainer.Resolve<ILocalizationManager>();


        return localizationManager.GetString(MMMConsts.LocalizationSourceName, resourceId, Thread.CurrentThread.CurrentUICulture);}

当我在WEB应用程序中切换语言并进行调试时,在Thread.CurrentThread.CurrentUICulture中我仍然是相同的值。

但是当Razor视图文化中的这种情况发生了正确的变化时。

如何在我的属性类中设置currentUp currencu culture,我需要它来从资源加载多语言文本。

模型属性在app启动之前初始化?谢谢你的帮助!

此代码解析:

        public LocalizedDisplayNameAttribute(string resourceKey)
        : base(resourceKey)
    {
        ResourceKey = resourceKey;
    }

    public string ResourceKey { get; set; }

    public override string DisplayName
    {
        get
        {
            string value = null;
            var ioc = Abp.Dependency.IocManager.Instance;
            var localizationManager = ioc.IocContainer.Resolve<ILocalizationManager>();
            value = localizationManager.GetString(MMConsts.LocalizationSourceName, ResourceKey, Thread.CurrentThread.CurrentUICulture);

            return value;
        }
    }

1 个答案:

答案 0 :(得分:0)

此代码解析:

  public LocalizedDisplayNameAttribute(string resourceKey)
        : base(resourceKey)
    {
    ResourceKey = resourceKey;
}

public string ResourceKey { get; set; }

public override string DisplayName
{
    get
    {
        string value = null;
        var ioc = Abp.Dependency.IocManager.Instance;
        var localizationManager = ioc.IocContainer.Resolve<ILocalizationManager>();
        value = localizationManager.GetString(MMConsts.LocalizationSourceName, ResourceKey, Thread.CurrentThread.CurrentUICulture);

        return value;
    }
}