Is .net core localization set the Current thread language automatically or is it should be done manually?

  • Thread starter Thread starter cansuk
  • Start date Start date
C

cansuk

Guest
public async Task Invoke(HttpContext context, IApplicationBuilder builder)
{
List<Language> languagesList = null;
using (var scope = builder.ApplicationServices.CreateScope())
{

var localizationService = scope.ServiceProvider.GetRequiredService<ILocalizationService>();
languagesList = localizationService.Languages;
}

var supportedCultures = languagesList
.Where(languages => languages.IsActive)
.Select(languages => new CultureInfo(languages.TwoLetterCode))
.Distinct()
.ToArray();

var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("tr-TR"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures,
};

options.RequestCultureProviders.Clear();
options.RequestCultureProviders.Insert(0, new AcceptLanguageHeaderRequestCultureProvider());
options.RequestCultureProviders.Insert(1, new QueryStringRequestCultureProvider());

builder.UseRequestLocalization(options);

await _next(context);

}


Hi,

We use .net cre localization. We expect the current thread language set as according to http request accept header (1), cookie (2), or query parameter (3) methods. For example, we send an htpp request with accept header "en". After that when we check the current thread culture language it is still "tr" which is our default culture. We are not doing any additional operations to set te current thread. We exect it to be in automatically. IS it possible? Are we missing some points? Or it is not possible and should we do it manually(i mean setting current thread language according to accept header (1), cookie (2), or query parametere (3)) ?

Please help, thank you.

Code :

public void ConfigureServices(IServiceCollection services)
{
//...
services.AddLocalization();
var sp = services.BuildServiceProvider();
var fooService = sp.GetService<ILocalizationService>();
var supportedCultures = fooService.Languages
.Where(languages => languages.IsActive)
.Select(languages => new CultureInfo(languages.TwoLetterCode))
.Distinct()
.ToArray();

services.Configure<RequestLocalizationOptions>(s =>
{
s.SupportedCultures = supportedCultures;
s.SupportedUICultures = supportedCultures;
s.DefaultRequestCulture = new RequestCulture(culture: "tr", uiCulture: "tr");
s.RequestCultureProviders.Clear();
s.RequestCultureProviders.Insert(0, new AcceptLanguageHeaderRequestCultureProvider());
s.RequestCultureProviders.Insert(1, new QueryStringRequestCultureProvider());
});
//...
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApiVersionDescriptionProvider provider, ILoggerFactory loggerFactory)
{
// ...
app.UseLanguageHandler();
// ...
}

Continue reading...
 
Back
Top