Created
September 25, 2018 04:35
-
-
Save vlapenkov/308a68ec5bba0d6db6f9d8e771a2b1c6 to your computer and use it in GitHub Desktop.
Manually set culture in asp core app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddCors(); | |
// Add framework services. | |
services.AddDbContext<ApplicationDbContext>(options => | |
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); | |
services.AddIdentity<ApplicationUser, IdentityRole>() | |
.AddEntityFrameworkStores<ApplicationDbContext>() | |
.AddDefaultTokenProviders(); | |
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); | |
services.Configure<MySettings>(Configuration.GetSection("MySettings")); | |
services.AddSingleton<IConfiguration>(Configuration); | |
services.AddResponseCaching(); | |
services.AddMvc().AddViewLocalization( | |
LanguageViewLocationExpanderFormat.Suffix, | |
opts => { opts.ResourcesPath = "Resources"; }) | |
.AddDataAnnotationsLocalization(); | |
services.Configure<RequestLocalizationOptions>( | |
opts => | |
{ | |
var supportedCultures = new List<CultureInfo> | |
{ | |
new CultureInfo("en-GB"), | |
new CultureInfo("en-US"), | |
new CultureInfo("en"), | |
new CultureInfo("fr-FR"), | |
new CultureInfo("fr"), | |
}; | |
opts.DefaultRequestCulture = new RequestCulture("en-US"); | |
// Formatting numbers, dates, etc. | |
opts.SupportedCultures = supportedCultures; | |
// UI strings that we have localized. | |
opts.SupportedUICultures = supportedCultures; | |
}); | |
// Adds a default in-memory implementation of IDistributedCache. | |
services.AddDistributedMemoryCache(); | |
services.AddSession(options => | |
{ | |
// Set a short timeout for easy testing. | |
options.IdleTimeout = TimeSpan.FromSeconds(10); | |
options.CookieHttpOnly = true; | |
}); | |
// Add application services. | |
services.AddTransient<IEmailSender, AuthMessageSender>(); | |
services.AddTransient<ISmsSender, AuthMessageSender>(); | |
services.AddMemoryCache(); | |
services.AddSingleton<DbCachingService>(); | |
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
// отключаю стандартный log, т.к. он не работает | |
// loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
// loggerFactory.AddDebug(); | |
// loggerFactory.AddEventLog(); | |
// логирую в файл C:\Temp\nlog | |
loggerFactory.AddNLog(); | |
//add NLog.Web | |
app.AddNLogWeb(); | |
app.UseResponseCaching(); | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
app.UseDatabaseErrorPage(); | |
app.UseBrowserLink(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Home/Error"); | |
} | |
app.UseStaticFiles(); | |
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>(); | |
app.UseRequestLocalization(options.Value); | |
app.UseIdentity(); | |
app.UseSession(); | |
var webSocketOptions = new WebSocketOptions() | |
{ | |
KeepAliveInterval = TimeSpan.FromSeconds(120), | |
ReceiveBufferSize = 4 * 1024 | |
}; | |
app.UseWebSockets(webSocketOptions); | |
// app.UseSession(o => o.IdleTimeout = TimeSpan.FromSeconds(10)); | |
app.Use(async (context, next) => | |
{ | |
var requestCulture = new RequestCulture("en"); | |
context.Features.Set<IRequestCultureFeature>(new RequestCultureFeature(requestCulture, new QueryStringRequestCultureProvider())); | |
CultureInfo.CurrentCulture = requestCulture.Culture; | |
CultureInfo.CurrentUICulture = requestCulture.UICulture; | |
await next.Invoke(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment