Skip to content

RequestLocalizationMiddleware does not handle CultureInfo.InvariantCulture since version 8 #58843

Open
@cloudlucky

Description

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The RequestLocalizationMiddleware sets the culture to default even if the RequestLocalizationOptions supports CultureInfo.InvariantCulture since version 8.

Example:

builder.Services.Configure<RequestLocalizationOptions>(options => {
    var supportedCultures = new[]
    {
        CultureInfo.InvariantCulture,
        CultureInfo.GetCultureInfo("fr"),
        CultureInfo.GetCultureInfo("en"),
    };

    options.DefaultRequestCulture = new(supportedCultures[1]);
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;

    options.RequestCultureProviders =
    [
        new DefaultRequestCultureProvider()
    ];
});

public class DefaultRequestCultureProvider : IRequestCultureProvider
{
    public Task<ProviderCultureResult?> DetermineProviderCultureResult(HttpContext httpContext)
    {
        var result = new ProviderCultureResult(culture: CultureInfo.InvariantCulture.Name, uiCulture: CultureInfo.CurrentUICulture.Name);

        return Task.FromResult<ProviderCultureResult?>(result);
    }
}

At the end, CultureInfo.CurrentCulture was always the DefaultRequestCulture instead of CultureInfo.InvariantCulture.

Note: this code worked until version 8

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlesware

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions