ResourceManager 的 MVC 本地化问题

发布于 2024-10-13 12:13:00 字数 2561 浏览 3 评论 0原文

本地化有很多解决方案。 我已经决定了: http ://geekswithblogs.net/brians/archive/2010/06/14/asp.net-mvc-localization-displaynameattribute-alternatives-a-better-way.aspx

public class LocalizedDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
        {
            protected override ModelMetadata CreateMetadata(
                IEnumerable<Attribute> attributes,
                Type containerType,
                Func<object> modelAccessor,
                Type modelType,
                string propertyName)
            {
                var meta = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
                if (string.IsNullOrEmpty(propertyName))
                    return meta;
                if (meta.DisplayName != null)
                    GetLocalizedDisplayName(meta, propertyName);
                if (string.IsNullOrEmpty(meta.DisplayName))
                    meta.DisplayName = string.Format("[[{0}]]", propertyName);
                return meta;
            }
            private static void GetLocalizedDisplayName(ModelMetadata meta, string propertyName)
            {
                ResourceManager resourceManager = App_GlobalResources.Strings.ResourceManager;
                CultureInfo culture = Thread.CurrentThread.CurrentUICulture;
                meta.DisplayName = resourceManager.GetString(propertyName, culture);
            }
        }

我已将行:更改

if (meta.DisplayName == null)

为:

if (meta.DisplayName != null)

输入GetLocalizedDisplayName函数

在App_GlobalResources中有2个文件: Strings.resxStrings.pl.resx。 他们两个都有公共访问修饰符,并且构建操作设置为嵌入式资源

整个网站已翻译,但我对属性有问题

[Required]
[LocalizedDisplayName("UserName", NameResourceType = typeof(App_GlobalResources.Strings))]
public string UserName { get; set; }

我认为问题位于我上面发布的链接中的这一行:

meta.DisplayName = resourceManager.GetString(propertyName, culture);

GetString 始终从 Strings.resx 返回默认值。

cultureplpropertyName 是正确的 UserName,因此返回值应该来自字符串。pl< /strong>.resx,不是来自 Strings.resx。

请帮我 :)

There is many solutions for localization.
I've decided for this one:
http://geekswithblogs.net/brians/archive/2010/06/14/asp.net-mvc-localization-displaynameattribute-alternatives-a-better-way.aspx

public class LocalizedDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
        {
            protected override ModelMetadata CreateMetadata(
                IEnumerable<Attribute> attributes,
                Type containerType,
                Func<object> modelAccessor,
                Type modelType,
                string propertyName)
            {
                var meta = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
                if (string.IsNullOrEmpty(propertyName))
                    return meta;
                if (meta.DisplayName != null)
                    GetLocalizedDisplayName(meta, propertyName);
                if (string.IsNullOrEmpty(meta.DisplayName))
                    meta.DisplayName = string.Format("[[{0}]]", propertyName);
                return meta;
            }
            private static void GetLocalizedDisplayName(ModelMetadata meta, string propertyName)
            {
                ResourceManager resourceManager = App_GlobalResources.Strings.ResourceManager;
                CultureInfo culture = Thread.CurrentThread.CurrentUICulture;
                meta.DisplayName = resourceManager.GetString(propertyName, culture);
            }
        }

I've changed line:

if (meta.DisplayName == null)

into:

if (meta.DisplayName != null)

to enter GetLocalizedDisplayName function

In App_GlobalResources there are 2 files:
Strings.resx and Strings.pl.resx.
Both of them ahave Public access modifier and build action is set to Embedded Resource

Whole site is translated but i have problem with Attributes

[Required]
[LocalizedDisplayName("UserName", NameResourceType = typeof(App_GlobalResources.Strings))]
public string UserName { get; set; }

I think the problem is in this line from the link that i posted above:

meta.DisplayName = resourceManager.GetString(propertyName, culture);

GetString always return default value from Strings.resx.

culture is pl and propertyName is correct UserName, so returned value should be from Strings.pl.resx, not from Strings.resx.

Please help me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2024-10-20 12:13:00

尝试将构建操作设置为内容。

try making build action set to Content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文