授予這個你應該已經有了一定的資源管理器,可以返回本地化字符串鍵文本。我的ResourceManager
具有靜態訪問器(並且註冊了Unity for DI),所以不需要傳遞它。 在global.asax
:
ModelMetadataProviders.Current = new LocalizedModelMetadataProvider(); //field name localization
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider()); //validation message localization
和實現是這樣的:
public class LocalizedModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType,
Func<object> modelAccessor, Type modelType, string propertyName)
{
var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
if (containerType == null || propertyName == null)
return metadata;
if (metadata.DisplayName == null)
metadata.DisplayName = ResourceManager.Current.GetLocalizedAttribute(containerType, propertyName);
if (metadata.Watermark == null)
metadata.Watermark = ResourceManager.Current.GetLocalizedAttribute(containerType, propertyName, "Watermark");
if (metadata.Description == null)
metadata.Description = ResourceManager.Current.GetLocalizedAttribute(containerType, propertyName, "Description");
if (metadata.NullDisplayText == null)
metadata.NullDisplayText = ResourceManager.Current.GetLocalizedAttribute(containerType, propertyName, "NullDisplayText");
if (metadata.ShortDisplayName == null)
metadata.ShortDisplayName = ResourceManager.Current.GetLocalizedAttribute(containerType, propertyName, "ShortDisplayName");
return metadata;
}
}
public class LocalizedModelValidatorProvider : DataAnnotationsModelValidatorProvider
{
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
foreach (var attribute in attributes.OfType<ValidationAttribute>())
attribute.ErrorMessage = ResourceManager.Current.GetValidationMessage(attribute);
return base.GetValidators(metadata, context, attributes);
}
}
感謝,會嘗試看看 – Shaddix