From 061c11b0386073d920ec7af75928a519270b5efe Mon Sep 17 00:00:00 2001 From: Neil Boyd Date: Thu, 17 Mar 2022 07:35:11 +0100 Subject: [PATCH] fix for cultural variations --- ...provalTest.approve_public_api.approved.txt | 3 +-- .../Formatters/DefaultFormatter.cs | 26 +++++++------------ .../Formatters/IcelandicFormatter.cs | 6 ++--- .../Formatters/RomanianFormatter.cs | 2 +- .../Formatters/RussianFormatter.cs | 2 +- .../Formatters/UkrainianFormatter.cs | 2 +- 6 files changed, 17 insertions(+), 24 deletions(-) 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 ceac370bd..13881fe44 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -1945,9 +1945,8 @@ namespace Humanizer.Localisation.Formatters public virtual string DateHumanize_Never() { } public virtual string DateHumanize_Now() { } protected virtual string Format(string resourceKey) { } - protected virtual string Format(string resourceKey, int number, bool toWords = False) { } + protected virtual string Format(string resourceKey, int number, Humanizer.TimeSpanStyle timeSpanStyle = 0) { } protected virtual string GetResourceKey(string resourceKey, int number) { } - protected virtual string GetResourceKey(string resourceKey) { } public virtual string TimeSpanHumanize(Humanizer.Localisation.TimeUnit timeUnit, int unit, Humanizer.TimeSpanStyle timeSpanStyle = 0) { } public virtual string TimeSpanHumanize_Zero() { } public virtual string TimeUnitHumanize(Humanizer.Localisation.TimeUnit timeUnit) { } diff --git a/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs b/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs index 9980033ad..05bffe296 100644 --- a/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs +++ b/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs @@ -100,7 +100,7 @@ private string GetResourceForTimeSpan(TimeUnit unit, int count, TimeSpanStyle ti { var resourceKey = ResourceKeys.TimeSpanHumanize.GetResourceKey(unit, count, timeSpanStyle); - return count == 1 ? Format(resourceKey) : Format(resourceKey, count, timeSpanStyle == TimeSpanStyle.Words); + return count == 1 ? Format(resourceKey) : Format(resourceKey, count, timeSpanStyle); } /// @@ -111,7 +111,7 @@ private string GetResourceForTimeSpan(TimeUnit unit, int count, TimeSpanStyle ti /// If the resource not exists on the specified culture. protected virtual string Format(string resourceKey) { - var resourceString = Resources.GetResource(GetResourceKey(resourceKey), _culture); + var resourceString = Resources.GetResource(GetResourceKey(resourceKey, 0), _culture); if (string.IsNullOrEmpty(resourceString)) { @@ -126,19 +126,23 @@ protected virtual string Format(string resourceKey) /// /// The resource key. /// The number. - /// + /// Time span style /// /// If the resource not exists on the specified culture. - protected virtual string Format(string resourceKey, int number, bool toWords = false) + protected virtual string Format(string resourceKey, int number, TimeSpanStyle timeSpanStyle = TimeSpanStyle.Full) { - var resourceString = Resources.GetResource(GetResourceKey(resourceKey, number), _culture); + if (timeSpanStyle == TimeSpanStyle.Full || timeSpanStyle == TimeSpanStyle.Words) + { + resourceKey = GetResourceKey(resourceKey, number); + } + var resourceString = Resources.GetResource(resourceKey, _culture); if (string.IsNullOrEmpty(resourceString)) { throw new ArgumentException($"The resource object with key '{resourceKey}' was not found", nameof(resourceKey)); } - return toWords + return timeSpanStyle == TimeSpanStyle.Words ? resourceString.FormatWith(number.ToWords(_culture)) : resourceString.FormatWith(number); } @@ -153,15 +157,5 @@ protected virtual string GetResourceKey(string resourceKey, int number) { return resourceKey; } - - /// - /// - /// - /// - /// - protected virtual string GetResourceKey(string resourceKey) - { - return resourceKey; - } } } diff --git a/src/Humanizer/Localisation/Formatters/IcelandicFormatter.cs b/src/Humanizer/Localisation/Formatters/IcelandicFormatter.cs index 0eff18027..c91f23f48 100644 --- a/src/Humanizer/Localisation/Formatters/IcelandicFormatter.cs +++ b/src/Humanizer/Localisation/Formatters/IcelandicFormatter.cs @@ -17,13 +17,13 @@ public override string DataUnitHumanize(DataUnit dataUnit, double count, bool to { return base.DataUnitHumanize(dataUnit, count, toSymbol)?.TrimEnd('s'); } - protected override string Format(string resourceKey, int number, bool toWords = false) + protected override string Format(string resourceKey, int number, TimeSpanStyle timeSpanStyle = TimeSpanStyle.Full) { var resourceString = Resources.GetResource(GetResourceKey(resourceKey, number), _localCulture); if (string.IsNullOrEmpty(resourceString)) { - throw new ArgumentException($@"The resource object with key '{resourceKey}' was not found", nameof(resourceKey)); + throw new ArgumentException($"The resource object with key '{resourceKey}' was not found", nameof(resourceKey)); } var words = resourceString.Split(' '); @@ -35,7 +35,7 @@ var x when x.StartsWith("ár") => GrammaticalGender.Neuter, _ => GrammaticalGender.Feminine }; - return toWords ? + return timeSpanStyle == TimeSpanStyle.Words ? resourceString.FormatWith(number.ToWords(unitGender, _localCulture)) : resourceString.FormatWith(number); } diff --git a/src/Humanizer/Localisation/Formatters/RomanianFormatter.cs b/src/Humanizer/Localisation/Formatters/RomanianFormatter.cs index df46857b9..cc7fd2556 100644 --- a/src/Humanizer/Localisation/Formatters/RomanianFormatter.cs +++ b/src/Humanizer/Localisation/Formatters/RomanianFormatter.cs @@ -21,7 +21,7 @@ public RomanianFormatter() _romanianCulture = new CultureInfo(RomanianCultureCode); } - protected override string Format(string resourceKey, int number, bool toWords = false) + protected override string Format(string resourceKey, int number, TimeSpanStyle timeSpanStyle = TimeSpanStyle.Full) { var format = Resources.GetResource(GetResourceKey(resourceKey, number), _romanianCulture); var preposition = ShouldUsePreposition(number) diff --git a/src/Humanizer/Localisation/Formatters/RussianFormatter.cs b/src/Humanizer/Localisation/Formatters/RussianFormatter.cs index ceb4e0c56..964c1ac7c 100644 --- a/src/Humanizer/Localisation/Formatters/RussianFormatter.cs +++ b/src/Humanizer/Localisation/Formatters/RussianFormatter.cs @@ -16,7 +16,7 @@ protected override string GetResourceKey(string resourceKey, int number) return resourceKey + suffix; } - private string GetSuffix(RussianGrammaticalNumber grammaticalNumber) + private static string GetSuffix(RussianGrammaticalNumber grammaticalNumber) { if (grammaticalNumber == RussianGrammaticalNumber.Singular) { diff --git a/src/Humanizer/Localisation/Formatters/UkrainianFormatter.cs b/src/Humanizer/Localisation/Formatters/UkrainianFormatter.cs index af0cd37bc..4540fc825 100644 --- a/src/Humanizer/Localisation/Formatters/UkrainianFormatter.cs +++ b/src/Humanizer/Localisation/Formatters/UkrainianFormatter.cs @@ -16,7 +16,7 @@ protected override string GetResourceKey(string resourceKey, int number) return resourceKey + suffix; } - private string GetSuffix(RussianGrammaticalNumber grammaticalNumber) + private static string GetSuffix(RussianGrammaticalNumber grammaticalNumber) { if (grammaticalNumber == RussianGrammaticalNumber.Singular) {