Hello, We have issue to change API culture. We have created non-ABP external Blazor server UI project. We send request to api endpoints. But we can not specify request or client culture while sending. Culture info in sending on Accept-Language header.
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
var supportedCultures = new[]
{
new CultureInfo("en-GB"),
new CultureInfo("tr-TR")
};
app.UseAbpRequestLocalization(options =>
{
options.DefaultRequestCulture = new RequestCulture("en-GB");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{ new AcceptLanguageHeaderRequestCultureProvider(),
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()};
});
app.UseAbpRequestLocalization();
//app.UseStaticFiles();
//app.MapStaticAssets();
app.MapAbpStaticAssets();
app.UseAbpSecurityHeaders();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseUnitOfWork();
app.UseDynamicClaims();
app.UseAuthorization();
app.UseSwagger();
app.UseAbpSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Aizanoi API");
var configuration = context.GetConfiguration();
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
});
app.UseAbpSerilogEnrichers();
app.UseConfiguredEndpoints();
}
We have put above code into ApiHostModule.cs.
Our supported languages are "tr-TR" and "en-GB". How can we correctly configure our api to get culture from request Accept-Language header?
3 Answer(s)
-
0
To ensure your ABP API correctly reads the culture from the
Accept-Language
header, you should:- Call
app.UseAbpRequestLocalization()
only once, after configuring the options. - Make sure
UseAbpRequestLocalization
is placed beforeUseAuthentication
andUseAuthorization
in the middleware pipeline. - The
AcceptLanguageHeaderRequestCultureProvider
should be the first provider in theRequestCultureProviders
list, as you have done.
Your configuration is almost correct, but you should remove the second call to
app.UseAbpRequestLocalization();
(you only need to call it once with your options). The correct order is:public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } var supportedCultures = new[] { new CultureInfo("en-GB"), new CultureInfo("tr-TR") }; app.UseAbpRequestLocalization(options => { options.DefaultRequestCulture = new RequestCulture("en-GB"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.RequestCultureProviders = new List<IRequestCultureProvider> { new AcceptLanguageHeaderRequestCultureProvider(), new QueryStringRequestCultureProvider(), new CookieRequestCultureProvider() }; }); // ... other middleware registrations ... app.UseAuthentication(); app.UseAuthorization(); // ... rest of your pipeline ... }
Key Points:
- Only call
UseAbpRequestLocalization
once, with your configuration. - Place it before authentication and authorization.
- The
AcceptLanguageHeaderRequestCultureProvider
will automatically pick up theAccept-Language
header from incoming requests.
This will allow your API to respect the
Accept-Language
header and set the culture accordingly.Sources:
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.
- Call
-
0
I noticed i wrote app.UseAbpRequestLocalization(); twice. I remove below one. Result is still same. Any other advice?
-
0
hi
Your code seems no problem.
We have created non-ABP external Blazor server UI project. But we can not specify request or client culture while sending.
You can set the
Accept-Language
in yourBlazor server UI project
What is the current localization behavior?
Can you share some screenshots or logs?
Thanks.