Example #1
0
        /// <summary>
        /// Uses Data-Annotation Translations with custom settings
        /// </summary>
        /// <param name="services">the service-collection where ot inject the Attribute-Translation</param>
        /// <param name="options">a callback that can be used to modify the default settings</param>
        /// <returns>the generated Translation options for the current application</returns>
        public static AttributeTranslationOptions ConfigureAttributeTranslation(this IServiceCollection services, Action <AttributeTranslationOptions> options)
        {
            var o = new AttributeTranslationOptions();

            o.MapResource("ITV", typeof(DefaultModelMessages));
            o.MapAttribute(typeof(CustomValidationAttribute), "ITV");
            o.MapAttribute(typeof(MaxLengthAttribute), "ITV");
            o.MapAttribute(typeof(MinLengthAttribute), "ITV");
            o.MapAttribute(typeof(RangeAttribute), "ITV");
            o.MapAttribute(typeof(RegularExpressionAttribute), "ITV");
            o.MapAttribute(typeof(StringLengthAttribute), "ITV");
            o.MapAttribute(typeof(RequiredAttribute), "ITV");
            o.AddTopicCallback(typeof(StringLengthAttribute), (attribute, s) =>
            {
                var sla = (StringLengthAttribute)attribute;
                if (sla.MinimumLength != 0 && s == "ValidationError")
                {
                    return("ValidationErrorIncludingMinimum");
                }

                return(s);
            });
            options(o);
            services.AddSingleton(o);
            return(o);
        }
Example #2
0
 public FallbackLocalizer(IStringLocalizer primary, IStringLocalizerFactory factory, AttributeTranslationOptions options)
 {
     this.primary = primary;
     this.factory = factory;
     this.options = options;
 }
Example #3
0
 public static IMvcBuilder UseToolkitAttributeTranslator(this IMvcBuilder builder, AttributeTranslationOptions options)
 {
     return(builder.AddDataAnnotationsLocalization(opt =>
     {
         opt.DataAnnotationLocalizerProvider = (type, factory) =>
         {
             var retVal = new FallbackLocalizer(factory.Create(type), factory, options);
             return retVal;
         };
     }));
 }
 public ModelMetaOverrider(AttributeTranslationOptions options)
 {
     this.options = options;
 }
 public static void ConfigureLocalizationMetaProvider(this MvcOptions options, AttributeTranslationOptions translationOptions)
 {
     options.ModelMetadataDetailsProviders.Add(new ModelMetaOverrider(translationOptions));
 }