コード例 #1
0
        public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
        {
            var theAttributes = context.Attributes;
            var modelMetadata = context.DisplayMetadata;
            var propertyName  = context.Key.Name;
            var containerType = context.Key.ContainerType;

            if (containerType == null)
            {
                return;
            }

            if (containerType.GetCustomAttribute <LocalizedModelAttribute>() == null)
            {
                return;
            }

            var currentMetaData = modelMetadata.DisplayName?.Invoke();

            modelMetadata.DisplayName = () => !ModelMetadataLocalizationHelper.UseLegacyMode(currentMetaData)
                ? ModelMetadataLocalizationHelper.GetTranslation(containerType, propertyName)
                : ModelMetadataLocalizationHelper.GetTranslation(currentMetaData);

            var displayAttribute = theAttributes.OfType <DisplayAttribute>().FirstOrDefault();

            if (displayAttribute?.Description != null)
            {
                modelMetadata.Description = () => ModelMetadataLocalizationHelper.GetTranslation(containerType, $"{propertyName}-Description");
            }
        }
コード例 #2
0
        /// <summary>
        /// Called by ASP.NET Core when we need to translate view model display labels
        /// </summary>
        /// <param name="context"></param>
        public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
        {
            var theAttributes = context.Attributes;
            var modelMetadata = context.DisplayMetadata;
            var propertyName  = context.Key.Name;
            var containerType = context.Key.ContainerType;

            if (containerType == null)
            {
                return;
            }

            var currentMetaData = modelMetadata.DisplayName?.Invoke();

            if (currentMetaData == null)
            {
                return;
            }

            modelMetadata.DisplayName = () => !ConfigurationContext.Current.ResourceLookupFilter(currentMetaData)
                ? ModelMetadataLocalizationHelper.GetTranslation(currentMetaData)
                : ModelMetadataLocalizationHelper.GetTranslation(containerType, propertyName);

            var displayAttribute = theAttributes.OfType <DisplayAttribute>().FirstOrDefault();

            if (displayAttribute?.Description != null)
            {
                modelMetadata.Description = () =>
                                            ModelMetadataLocalizationHelper.GetTranslation(containerType, $"{propertyName}-Description");
            }
        }
コード例 #3
0
 public LocalizedModelValidator(
     ValidationAttribute attribute,
     ResourceKeyBuilder keyBuilder,
     ModelMetadataLocalizationHelper metadataHelper)
 {
     _attribute      = attribute;
     _keyBuilder     = keyBuilder;
     _metadataHelper = metadataHelper;
 }
コード例 #4
0
        public IEnumerable <ModelValidationResult> Validate(ModelValidationContext validationContext)
        {
            if (validationContext == null)
            {
                throw new ArgumentNullException(nameof(validationContext));
            }

            if (validationContext.ModelMetadata == null)
            {
                throw new ArgumentException($"{nameof(validationContext.ModelMetadata)} is null", nameof(validationContext));
            }

            if (validationContext.MetadataProvider == null)
            {
                throw new ArgumentException($"{nameof(validationContext.MetadataProvider)} in null", nameof(validationContext));
            }

            var metadata   = validationContext.ModelMetadata;
            var memberName = metadata.PropertyName;
            var container  = validationContext.Container;

            var context = new ValidationContext(
                container ?? validationContext.Model ?? _emptyValidationContextInstance,
                validationContext.ActionContext?.HttpContext?.RequestServices,
                null)
            {
                DisplayName = metadata.GetDisplayName(),
                MemberName  = memberName
            };

            var result = _attribute.GetValidationResult(validationContext.Model, context);

            if (result != ValidationResult.Success)
            {
                var resourceKey  = ResourceKeyBuilder.BuildResourceKey(metadata.ContainerType, metadata.PropertyName, _attribute);
                var translation  = ModelMetadataLocalizationHelper.GetTranslation(resourceKey);
                var errorMessage = !string.IsNullOrEmpty(translation) ? translation : result.ErrorMessage;

                var validationResults = new List <ModelValidationResult>();
                if (result.MemberNames != null)
                {
                    foreach (var resultMemberName in result.MemberNames)
                    {
                        // ModelValidationResult.MemberName is used by invoking validators (such as ModelValidator) to
                        // append construct the ModelKey for ModelStateDictionary. When validating at type level we
                        // want the returned MemberNames if specified (e.g. "person.Address.FirstName"). For property
                        // validation, the ModelKey can be constructed using the ModelMetadata and we should ignore
                        // MemberName (we don't want "person.Name.Name"). However the invoking validator does not have
                        // a way to distinguish between these two cases. Consequently we'll only set MemberName if this
                        // validation returns a MemberName that is different from the property being validated.
                        var newMemberName = string.Equals(resultMemberName, memberName, StringComparison.Ordinal)
                            ? null
                            : resultMemberName;
                        var validationResult = new ModelValidationResult(newMemberName, errorMessage);

                        validationResults.Add(validationResult);
                    }
                }

                if (validationResults.Count == 0)
                {
                    validationResults.Add(new ModelValidationResult(null, errorMessage));
                }

                return(validationResults);
            }

            return(Enumerable.Empty <ModelValidationResult>());
        }
 /// <summary>
 /// Initiates new instance of this helper.
 /// </summary>
 /// <param name="metadataHelper">Metadata helper.</param>
 /// <param name="configurationContext">Configuration settings.</param>
 public LocalizedDisplayMetadataProvider(ModelMetadataLocalizationHelper metadataHelper, ConfigurationContext configurationContext)
 {
     _metadataHelper       = metadataHelper;
     _configurationContext = configurationContext;
 }