Esempio n. 1
0
        /// <summary>
        /// Gets a translated error message for the given controller action.
        /// </summary>
        /// <param name="controllerAction">The controller action that originated the error.</param>
        /// <param name="errorCode">The code of the error to translate.</param>
        /// <returns>A localized string.</returns>
        public LocalizedString GetLocalizedErrorMessage(ControllerActionDescriptor controllerAction, string errorCode)
        {
            LocalizedString errorMessage;
            // build a key for the resource.
            ResourceKey resourceKey = _resourceMapper.GetKey(controllerAction.ControllerTypeInfo.Namespace,
                                                             controllerAction.ControllerTypeInfo.Name,
                                                             controllerAction.ActionName,
                                                             errorCode);

            if (resourceKey != null)
            {
                // if the key was built, proceed to translation.
                IStringLocalizer stringLocalizer = _stringLocalizerFactory.Create(resourceKey.ResourceName, resourceKey.ResourceLocation);
                errorMessage = stringLocalizer.GetString(resourceKey.KeyName, _resourceMapper.GetSupportInfoArguments());
                if (errorMessage.ResourceNotFound)
                {
                    errorMessage = GetFallbackErrorMessage(errorCode);
                }
            }
            else
            {
                // the key was not built, get the default error for the message.
                errorMessage = GetFallbackErrorMessage(errorCode);
            }

            return(errorMessage);
        }
        public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
        {
            switch (context.Key.MetadataKind)
            {
            case ModelMetadataKind.Property:
                if (context.DisplayMetadata.DisplayName == null)
                {
                    ResourceKey displayKey = _resourceMapper.GetKey(context.Key.ContainerType.Namespace, context.Key.ContainerType.Name, context.Key.Name, "DisplayName");
                    if (displayKey != null)
                    {
                        IStringLocalizer localizer = _stringLocalizerFactory.Create(displayKey.ResourceName, displayKey.ResourceLocation);
                        context.DisplayMetadata.DisplayName = () => localizer.GetString(displayKey.KeyName);
                    }
                }
                break;

            case ModelMetadataKind.Type:
                break;
            }
        }
        public void CreateValidators(ModelValidatorProviderContext context)
        {
            for (var i = 0; i < context.Results.Count; i++)
            {
                var validatorItem = context.Results[i];
                if (validatorItem.Validator != null)
                {
                    continue;
                }

                var attribute = validatorItem.ValidatorMetadata as ValidationAttribute;
                if (attribute == null)
                {
                    continue;
                }

                IStringLocalizer stringLocalizer = null;
                Type             modelType       = context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType;
                string           validatorName   = attribute.GetType().Name.Replace("Attribute", "");
                ResourceKey      key             = _resourceMapper.GetKey(modelType.Namespace,
                                                                          modelType.Name,
                                                                          context.ModelMetadata.PropertyName,
                                                                          validatorName);
                if (key != null)
                {
                    stringLocalizer = _stringLocalizerFactory.Create(key.ResourceName, key.ResourceLocation);
                    if (attribute.ErrorMessage == null || attribute is DataTypeAttribute)
                    {
                        attribute.ErrorMessage = key.KeyName;
                    }
                }

                ResourceKey alternateKey = _resourceMapper.GetFallbackKey("Validation", validatorName, nameof(ValidationAttribute.ErrorMessage));
                if (alternateKey != null)
                {
                    IStringLocalizer alternateStringLocalizer = _stringLocalizerFactory.Create(alternateKey.ResourceName, alternateKey.ResourceLocation);
                    stringLocalizer = new ValidatorStringLocalizerAdapter(new[] { stringLocalizer, alternateStringLocalizer }, new[] { key.KeyName, alternateKey.KeyName });
                }

                if (stringLocalizer == null)
                {
                    stringLocalizer = _stringLocalizerFactory.Create(context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType);
                }

                var validator = new DataAnnotationsModelValidator(_validationAttributeAdapterProvider, attribute, stringLocalizer);
                validatorItem.Validator  = validator;
                validatorItem.IsReusable = true;

                if (attribute is RequiredAttribute)
                {
                    context.Results.Remove(validatorItem);
                    context.Results.Insert(0, validatorItem);
                }
            }

            if (typeof(IValidatableObject).IsAssignableFrom(context.ModelMetadata.ModelType))
            {
                context.Results.Add(new ValidatorItem
                {
                    Validator  = new ValidatableObjectAdapter(),
                    IsReusable = true
                });
            }
        }