Skip to content

Wrong culture returned from CultureInfo.GetCultureInfo("") #115913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
podliy16 opened this issue May 22, 2025 · 1 comment · May be fixed by #115919
Open

Wrong culture returned from CultureInfo.GetCultureInfo("") #115913

podliy16 opened this issue May 22, 2025 · 1 comment · May be fixed by #115919

Comments

@podliy16
Copy link

podliy16 commented May 22, 2025

Description

CultureInfo returns "Unknown Language" for empty string instead of InvariantCulture

The CultureInfo.GetCultureInfo("") method incorrectly returns a cached "Unknown Language" culture instead of the Invariant Culture as specified in the documentation.

Reference:
https://learn.microsoft.com/en-us/dotnet/api/System.Globalization.CultureInfo.GetCultureInfo?view=net-8.0#system-globalization-cultureinfo-getcultureinfo(system-string)

Documentation explicitly states:

If name is String.Empty, the method returns the invariant culture. This is equivalent to retrieving the value of the InvariantCulture property.

Current Behavior:
The method appears to be caching and returning an "Unknown Language" culture instead of the expected InvariantCulture when an empty string is provided.

This issue affects real-world applications, particularly when using Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware (https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.localization.requestlocalizationmiddleware?view=aspnetcore-9.0).

In our case:
Clients send Accept-Language: und header.
This results in calls to GetCultureInfo("und"), which cache Unknown Language culture under empty string key.
The unexpected "Unknown Language" culture causes application crashes since we only expected InvariantCulture per the documentation

Reproduction Steps

using System.Globalization;

var tmp = 5.42;
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("");
Console.WriteLine($"{tmp:F2}");
CultureInfo.GetCultureInfo("und");
Console.WriteLine($"{tmp:F2}");
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("");
Console.WriteLine($"{tmp:F2}");

Expected behavior

Doesn't crash, returns invariant culture.

Actual behavior

Crash with exception, because Unknown Language culture is not usable

Regression?

I don't think so

Known Workarounds

Currently, we don't have a clean solution for this issue. As a temporary measure, we're considering implementing a custom middleware with our own caching mechanism that properly handles empty culture strings by explicitly returning CultureInfo.InvariantCulture, or something like this.

Configuration

The issue has been reproduced on:

Windows 11 with .NET 9
Ubuntu 22.04.5 LTS (WSL) with .NET 8

We have reproduced it in production from official docker images as well, I'll add versions later, don't have access for specific tag right now.

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 22, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-globalization
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added this to the 10.0.0 milestone May 23, 2025
@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label May 23, 2025
@tarekgh tarekgh linked a pull request May 23, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants