Context to configure various localization provider features and behavior
コード例 #1
0
 /// <summary>
 /// Creates new instance.
 /// </summary>
 /// <param name="context">Configuration settings.</param>
 public QueryExecutor(ConfigurationContext context)
 {
     _context = context;
 }
コード例 #2
0
 /// <summary>
 ///Creates new instance of the class.
 /// </summary>
 /// <param name="configurationContext">Configuration settings.</param>
 public CommandExecutor(ConfigurationContext configurationContext)
 {
     _configurationContext = configurationContext;
 }
コード例 #3
0
        public static IAppBuilder UseDbLocalizationProvider(this IAppBuilder builder, Action <ConfigurationContext> setup = null)
        {
            // setup default implementations
            ConfigurationContext.Current.TypeFactory.ForQuery <AvailableLanguages.Query>().SetHandler <AvailableLanguages.Handler>();
            ConfigurationContext.Current.TypeFactory.ForQuery <GetTranslation.Query>().SetHandler <GetTranslation.Handler>();

            ConfigurationContext.Current.TypeFactory.ForQuery <GetAllResources.Query>().SetHandler <GetAllResources.Handler>();
            ConfigurationContext.Current.TypeFactory.ForQuery <GetAllTranslations.Query>().SetHandler <GetAllTranslations.Handler>();

            ConfigurationContext.Current.TypeFactory.ForQuery <DetermineDefaultCulture.Query>().SetHandler <DetermineDefaultCulture.Handler>();

            ConfigurationContext.Current.TypeFactory.ForCommand <CreateNewResource.Command>().SetHandler <CreateNewResource.Handler>();
            ConfigurationContext.Current.TypeFactory.ForCommand <DeleteResource.Command>().SetHandler <DeleteResource.Handler>();
            ConfigurationContext.Current.TypeFactory.ForCommand <CreateOrUpdateTranslation.Command>().SetHandler <CreateOrUpdateTranslation.Handler>();
            ConfigurationContext.Current.TypeFactory.ForCommand <ClearCache.Command>().SetHandler <ClearCache.Handler>();

            if (setup != null)
            {
                ConfigurationContext.Setup(setup);
            }

            var synchronizer = new ResourceSynchronizer();

            synchronizer.DiscoverAndRegister();

            // set model metadata providers
            if (ConfigurationContext.Current.ModelMetadataProviders.ReplaceProviders)
            {
                // set current provider
                if (ModelMetadataProviders.Current == null)
                {
                    if (ConfigurationContext.Current.ModelMetadataProviders.UseCachedProviders)
                    {
                        ModelMetadataProviders.Current = new CachedLocalizedMetadataProvider();
                    }
                    else
                    {
                        ModelMetadataProviders.Current = new LocalizedMetadataProvider();
                    }
                }
                else
                {
                    if (ConfigurationContext.Current.ModelMetadataProviders.UseCachedProviders)
                    {
                        ModelMetadataProviders.Current = new CompositeModelMetadataProvider <CachedLocalizedMetadataProvider>(ModelMetadataProviders.Current);
                    }
                    else
                    {
                        ModelMetadataProviders.Current = new CompositeModelMetadataProvider <LocalizedMetadataProvider>(ModelMetadataProviders.Current);
                    }
                }

                for (var i = 0; i < ModelValidatorProviders.Providers.Count; i++)
                {
                    var provider = ModelValidatorProviders.Providers[i];
                    if (!(provider is DataAnnotationsModelValidatorProvider))
                    {
                        continue;
                    }

                    ModelValidatorProviders.Providers.RemoveAt(i);
                    ModelValidatorProviders.Providers.Insert(i, new LocalizedModelValidatorProvider());
                    break;
                }
            }

            return(builder);
        }
コード例 #4
0
        /// <summary>
        ///     This si the method you are likely to call if you want to use LocalizationProvider in your ASP.NET MVC application.
        /// </summary>
        /// <param name="builder">AppBuilder instance</param>
        /// <param name="setup">Your custom setup lambda</param>
        /// <returns>The same app builder instance to support chaining</returns>
        public static IAppBuilder UseDbLocalizationProvider(this IAppBuilder builder, Action <ConfigurationContext> setup = null)
        {
            var sw = new Stopwatch();

            sw.Start();

            var ctx = ConfigurationContext.Current;

            // setup default implementations
            ctx.TypeFactory.ForQuery <AvailableLanguages.Query>().SetHandler <DefaultAvailableLanguagesHandler>();
            ctx.TypeFactory.ForQuery <GetAllResources.Query>().DecorateWith <CachedGetAllResourcesHandler>();
            ctx.TypeFactory.ForQuery <GetAllTranslations.Query>().SetHandler <GetAllTranslationsHandler>();
            ctx.TypeFactory.ForQuery <DetermineDefaultCulture.Query>().SetHandler <DetermineDefaultCulture.Handler>();
            ctx.TypeFactory.ForCommand <ClearCache.Command>().SetHandler <ClearCacheHandler>();

            ctx.CacheManager = new HttpCacheManager();

            // custom callback invoke here (before rest of the config is finished)
            if (setup != null)
            {
                ConfigurationContext.Setup(setup);
            }

            // also we need to make sure that invariant culture is last in the list of fallback to invariant is true
            if (ctx.EnableInvariantCultureFallback)
            {
                ctx.FallbackCultures.Then(CultureInfo.InvariantCulture);
            }

            // if we need to sync - then it's good time to do it now
            var sync = new Synchronizer();

            sync.SyncResources(ctx.DiscoverAndRegisterResources);

            // set model metadata providers
            if (ctx.ModelMetadataProviders.ReplaceProviders)
            {
                if (ctx.ModelMetadataProviders.SetupCallback != null)
                {
                    ctx.ModelMetadataProviders.SetupCallback();
                }
                else
                {
                    // set current provider
                    if (ModelMetadataProviders.Current == null)
                    {
                        if (ctx.ModelMetadataProviders.UseCachedProviders)
                        {
                            ModelMetadataProviders.Current = new CachedLocalizedMetadataProvider();
                        }
                        else
                        {
                            ModelMetadataProviders.Current = new LocalizedMetadataProvider();
                        }
                    }
                    else
                    {
                        if (ctx.ModelMetadataProviders.UseCachedProviders)
                        {
                            ModelMetadataProviders.Current = new CompositeModelMetadataProvider <CachedLocalizedMetadataProvider>(ModelMetadataProviders.Current);
                        }
                        else
                        {
                            ModelMetadataProviders.Current = new CompositeModelMetadataProvider <LocalizedMetadataProvider>(ModelMetadataProviders.Current);
                        }
                    }

                    for (var i = 0; i < ModelValidatorProviders.Providers.Count; i++)
                    {
                        var provider = ModelValidatorProviders.Providers[i];

                        if (!(provider is DataAnnotationsModelValidatorProvider))
                        {
                            continue;
                        }

                        ModelValidatorProviders.Providers.RemoveAt(i);
                        ModelValidatorProviders.Providers.Insert(i, new LocalizedModelValidatorProvider());

                        break;
                    }
                }
            }

            sw.Stop();
            ctx.Logger?.Debug($"DbLocalizationProvider overall initialization took: {sw.ElapsedMilliseconds}ms");

            return(builder);
        }