From 5ca628c127ccd9d290532831e9331b182d24e480 Mon Sep 17 00:00:00 2001 From: Thijs Brobbel Date: Tue, 1 Nov 2016 21:13:31 +0100 Subject: [PATCH 1/5] Add new overload for Ordinalize that accepts a CultureInfo, so the consumer can override the requested culture. This fixes #593. See the discussion there for details. --- src/Humanizer.Tests.Shared/OrdinalizeTests.cs | 11 ++++++++++- src/Humanizer/OrdinalizeExtensions.cs | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Humanizer.Tests.Shared/OrdinalizeTests.cs b/src/Humanizer.Tests.Shared/OrdinalizeTests.cs index 99638bf9d..fa5abc136 100644 --- a/src/Humanizer.Tests.Shared/OrdinalizeTests.cs +++ b/src/Humanizer.Tests.Shared/OrdinalizeTests.cs @@ -1,4 +1,5 @@ -using Xunit; +using System.Globalization; +using Xunit; namespace Humanizer.Tests { @@ -95,5 +96,13 @@ public void OrdinalizeStringGenderIsImmaterial(string number) var feminineOrdinalized = number.Ordinalize(GrammaticalGender.Feminine); Assert.Equal(masculineOrdinalized, feminineOrdinalized); } + + [Theory] + [InlineData("en-US", 1, "1st")] + [InlineData("nl-NL", 1, "1e")] + public void OrdinalizeWithCultureOverridesCurrentCulture(CultureInfo culture, int number, string ordinalized) + { + Assert.Equal(number.Ordinalize(culture), ordinalized); + } } } diff --git a/src/Humanizer/OrdinalizeExtensions.cs b/src/Humanizer/OrdinalizeExtensions.cs index b689804b0..069a3052c 100644 --- a/src/Humanizer/OrdinalizeExtensions.cs +++ b/src/Humanizer/OrdinalizeExtensions.cs @@ -42,6 +42,17 @@ public static string Ordinalize(this int number) return Configurator.Ordinalizer.Convert(number, number.ToString(CultureInfo.InvariantCulture)); } + /// + /// Turns a number into an ordinal number used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. + /// + /// The number to be ordinalized + /// Culture to use. If null, current thread's UI culture is used. + /// + public static string Ordinalize(this int number, CultureInfo culture) + { + return Configurator.Ordinalizers.ResolveForCulture(culture).Convert(number, number.ToString(culture)); + } + /// /// Turns a number into an ordinal number used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. /// Gender for Brazilian Portuguese locale From 4bb4540e27f852978c7ffe18d7a945066ec45a80 Mon Sep 17 00:00:00 2001 From: Alexander Zaytsev Date: Fri, 9 Dec 2016 09:54:38 +1300 Subject: [PATCH 2/5] Correct OrdinalizeWithCultureOverridesCurrentCulture test parameters --- src/Humanizer.Tests.Shared/OrdinalizeTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Humanizer.Tests.Shared/OrdinalizeTests.cs b/src/Humanizer.Tests.Shared/OrdinalizeTests.cs index fa5abc136..95383dfa7 100644 --- a/src/Humanizer.Tests.Shared/OrdinalizeTests.cs +++ b/src/Humanizer.Tests.Shared/OrdinalizeTests.cs @@ -100,8 +100,9 @@ public void OrdinalizeStringGenderIsImmaterial(string number) [Theory] [InlineData("en-US", 1, "1st")] [InlineData("nl-NL", 1, "1e")] - public void OrdinalizeWithCultureOverridesCurrentCulture(CultureInfo culture, int number, string ordinalized) + public void OrdinalizeWithCultureOverridesCurrentCulture(string cultureName, int number, string ordinalized) { + var culture = new CultureInfo(cultureName); Assert.Equal(number.Ordinalize(culture), ordinalized); } } From dd234809ea2cff4ed2044aa38e7e89400cadedf7 Mon Sep 17 00:00:00 2001 From: Thijs Brobbel Date: Fri, 9 Dec 2016 08:51:14 +0100 Subject: [PATCH 3/5] Add new Ordinalize overload to approved public API per discussion on PR #595. --- .../PublicApiApprovalTest.approve_public_api.approved.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index fad778dad..dfad5c492 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -980,6 +980,7 @@ namespace Humanizer public static string Ordinalize(this string numberString) { } public static string Ordinalize(this string numberString, Humanizer.GrammaticalGender gender) { } public static string Ordinalize(this int number) { } + public static string Ordinalize(this int number, System.Globalization.CultureInfo culture) { } public static string Ordinalize(this int number, Humanizer.GrammaticalGender gender) { } } public enum Plurality From e4214a65b0397b4144c920d830ba03cf9b45eca5 Mon Sep 17 00:00:00 2001 From: Thijs Brobbel Date: Wed, 3 Apr 2019 22:34:38 +0200 Subject: [PATCH 4/5] Add missing overloads, and associated tests. --- src/Humanizer.Tests.Shared/OrdinalizeTests.cs | 37 +++++++++++++++- src/Humanizer/OrdinalizeExtensions.cs | 43 ++++++++++++++++++- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/Humanizer.Tests.Shared/OrdinalizeTests.cs b/src/Humanizer.Tests.Shared/OrdinalizeTests.cs index 95383dfa7..56225f9b9 100644 --- a/src/Humanizer.Tests.Shared/OrdinalizeTests.cs +++ b/src/Humanizer.Tests.Shared/OrdinalizeTests.cs @@ -96,14 +96,47 @@ public void OrdinalizeStringGenderIsImmaterial(string number) var feminineOrdinalized = number.Ordinalize(GrammaticalGender.Feminine); Assert.Equal(masculineOrdinalized, feminineOrdinalized); } - + + [Theory] + [InlineData("en-US", "1", "1st")] + [InlineData("nl-NL", "1", "1e")] + public void OrdinalizeStringWithCultureOverridesCurrentCulture(string cultureName, string number, string ordinalized) + { + var culture = new CultureInfo(cultureName); + Assert.Equal(number.Ordinalize(culture), ordinalized); + } + [Theory] [InlineData("en-US", 1, "1st")] [InlineData("nl-NL", 1, "1e")] - public void OrdinalizeWithCultureOverridesCurrentCulture(string cultureName, int number, string ordinalized) + public void OrdinalizeNumberWithCultureOverridesCurrentCulture(string cultureName, int number, string ordinalized) { var culture = new CultureInfo(cultureName); Assert.Equal(number.Ordinalize(culture), ordinalized); } + + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(8)] + public void OrdinalizeNumberWithOverridenCultureGenderIsImmaterial(int number) + { + var culture = new CultureInfo("nl-NL"); + var masculineOrdinalized = number.Ordinalize(GrammaticalGender.Masculine, culture); + var feminineOrdinalized = number.Ordinalize(GrammaticalGender.Feminine, culture); + Assert.Equal(masculineOrdinalized, feminineOrdinalized); + } + + [Theory] + [InlineData("0")] + [InlineData("1")] + [InlineData("8")] + public void OrdinalizeStringWithOverridenGenderIsImmaterial(string number) + { + var culture = new CultureInfo("nl-NL"); + var masculineOrdinalized = number.Ordinalize(GrammaticalGender.Masculine, culture); + var feminineOrdinalized = number.Ordinalize(GrammaticalGender.Feminine, culture); + Assert.Equal(masculineOrdinalized, feminineOrdinalized); + } } } diff --git a/src/Humanizer/OrdinalizeExtensions.cs b/src/Humanizer/OrdinalizeExtensions.cs index 069a3052c..fc1aa36bb 100644 --- a/src/Humanizer/OrdinalizeExtensions.cs +++ b/src/Humanizer/OrdinalizeExtensions.cs @@ -18,6 +18,17 @@ public static string Ordinalize(this string numberString) return Configurator.Ordinalizer.Convert(int.Parse(numberString), numberString); } + /// + /// Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. + /// + /// The number, in string, to be ordinalized + /// Culture to use. If null, current thread's UI culture is used. + /// + public static string Ordinalize(this string numberString, CultureInfo culture) + { + return Configurator.Ordinalizers.ResolveForCulture(culture).Convert(int.Parse(numberString, culture), numberString); + } + /// /// Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. /// Gender for Brazilian Portuguese locale @@ -32,6 +43,21 @@ public static string Ordinalize(this string numberString, GrammaticalGender gend return Configurator.Ordinalizer.Convert(int.Parse(numberString), numberString, gender); } + /// + /// Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. + /// Gender for Brazilian Portuguese locale + /// "1".Ordinalize(GrammaticalGender.Masculine) -> "1º" + /// "1".Ordinalize(GrammaticalGender.Feminine) -> "1ª" + /// + /// The number, in string, to be ordinalized + /// The grammatical gender to use for output words + /// Culture to use. If null, current thread's UI culture is used. + /// + public static string Ordinalize(this string numberString, GrammaticalGender gender, CultureInfo culture) + { + return Configurator.Ordinalizers.ResolveForCulture(culture).Convert(int.Parse(numberString, culture), numberString, gender); + } + /// /// Turns a number into an ordinal number used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. /// @@ -66,5 +92,20 @@ public static string Ordinalize(this int number, GrammaticalGender gender) { return Configurator.Ordinalizer.Convert(number, number.ToString(CultureInfo.InvariantCulture), gender); } + + /// + /// Turns a number into an ordinal number used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. + /// Gender for Brazilian Portuguese locale + /// 1.Ordinalize(GrammaticalGender.Masculine) -> "1º" + /// 1.Ordinalize(GrammaticalGender.Feminine) -> "1ª" + /// + /// The number to be ordinalized + /// The grammatical gender to use for output words + /// Culture to use. If null, current thread's UI culture is used. + /// + public static string Ordinalize(this int number, GrammaticalGender gender, CultureInfo culture) + { + return Configurator.Ordinalizers.ResolveForCulture(culture).Convert(number, number.ToString(culture), gender); + } } -} \ No newline at end of file +} From 06bc103e3bc934eab48c51988f8e71e6f47ca8e2 Mon Sep 17 00:00:00 2001 From: Thijs Brobbel Date: Wed, 3 Apr 2019 23:48:22 +0200 Subject: [PATCH 5/5] Add missing overloads in the public api.txt, added in previous commit. --- .../PublicApiApprovalTest.approve_public_api.approved.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index dfad5c492..0702c7af6 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -978,10 +978,13 @@ namespace Humanizer public class static OrdinalizeExtensions { public static string Ordinalize(this string numberString) { } + public static string Ordinalize(this string numberString, System.Globalization.CultureInfo culture) { } public static string Ordinalize(this string numberString, Humanizer.GrammaticalGender gender) { } + public static string Ordinalize(this string numberString, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture) { } public static string Ordinalize(this int number) { } public static string Ordinalize(this int number, System.Globalization.CultureInfo culture) { } public static string Ordinalize(this int number, Humanizer.GrammaticalGender gender) { } + public static string Ordinalize(this int number, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture) { } } public enum Plurality {