내가 지금까지 찾은 깨끗한 솔루션입니다 : http://www.codeproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC.
의견/피드백은 환영합니다.
편집 1 : 의견에 따라 코드 예제를 추가하고 링크를 참조로 사용했습니다.
은 내가 customDataAnnotationsProvider 클래스 생성 : 내가 Global.asax에
에 ApplicationStart에 사용자 지정 공급자를 참조 그런
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);
}
}
을
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