From 2fc2311fe2b4919ab7f1c9c405b4201d060e79c9 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Thu, 12 Nov 2020 15:22:17 -0800 Subject: [PATCH] Fix zh cultures parent chain (#44607) --- .../tests/CultureInfo/CultureInfoParent.cs | 5 +++++ .../src/System/Globalization/CultureInfo.cs | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/libraries/System.Globalization/tests/CultureInfo/CultureInfoParent.cs b/src/libraries/System.Globalization/tests/CultureInfo/CultureInfoParent.cs index be9d74e..632afa3 100644 --- a/src/libraries/System.Globalization/tests/CultureInfo/CultureInfoParent.cs +++ b/src/libraries/System.Globalization/tests/CultureInfo/CultureInfoParent.cs @@ -11,6 +11,11 @@ namespace System.Globalization.Tests [InlineData("en-US", "en")] [InlineData("en", "")] [InlineData("", "")] + [InlineData("zh-CN", "zh-Hans")] + [InlineData("zh-SG", "zh-Hans")] + [InlineData("zh-HK", "zh-Hant")] + [InlineData("zh-MO", "zh-Hant")] + [InlineData("zh-TW", "zh-Hant")] public void Parent(string name, string expectedParentName) { CultureInfo culture = new CultureInfo(name); diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs index 6cf5e4c..82258f7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs @@ -496,6 +496,28 @@ namespace System.Globalization CultureInfo culture; string parentName = _cultureData.ParentName; + if (parentName == "zh" && _name.Length == 5 && _name[2] == '-') + { + // We need to keep the parent chain for the zh cultures as follows to preserve the resource lookup compatability + // zh-CN -> zh-Hans -> zh -> Invariant + // zh-HK -> zh-Hant -> zh -> Invariant + // zh-MO -> zh-Hant -> zh -> Invariant + // zh-SG -> zh-Hans -> zh -> Invariant + // zh-TW -> zh-Hant -> zh -> Invariant + + if ((_name[3] == 'C' && _name[4] == 'N' ) || // zh-CN + (_name[3] == 'S' && _name[4] == 'G' )) // zh-SG + { + parentName = "zh-Hans"; + } + else if ((_name[3] == 'H' && _name[4] == 'K' ) || // zh-HK + (_name[3] == 'M' && _name[4] == 'O' ) || // zh-MO + (_name[3] == 'T' && _name[4] == 'W' )) // zh-TW + { + parentName = "zh-Hant"; + } + } + if (string.IsNullOrEmpty(parentName)) { culture = InvariantCulture; -- 2.7.4