到目前爲止,我已經找到了乾淨的解決方案是:http://www.codeproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC。
歡迎評論/反饋。
編輯1:根據評論,我添加了代碼示例並將鏈接用作參考。
我創建了一個customDataAnnotationsProvider類:
public class CustomDataAnnotationsProvider: DataAnnotationsModelMetadataProvider
{
private ResourceManager resourceManager = new ResourceManager();
protected override ModelMetadata CreateMetadata(
IEnumerable<Attribute> attributes,
Type containerType,
Func<object> modelAccessor,
Type modelType,
string propertyName)
{
string key = string.Empty;
string localizedValue = string.Empty;
foreach (var attr in attributes)
{
if (attr != null)
{
if (attr is DisplayAttribute)
{
key = ((DisplayAttribute)attr).Name;
if (!string.IsNullOrEmpty(key))
{
localizedValue = resourceManager.GetLocalizedText(key);
((DisplayAttribute)attr).Name = localizedValue;
}
}
else if (attr is ValidationAttribute)
{
key = ((ValidationAttribute)attr).ErrorMessage;
if (!string.IsNullOrEmpty(key))
{
localizedValue = resourceManager.GetLocalizedText(key);
((ValidationAttribute)attr).ErrorMessage = localizedValue;
}
}
}
}
return base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
}
}
然後,我在Global.asax中
引用上ApplicationStart自定義提供
ModelMetadataProviders.Current = new Project.Web.Helpers.CustomDataAnnotationsProvider();
你不必改變你的模型,並可以使用顯示註解:
[Display(Name = "CustomerAccountNumber")]
public string CustomerAccountNumber { get; set; }
[ASP.NET MVC 2 Localization/Globalizatio n存儲在數據庫中嗎?](http://stackoverflow.com/questions/2568129/asp-net-mvc-2-localization-globalization-stored-in-the-database) – jrummell 2011-12-19 14:50:05