public override Task <ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } var path = httpContext.Request.Path.ToString(); var languageParts = path.Split('/'); if (languageParts.Length > 0) { var maybeLanguage = languageParts[1]; if (!String.IsNullOrWhiteSpace(maybeLanguage)) { var languageValidation = _languageManipulationService.LanguageValidationRegexp(); if (Regex.IsMatch(maybeLanguage, languageValidation, RegexOptions.IgnoreCase)) { var languageDefinition = _languageManipulationService.LanguageRepresentationToLanguageDefinition(maybeLanguage); var cultureInfo = _languageManipulationService.LanguageDefinitionToCultureInfo(languageDefinition); return(Task.FromResult(new ProviderCultureResult(cultureInfo.Name))); } } } // No result determined return(Task.FromResult((ProviderCultureResult)null)); }
// Configure is called after ConfigureServices is called. public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IPageConfiguration pageConfiguration, ILanguageManipulationService languageManipulationService) { if (pageConfiguration == null) { throw new ArgumentNullException(nameof(pageConfiguration)); } if (languageManipulationService == null) { throw new ArgumentNullException(nameof(languageManipulationService)); } loggerFactory.MinimumLevel = LogLevel.Warning; loggerFactory.AddConsole(); // Configure the HTTP request pipeline. // Add the following to the request pipeline only in development environment. if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(x => x.EnableAll()); loggerFactory.AddDebug(LogLevel.Debug); } else { // Add Error handling middleware which catches all application specific errors and // sends the request to the following path or controller action. app.UseExceptionHandler("/Home/Error"); loggerFactory.AddDebug(LogLevel.Critical); } // Add the platform handler to the request pipeline. app.UseIISPlatformHandler(); // Add static files to the request pipeline. app.UseStaticFiles(); // Add cookie-based authentication to the request pipeline. app.UseIdentity(); var defaultCulture = languageManipulationService .LanguageDefinitionToCultureInfo( pageConfiguration.DefaultLanguage ); app.UseRequestLocalization(new RequestLocalizationOptions { SupportedCultures = languageManipulationService.SupportedCultures, SupportedUICultures = languageManipulationService.SupportedCultures, RequestCultureProviders = new[] { new CustomUrlStringCultureProvider(languageManipulationService) } }, new RequestCulture(defaultCulture, defaultCulture) ); // Add MVC to the request pipeline. app.UseMvc(routes => { routes.MapRoute( name: nameof(PersonalWebsite.Areas.Private), template: "{area}/{controller}/{action}/{id?}", defaults: new { }, constraints: new { area = "private" }); routes.MapRoute( name: "defaultWithLanguage", template: "{language}/{controller=Home}/{action=Index}", defaults: new { }, constraints: new { language = languageManipulationService.LanguageValidationRegexp() } ); routes.MapRoute( name: "defaultWithoutLanguage", template: "{controller=Home}/{action=Index}", defaults: new { language = String.Empty } ); routes.MapRoute( name: "contentsWithLanguage", template: "{language}/{urlName}/{controller=Contents}/{action=Show}", defaults: new { }, constraints: new { language = languageManipulationService.LanguageValidationRegexp() } ); routes.MapRoute( name: "contentsWithoutLanguage", template: "{urlName}/{controller=Contents}/{action=Show}" ); }); using (var dataInitializer = new DataInitializer(app.ApplicationServices)) { if (env.IsDevelopment()) { dataInitializer.ClearRequiredContents(); dataInitializer.ClearInitialUser(); } dataInitializer.EnsureRequiredContentsAvailable(); dataInitializer.EnsureInitialUserAvaialble(); } }
// Configure is called after ConfigureServices is called. public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IPageConfiguration pageConfiguration, ILanguageManipulationService languageManipulationService) { if(pageConfiguration == null) { throw new ArgumentNullException(nameof(pageConfiguration)); } if(languageManipulationService == null) { throw new ArgumentNullException(nameof(languageManipulationService)); } loggerFactory.MinimumLevel = LogLevel.Warning; loggerFactory.AddConsole(); // Configure the HTTP request pipeline. // Add the following to the request pipeline only in development environment. if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(x => x.EnableAll()); loggerFactory.AddDebug(LogLevel.Debug); } else { // Add Error handling middleware which catches all application specific errors and // sends the request to the following path or controller action. app.UseExceptionHandler("/Home/Error"); loggerFactory.AddDebug(LogLevel.Critical); } // Add the platform handler to the request pipeline. app.UseIISPlatformHandler(); // Add static files to the request pipeline. app.UseStaticFiles(); // Add cookie-based authentication to the request pipeline. app.UseIdentity(); var defaultCulture = languageManipulationService .LanguageDefinitionToCultureInfo( pageConfiguration.DefaultLanguage ); app.UseRequestLocalization(new RequestLocalizationOptions { SupportedCultures = languageManipulationService.SupportedCultures, SupportedUICultures = languageManipulationService.SupportedCultures, RequestCultureProviders = new[] { new CustomUrlStringCultureProvider(languageManipulationService) } }, new RequestCulture(defaultCulture, defaultCulture) ); // Add MVC to the request pipeline. app.UseMvc(routes => { routes.MapRoute( name: nameof(PersonalWebsite.Areas.Private), template: "{area}/{controller}/{action}/{id?}", defaults: new { }, constraints: new { area = "private" }); routes.MapRoute( name: "defaultWithLanguage", template: "{language}/{controller=Home}/{action=Index}", defaults: new { }, constraints: new { language = languageManipulationService.LanguageValidationRegexp() } ); routes.MapRoute( name: "defaultWithoutLanguage", template: "{controller=Home}/{action=Index}", defaults: new { language=String.Empty } ); routes.MapRoute( name: "contentsWithLanguage", template: "{language}/{urlName}/{controller=Contents}/{action=Show}", defaults: new { }, constraints: new { language = languageManipulationService.LanguageValidationRegexp() } ); routes.MapRoute( name: "contentsWithoutLanguage", template: "{urlName}/{controller=Contents}/{action=Show}" ); }); using (var dataInitializer = new DataInitializer(app.ApplicationServices)) { if (env.IsDevelopment()) { dataInitializer.ClearRequiredContents(); dataInitializer.ClearInitialUser(); } dataInitializer.EnsureRequiredContentsAvailable(); dataInitializer.EnsureInitialUserAvaialble(); } }