Fix neutral cultures created with the underscore (#87411)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Tue, 13 Jun 2023 03:34:04 +0000 (20:34 -0700)
committerGitHub <noreply@github.com>
Tue, 13 Jun 2023 03:34:04 +0000 (20:34 -0700)
src/libraries/System.Globalization/tests/CultureInfo/CultureInfoCtor.cs
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Icu.cs

index 329bf07..44386fa 100644 (file)
@@ -452,17 +452,12 @@ namespace System.Globalization.Tests
             Assert.Equal(expectedSortName, ci.CompareInfo.Name);
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsIcuGlobalization))]
-        public void TestNeutralCultureWithCollationName()
-        {
-            Assert.Throws<CultureNotFoundException>(() => CultureInfo.GetCultureInfo("zh-u-co-zhuyin"));
-            Assert.Throws<CultureNotFoundException>(() => CultureInfo.GetCultureInfo("de-u-co-phonebk"));
-        }
-
         [InlineData("xx-u-XX", "xx-u-xx")]
         [InlineData("xx-u-XX-u-yy", "xx-u-xx-u-yy")]
         [InlineData("xx-t-ja-JP", "xx-t-ja-jp")]
         [InlineData("qps-plocm", "qps-PLOCM")] // ICU normalize this name to "qps--plocm" which we normalize it back to "qps-plocm"
+        [InlineData("zh_CN", "zh_cn")]
+        [InlineData("km_KH", "km_kh")]
         [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsIcuGlobalization))]
         public void TestCreationWithICUNormalizedNames(string cultureName, string expectedCultureName)
         {
index 2eeb65d..0d4dad1 100644 (file)
@@ -138,13 +138,9 @@ namespace System.Globalization
             _bNeutral = TwoLetterISOCountryName.Length == 0;
             _sSpecificCulture = _bNeutral ? IcuLocaleData.GetSpecificCultureName(_sRealName) : _sRealName;
 
-            if (_bNeutral && collationStart > 0)
-            {
-                return false; // neutral cultures cannot have collation
-            }
-
             // Remove the sort from sName unless custom culture
-            _sName = collationStart < 0 ? _sRealName : _sRealName.Substring(0, collationStart);
+            // To ensure compatibility, it is necessary to allow the creation of cultures like zh_CN (using ICU notation) in the case of _bNeutral.
+            _sName = collationStart < 0 || _bNeutral ? _sRealName : _sRealName.Substring(0, collationStart);
 
             return true;
         }