/// <summary>
        ///
        /// </summary>
        /// <param name="attr"></param>
        /// <param name="containerType"> </param>
        /// <param name="propertyName"> </param>
        /// <param name="defaultResource"> </param>
        public static void Transform([NotNull] ValidationAttribute attr, Type containerType, string propertyName, Type defaultResource)
        {
            if (!LocalizationConventions.Enabled ||
                (!string.IsNullOrEmpty(attr.ErrorMessage) ||
                 (attr.ErrorMessageResourceType != null && !string.IsNullOrEmpty(attr.ErrorMessageResourceName))))
            {
                return;
            }

            var resourceType = attr.ErrorMessageResourceType ?? defaultResource;

            // do not apply convensions if no resource type found
            if (resourceType == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(attr.ErrorMessageResourceName) && ResourceUtil.HasResourceValue(resourceType, attr.ErrorMessageResourceName))
            {
                attr.ErrorMessageResourceType = resourceType;
            }
            else
            {
                var    attributeName = attr.GetType().Name.Replace("Attribute", String.Empty);
                string resourceKey   = null;
                var    resouceFound  = false;
                if (containerType != null && propertyName != null)
                {
                    resourceKey  = string.Format("{0}_{1}", ResourceUtil.GetResourceKey(containerType, propertyName), attributeName);
                    resouceFound = ResourceUtil.HasResourceValue(resourceType, resourceKey);
                }

                if (!resouceFound)
                {
                    resourceKey  = string.Format("Validation_{0}", attributeName);
                    resouceFound = ResourceUtil.HasResourceValue(resourceType, resourceKey);
                }

                if (resouceFound)
                {
                    attr.ErrorMessageResourceType = resourceType;
                    attr.ErrorMessageResourceName = resourceKey;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Tranform <see cref="ModelMetadata"/>
        /// </summary>
        /// <param name="metadata"></param>
        public static void Transform([NotNull] ModelMetadata metadata)
        {
            Invariant.IsNotNull(metadata, "metadata");

            var containerType = metadata.ContainerType;

            if (!LocalizationConventions.Enabled || containerType == null || string.IsNullOrEmpty(metadata.PropertyName))
            {
                return;
            }

            // flent configuration does not have ResourceType, so get it from type
            var resourceType = LocalizationConventions.GetDefaultResourceType(containerType);
            var propertyName = metadata.PropertyName;

            if (resourceType != null && !string.IsNullOrEmpty(propertyName))
            {
                var key = ResourceUtil.GetResourceKey(containerType, propertyName);
                if (metadata.DisplayName == null)
                {
                    metadata.DisplayName = RetrieveValue(resourceType, key, propertyName);
                }

                if (metadata.ShortDisplayName == null)
                {
                    metadata.ShortDisplayName = RetrieveValue(resourceType, key + ShortDisplayNameSuffix, propertyName + ShortDisplayNameSuffix);
                }

                if (metadata.Watermark == null)
                {
                    metadata.Watermark = RetrieveValue(resourceType, key + PromptSuffix, propertyName + PromptSuffix);
                }

                if (metadata.Description == null)
                {
                    metadata.Description = RetrieveValue(resourceType, key + DescriptionSuffix, propertyName + DescriptionSuffix);
                }
            }
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="attr"></param>
        /// <param name="containerType"></param>
        /// <param name="propertyName"></param>
        /// <param name="defaultResourceType"></param>
        public static void Transform([NotNull] DisplayAttribute attr, [NotNull] Type containerType, string propertyName, Type defaultResourceType)
        {
            Invariant.IsNotNull(attr, "displayAttribute");

            var resourceType = attr.ResourceType ?? defaultResourceType;

            if (resourceType == null)
            {
                return;
            }

            // reset resource and manually set values
            attr.ResourceType = null;
            var resourceKey = ResourceUtil.GetResourceKey(containerType, propertyName);

            // retrieve values
            attr.Name        = GetValue(resourceType, attr.Name, resourceKey, propertyName);
            attr.Description = GetValue(resourceType, attr.Description, resourceKey + DescriptionSuffix, propertyName + DescriptionSuffix);
            attr.ShortName   = GetValue(resourceType, attr.ShortName, resourceKey + ShortDisplayNameSuffix, propertyName + ShortDisplayNameSuffix);
            attr.Prompt      = GetValue(resourceType, attr.Prompt, resourceKey + PromptSuffix, propertyName + PromptSuffix);
            attr.GroupName   = GetValue(resourceType, attr.GroupName, null, null); // leave original behaviour
        }