You've already forked linux-packaging-mono
Imported Upstream version 4.8.0.309
Former-commit-id: 5f9c6ae75f295e057a7d2971f3a6df4656fa8850
This commit is contained in:
parent
ee1447783b
commit
94b2861243
@ -2,8 +2,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Threading;
|
||||
using System.Web.Globalization;
|
||||
|
||||
public class DataAnnotationsModelValidator : ModelValidator {
|
||||
public class DataAnnotationsModelValidator : ModelValidator {
|
||||
public DataAnnotationsModelValidator(ModelMetadata metadata, ModelBindingExecutionContext context, ValidationAttribute attribute)
|
||||
: base(metadata, context) {
|
||||
|
||||
@ -18,7 +20,24 @@
|
||||
|
||||
protected internal string ErrorMessage {
|
||||
get {
|
||||
return Attribute.FormatErrorMessage(Metadata.GetDisplayName());
|
||||
if (UseStringLocalizerProvider) {
|
||||
var errorMsg = GetLocalizedString(Attribute.ErrorMessage);
|
||||
|
||||
return errorMsg ?? Attribute.FormatErrorMessage(Metadata.GetDisplayName());
|
||||
}
|
||||
else {
|
||||
return Attribute.FormatErrorMessage(Metadata.GetDisplayName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected string GetLocalizedString(string name, params object[] arguments) {
|
||||
if (StringLocalizerProviders.DataAnnotationStringLocalizerProvider != null) {
|
||||
return StringLocalizerProviders.DataAnnotationStringLocalizerProvider
|
||||
.GetLocalizedString(Thread.CurrentThread.CurrentUICulture, name, arguments);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,9 +74,37 @@
|
||||
ValidationResult result = Attribute.GetValidationResult(Metadata.Model, context);
|
||||
if (result != ValidationResult.Success) {
|
||||
yield return new ModelValidationResult {
|
||||
Message = result.ErrorMessage
|
||||
Message = GetValidationErrorMessage(result)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual string GetLocalizedErrorMessage(string errorMessage) {
|
||||
return GetLocalizedString(errorMessage, Metadata.GetDisplayName());
|
||||
}
|
||||
|
||||
private string GetValidationErrorMessage(ValidationResult result) {
|
||||
string errorMsg;
|
||||
|
||||
if (UseStringLocalizerProvider) {
|
||||
errorMsg = GetLocalizedErrorMessage(Attribute.ErrorMessage);
|
||||
|
||||
errorMsg = errorMsg ?? result.ErrorMessage;
|
||||
}
|
||||
else {
|
||||
errorMsg = result.ErrorMessage;
|
||||
}
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
private bool UseStringLocalizerProvider {
|
||||
get {
|
||||
// if developer already uses existing localization feature,
|
||||
// then we don't opt in the new localization feature.
|
||||
return (!string.IsNullOrEmpty(Attribute.ErrorMessage) &&
|
||||
string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) &&
|
||||
Attribute.ErrorMessageResourceType == null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user