Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });

            services.AddSingleton(this.Configuration);
            services.AddMvc()
            .AddViewLocalization(
                LanguageViewLocationExpanderFormat.Suffix,
                opts => { opts.ResourcesPath = "Resources"; })
            .AddDataAnnotationsLocalization();

            services.AddMemoryCache();

            string database = this.Configuration["database"];

            if ("sqlite".Equals(database, StringComparison.CurrentCultureIgnoreCase))
            {
                services.AddEntityFrameworkSqlite()
                .AddDbContext <Core.Data.BlogContext>(opt =>
                {
                    opt.UseSqlite(this.Configuration["connectionString"], builder => { builder.MigrationsAssembly("DotNetBlog.Web"); });
                });
            }
            else if ("sqlserver".Equals(database, StringComparison.CurrentCultureIgnoreCase))
            {
                services.AddEntityFrameworkSqlServer()
                .AddDbContext <Core.Data.BlogContext>(opt =>
                {
                    opt.UseSqlServer(this.Configuration["connectionString"], builder =>
                    {
                        builder.MigrationsAssembly("DotNetBlog.Web");
                        builder.UseRowNumberForPaging();
                    });
                });
            }

            services.AddBlogService();

            AutoMapperConfig.Configure();

            services.Configure <RequestLocalizationOptions>(
                opts =>
            {
                var supportedCultures = new List <CultureInfo>
                {
                    new CultureInfo("en-GB"),
                    new CultureInfo("zh-CN")
                };

                opts.DefaultRequestCulture = new RequestCulture("en-GB");
                // Formatting numbers, dates, etc.
                opts.SupportedCultures = supportedCultures;
                // UI strings that we have localized.
                opts.SupportedUICultures = supportedCultures;

                //Uncomment for change language by user
                //opts.RequestCultureProviders.Add(new CookieRequestCultureProvider());
                //opts.RequestCultureProviders.Add(new AcceptLanguageHeaderRequestCultureProvider());
                //opts.RequestCultureProviders.Add(new QueryStringRequestCultureProvider() { QueryStringKey = "lang", UIQueryStringKey = "ui-lang" });
                opts.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
                {
                    var settingService = context.RequestServices.GetService <Core.Service.SettingService>();
                    // My custom request culture logic
                    return(Task.Run(() => new ProviderCultureResult(settingService.Get().Language)));
                }));
            });

            services.Configure <RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new ViewEngines.ThemeViewEngine());
            });
        }