ResourceManager 的 MVC 本地化问题
本地化有很多解决方案。 我已经决定了: 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.resx 和Strings.pl.resx。 他们两个都有公共访问修饰符,并且构建操作设置为嵌入式资源
整个网站已翻译,但我对属性有问题
[Required]
[LocalizedDisplayName("UserName", NameResourceType = typeof(App_GlobalResources.Strings))]
public string UserName { get; set; }
我认为问题位于我上面发布的链接中的这一行:
meta.DisplayName = resourceManager.GetString(propertyName, culture);
GetString 始终从 Strings.resx 返回默认值。
culture 是 pl,propertyName 是正确的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将构建操作设置为内容。
try making build action set to Content.