Set default culture

Kuler Master 406 Reputation points
2025-02-07T10:08:15.3666667+00:00

Hello guys,

I am using Microsoft.Extensions.Localization (8.0.11) but for some reason the following code does not work as expected. As you see, I expect the German to be default language but it's always English. Am I missing something?

var supportedCultures = new[] { "de-DE", "en-US" }; 
var localizationOptions = new RequestLocalizationOptions()     
.SetDefaultCulture("de-DE")     
.AddSupportedCultures(supportedCultures)     
.AddSupportedUICultures(supportedCultures); 

Thank you

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Blazor
{count} votes

1 answer

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 1,020 Reputation points Microsoft External Staff
    2025-07-10T07:10:40.4233333+00:00

    Hi @Kuler Master ,

    Here are a few recommendations and troubleshooting steps to help resolve this:

    1. Verify Middleware Execution Order

    The RequestLocalizationMiddleware must be registered before any middleware or components that rely on culture settings. From your shared Program.cs, it looks like you're applying it correctly, but double-check that it's placed before app.UseMiddleware<LocalizationMiddleware>() and any routing or endpoint configuration.

    1. Ensure Culture Providers Are Not Overriding Defaults

    ASP.NET Core uses a series of IRequestCultureProviders (e.g., query string, cookies, headers) to determine the culture. If any of these providers are active and detect a different culture (like en-US from browser headers), they will override your default.

    Recommendation: Consider customizing or reordering the culture providers if you want to strictly enforce de-DE as the default unless explicitly overridden.

    1. Check Browser Language Settings

    Browsers often send Accept-Language headers that influence culture detection. If your browser is set to English, it may override the default.

    Recommendation: Try testing with a browser set to German or temporarily remove the AcceptLanguageHeaderRequestCultureProvider from the provider list to isolate the issue.

    1. Validate Resource Availability

    Ensure that your .resx files for de-DE are correctly named and placed in the Resources folder. Missing or misnamed resource files can cause fallback to English.

    1. Use Logging for Diagnostics

    Add logging or debug output to confirm which culture is being applied during runtime:

    Console.WriteLine($"CurrentCulture: {CultureInfo.CurrentCulture}");
    Console.WriteLine($"CurrentUICulture: {CultureInfo.CurrentUICulture}"); 
    

    For more information, you can check out these links:

    https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-9.0

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization/select-language-culture?view=aspnetcore-9.0

    Hope my answer would help.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.