Open Closed

Issue on Getting Culture Info over Accept-Language Header #9571


User avatar
0
omer_yel created

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)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent

    To ensure your ABP API correctly reads the culture from the Accept-Language header, you should:

    1. Call app.UseAbpRequestLocalization() only once, after configuring the options.
    2. Make sure UseAbpRequestLocalization is placed before UseAuthentication and UseAuthorization in the middleware pipeline.
    3. The AcceptLanguageHeaderRequestCultureProvider should be the first provider in the RequestCultureProviders 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 the Accept-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.

  • User Avatar
    0
    omer_yel created

    I noticed i wrote app.UseAbpRequestLocalization(); twice. I remove below one. Result is still same. Any other advice?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    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 your Blazor server UI project

    What is the current localization behavior?

    Can you share some screenshots or logs?

    Thanks.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on July 14, 2025, 11:57