3
有许多本地化解决方案。 我已经决定为这一个: http://geekswithblogs.net/brians/archive/2010/06/14/asp.net-mvc-localization-displaynameattribute-alternatives-a-better-way.aspxMVC本地化ResourceManager问题
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 and Strings.pl.resx。 他们都ahave 公共访问修饰符和构建操作设置为嵌入的资源
全站翻译,但我有问题,属性
[Required]
[LocalizedDisplayName("UserName", NameResourceType = typeof(App_GlobalResources.Strings))]
public string UserName { get; set; }
我认为这个问题是在这
meta.DisplayName = resourceManager.GetString(propertyName, culture);
的GetString ALW:从我张贴上面的连结线ays返回默认值Strings.resx。
文化是PL和propertyName的是正确的用户名,所以返回的值应为字符串。 pl .resx,不是来自Strings.resx。
请帮我:)
然后,我有错误服务器无法找到特定的文化或区域性 源文件资源:{MyProject的} \ App_GlobalResources文件\ Strings.Designer.cs线:365 行363:公共静态字符串用户名{ Line 364:get { Line 365:return ResourceManager.GetString(“UserName”,resourceCulture); Line 366:} Line 367:} – 2011-01-25 10:44:29