コード例 #1
0
        /// <summary>
        /// Add XLocalizer with DB support using customized entity models,
        /// and use defined translation service type
        /// </summary>
        /// <typeparam name="TContext">DbContext</typeparam>
        /// <typeparam name="TTranslator">Translation service</typeparam>
        /// <typeparam name="TResource">Type of localization DbEntity</typeparam>
        /// <param name="options">XLDbOptions</param>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IMvcBuilder AddXDbLocalizer <TContext, TTranslator, TResource>(this IMvcBuilder builder, Action <XLocalizerOptions> options)
            where TContext : DbContext
            where TTranslator : ITranslator
            where TResource : class, IXDbResource, new()
        {
            builder.Services.Configure <XLocalizerOptions>(options);

            // ExpressMemoryCache for caching localized values
            builder.Services.AddSingleton <ExpressMemoryCache>();

            // Try add a default data exporter service, unless another service is defined in startup
            builder.Services.TryAddSingleton <IDbResourceExporter, EFDbResourceExporter <TContext> >();

            // Try add a default resx service.
            // if another service has been registered this will be bypassed, and the other service will be in use.
            builder.Services.TryAddSingleton <IDbResourceProvider, EFDbResourceProvider <TContext> >();

            // Register IStringLocalizer for the default shared resource and translation type
            // This is the default (shared) resource entity and translation
            builder.Services.AddSingleton <IStringLocalizer, DbStringLocalizer <TResource> >();
            builder.Services.AddSingleton <IStringLocalizerFactory, DbStringLocalizerFactory <TResource> >();

            // Register IHtmlLocalizer for the default shared resource and translation type
            // This is the default (shared) resource entity and translation
            builder.Services.AddSingleton <IHtmlLocalizer, DbHtmlLocalizer <TResource> >();
            builder.Services.AddSingleton <IHtmlLocalizerFactory, DbHtmlLocalizerFactory <TResource> >();

            // Register generic IDbStringLocalizer for user defined resource and translation entities
            // e.g. IDbStringLocalizer<ProductArea, ProductAreaTranslation>
            // e.g. IDbStringLocalizer<UserArea, UserAreaTranslation>
            builder.Services.AddTransient(typeof(IStringLocalizer <>), typeof(DbStringLocalizer <>));
            builder.Services.AddTransient(typeof(IHtmlLocalizer <>), typeof(DbHtmlLocalizer <>));

            // Express localizer factories for creating localizers with the default shared resource type
            // Use .Create() method for creating localizers.
            builder.Services.AddSingleton <IXStringLocalizerFactory, DbStringLocalizerFactory <TResource> >();
            builder.Services.AddSingleton <IXHtmlLocalizerFactory, DbHtmlLocalizerFactory <TResource> >();

            // Add custom providers for overriding default modelbinding and data annotations errors
            builder.Services.AddSingleton <IConfigureOptions <MvcOptions>, ConfigureMvcOptions>();

            // Add data annotations locailzation
            builder.AddDataAnnotationsLocalization(ops =>
            {
                // This will look for localization resource of default type T (shared resource)
                ops.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(TResource));
            });

            // Configure route culture provide
            return(builder.AddDbDataManagers <TContext>()
                   .AddIdentityErrorsLocalization()
                   .WithTranslationService <TTranslator>());
        }