Skip to content

Commit a8fcda9

Browse files
authored
Fix GetGeneralCode handling of zh-CN and zh-TW (#1476)
1 parent dce7400 commit a8fcda9

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1717
## [Unreleased]
1818

1919
### Fixed
20+
- [SIL.WritingSystems] Fix IetfLanguageTag.GetGeneralCode to handle cases when zh-CN or zh-TW is a prefix and not the whole string.
2021
- [SIL.Windows.Forms] Prevent BetterLabel from responding to OnTextChanged when it has been disposed.
2122

2223
### Changed

SIL.WritingSystems.Tests/IetfLanguageTagTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ public void TryGetSubtags_ComplexPrivateLanguageCode_ReturnsValidResults()
689689
[TestCase("qaa", ExpectedResult = "qaa")]
690690
[TestCase("qed", ExpectedResult = "qed")]
691691
[TestCase("qed-Lepc-x-rubbish", ExpectedResult = "qed")]
692+
[TestCase("zh-CN-fonipa", ExpectedResult = "zh")]
692693
public string GetLanguagePart_ValidIetfTag_ReturnsLanguage(string tag)
693694
{
694695
return IetfLanguageTag.GetLanguagePart(tag);
@@ -1109,5 +1110,20 @@ public void GetBestLanguageName_ForLotsOfVariants_FindsExpectedName(string codeV
11091110
Assert.That(name, Is.EqualTo(expectedResult));
11101111
}
11111112
#endregion
1113+
1114+
[TestCase("tpi-AR", ExpectedResult = "tpi")]
1115+
[TestCase("tpi-Lepc-BR-fonipa-x-blah", ExpectedResult = "tpi")]
1116+
[TestCase("qaa", ExpectedResult = "qaa")]
1117+
[TestCase("qed-Lepc-x-rubbish", ExpectedResult = "qed")]
1118+
[TestCase("zh-BN", ExpectedResult = "zh")]
1119+
[TestCase("zh-CN-fonipa", ExpectedResult = "zh-CN")]
1120+
[TestCase("zh-Hans", ExpectedResult = "zh")]
1121+
[TestCase("zh-TW", ExpectedResult = "zh-TW")]
1122+
[TestCase("a", ExpectedResult = "a")]
1123+
[TestCase("-", ExpectedResult = "")]
1124+
public string GetGeneralCode_ReturnsCode(string tag)
1125+
{
1126+
return IetfLanguageTag.GetGeneralCode(tag);
1127+
}
11121128
}
11131129
}

SIL.WritingSystems/IetfLanguageTag.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,9 @@ public static IEnumerable<VariantSubtag> GetVariantSubtags(string langTag)
11161116
/// <summary>
11171117
/// Gets the language part of the specified language tag.
11181118
/// </summary>
1119+
/// <remarks>
1120+
/// Returns `zh` for both `zh-CN` and `zh-TW`.
1121+
/// </remarks>
11191122
[PublicAPI]
11201123
public static string GetLanguagePart(string langTag)
11211124
{
@@ -1523,10 +1526,12 @@ public static string GetGeneralCode(string code)
15231526
// Though you might be tempted to simplify this by using GetLanguagePart, don't: this
15241527
// methods works with three-letter codes even if there is a valid 2-letter code that
15251528
// should be used instead.
1529+
if (code.StartsWith(ChineseSimplifiedTag))
1530+
return ChineseSimplifiedTag;
1531+
if (code.StartsWith(ChineseTraditionalTag))
1532+
return ChineseTraditionalTag;
15261533
var idxCountry = code.IndexOf("-");
1527-
if (idxCountry == -1 || code == ChineseSimplifiedTag || code == ChineseTraditionalTag)
1528-
return code;
1529-
return code.Substring(0, idxCountry);
1534+
return idxCountry == -1 ? code : code.Substring(0, idxCountry);
15301535
}
15311536

15321537
/// <summary>

0 commit comments

Comments
 (0)