core icon indicating copy to clipboard operation
core copied to clipboard

System.Globalization.RegionInfo.CurrentRegion does not reflect the changes of CultureInfo.CurrentCulture

Open ighristov opened this issue 3 years ago • 4 comments

Description

The System.Globalization.RegionInfo.CurrentRegion returns the value based on the result of GetUserGeoId() rather than CultureInfo.CurrentCulture. The current behavior is a change from .NET Framework. The following code snippet works in .NET Framework 4.8 and fails in .NET 7.0:

        using System.Globalization;

        static void AssertCurrentRegion()
        {
            // Ensure the current user's region is set to US.
            System.Diagnostics.Debug.Assert(RegionInfo.CurrentRegion.DisplayName == "United States");
            
            CultureInfo.CurrentCulture.ClearCachedData();
            CultureInfo.CurrentCulture = new CultureInfo("fr-FR");

            // This assert fails in .NET 6+, but passes in .NET Framework 4.6+
            System.Diagnostics.Debug.Assert(RegionInfo.CurrentRegion.DisplayName == "France");
        }

Configuration

.NET Framework 4.8, .NET 6, .NET 7 Windows 10 21H2

Regression?

Yes. The code above works as expected in a .NET Framework app. Seems there is no straight-forward way to change the RegionInfo.CurrentRegion value.

Other information

A variant of the code above is used in unit tests which checks the current region's IsMetric property. The default region is US and the test needs to change it to another region with Metric system - in this case France.

Inspected the .NET code and noticed that after clearing the cachedData, the new RegionInfo is instantiated based on the CultureData.GetCurrentRegionData(), which first executes GetUserGeoID() and if doesn't succeed, falls back to CultureInfo.CurrentCulture instance. In .NET Framework code the RegionInfo is directly instantiated by CultureInfo.CurrentCulture instance.

As a workaround we can use SetUserGeoID(GeoId) function, but this changes the current user's Region, which is not safe.

ighristov avatar Dec 28 '22 10:12 ighristov

@dotnet/area-system-globalization could you please have a look?

greenEkatherine avatar Dec 28 '22 15:12 greenEkatherine

I believe this is related to https://github.com/dotnet/runtime/pull/33834, where RegionInfo.CurrentRegion behavior was corrected on Windows to no longer rely on CurrentCulture.

Windows has different settings for the default region than the current culture.

As stated in one of the unit tests added with that change:

                RegionInfo ri = RegionInfo.CurrentRegion;
                CultureInfo.CurrentCulture.ClearCachedData(); // clear the current region cached data

                CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

                // Changing the current culture shouldn't affect the default current region as we get it from Windows settings.
                Assert.Equal(ri.TwoLetterISORegionName, RegionInfo.CurrentRegion.TwoLetterISORegionName);

As alluded to in that change, the unit test, and the documentation for CurrentRegion, the region should only be changed through Windows settings.

Our unit tests for IsMetric within dotnet/runtime construct a new RegionInfo rather than relying on CurrentRegion. For your own code, could you also change your implementation to allow a RegionInfo to be passed in, perhaps defaulting to CurrentRegion when not provided? Then your unit tests could provide a RegionInfo with and without metric?

jeffhandley avatar Dec 28 '22 20:12 jeffhandley

@jeffhandley is right. This is an intentional change because of the change on Windows. If you want to get the original behavior, instead of calling CurrentRegion, just do new RegionInfo(CultureInfo.CurrentCulture.Name). By that I am closing this issue. Feel free to send any question if you have any. Thanks for your report.

tarekgh avatar Dec 29 '22 20:12 tarekgh

Thanks for the provided info. Since it's an intentional change, I'll fix the code on our end, using new RegionInfo(CultureInfo.CurrentCulture.Name). I assume it will behave consistently on all combinations of OS and .NET versions.

ighristov avatar Dec 30 '22 08:12 ighristov