From f7dae52fa229f197a3abc89804fde7a72e956a13 Mon Sep 17 00:00:00 2001 From: hakanoo Date: Tue, 22 Jun 2021 21:29:32 +0300 Subject: [PATCH 1/2] Localization of Tupleize method --- .../EnglishNumberToWordsConverter.cs | 35 +++++++++++++++++- .../GenderedNumberToWordsConverter.cs | 10 +++++ .../GenderlessNumberToWordsConverter.cs | 10 +++++ .../NumberToWords/INumberToWordsConverter.cs | 7 ++++ .../TurkishNumberToWordConverter.cs | 37 +++++++++++++++++++ src/Humanizer/NumberToWordsExtension.cs | 11 ++++++ 6 files changed, 109 insertions(+), 1 deletion(-) diff --git a/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs index 47f01e99c..5899a48e2 100644 --- a/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; namespace Humanizer.Localisation.NumberToWords @@ -166,5 +166,38 @@ private static bool ExceptionNumbersToWords(long number, out string words) { return OrdinalExceptions.TryGetValue(number, out words); } + + public override string ConvertToTuple(int number) + { + switch (number) + { + case 1: + return "single"; + case 2: + return "double"; + case 3: + return "triple"; + case 4: + return "quadruple"; + case 5: + return "quintuple"; + case 6: + return "sextuple"; + case 7: + return "septuple"; + case 8: + return "octuple"; + case 9: + return "nonuple"; + case 10: + return "decuple"; + case 100: + return "centuple"; + case 1000: + return "milluple"; + default: + return $"{number}-tuple"; + } + } } } diff --git a/src/Humanizer/Localisation/NumberToWords/GenderedNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/GenderedNumberToWordsConverter.cs index 4c63afb48..eaad67fc3 100644 --- a/src/Humanizer/Localisation/NumberToWords/GenderedNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/GenderedNumberToWordsConverter.cs @@ -57,5 +57,15 @@ public string ConvertToOrdinal(int number) /// /// public abstract string ConvertToOrdinal(int number, GrammaticalGender gender); + + /// + /// Converts integer to named tuple (e.g. 'single', 'double' etc.). + /// + /// + /// + public virtual string ConvertToTuple(int number) + { + return Convert(number); + } } } diff --git a/src/Humanizer/Localisation/NumberToWords/GenderlessNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/GenderlessNumberToWordsConverter.cs index 15f6989cc..ea7c4678e 100644 --- a/src/Humanizer/Localisation/NumberToWords/GenderlessNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/GenderlessNumberToWordsConverter.cs @@ -50,5 +50,15 @@ public string ConvertToOrdinal(int number, GrammaticalGender gender) { return ConvertToOrdinal(number); } + + /// + /// Converts integer to named tuple (e.g. 'single', 'double' etc.). + /// + /// + /// + public virtual string ConvertToTuple(int number) + { + return Convert(number); + } } } diff --git a/src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs index a8abb9469..52bc31268 100644 --- a/src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs @@ -43,5 +43,12 @@ public interface INumberToWordsConverter /// /// string ConvertToOrdinal(int number, GrammaticalGender gender); + + /// + /// Converts integer to named tuple (e.g. 'single', 'double' etc.). + /// + /// + /// + string ConvertToTuple(int number); } } diff --git a/src/Humanizer/Localisation/NumberToWords/TurkishNumberToWordConverter.cs b/src/Humanizer/Localisation/NumberToWords/TurkishNumberToWordConverter.cs index e36d3c942..f734194ed 100644 --- a/src/Humanizer/Localisation/NumberToWords/TurkishNumberToWordConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/TurkishNumberToWordConverter.cs @@ -19,6 +19,19 @@ internal class TurkishNumberToWordConverter : GenderlessNumberToWordsConverter {'a', "ıncı"}, }; + private static readonly Dictionary TupleSuffix = new Dictionary + { + {'ı', "lı"}, + {'i', "li"}, + {'u', "lu"}, + {'ü', "lü"}, + {'o', "lu"}, + {'ö', "lü"}, + {'e', "li"}, + {'a', "lı"}, + }; + + public override string Convert(long input) { var number = input; @@ -121,5 +134,29 @@ public override string ConvertToOrdinal(int number) return string.Format("{0}{1}", word, wordSuffix); } + + public override string ConvertToTuple(int number) + { + switch (number) + { + case 1: + return "tek"; + case 2: + return "çift"; + default: + var word = Convert(number); + var wordSuffix = string.Empty; + + for (var i = word.Length - 1; i >= 0; i--) + { + if (TupleSuffix.TryGetValue(word[i], out wordSuffix)) + { + break; + } + } + + return string.Format("{0}{1}", word, wordSuffix); + } + } } } diff --git a/src/Humanizer/NumberToWordsExtension.cs b/src/Humanizer/NumberToWordsExtension.cs index a6c6daa76..552aef519 100644 --- a/src/Humanizer/NumberToWordsExtension.cs +++ b/src/Humanizer/NumberToWordsExtension.cs @@ -118,5 +118,16 @@ public static string ToOrdinalWords(this int number, GrammaticalGender gender, C { return Configurator.GetNumberToWordsConverter(culture).ConvertToOrdinal(number, gender); } + + /// + /// 1.ToTuple() -> "single" + /// + /// Number to be turned to tuple + /// Culture to use. If null, current thread's UI culture is used. + /// + public static string ToTuple(this int number, CultureInfo culture = null) + { + return Configurator.GetNumberToWordsConverter(culture).ConvertToTuple(number); + } } } From 03560ba5d49aeef2dbcf76dae10fe7188b9dad75 Mon Sep 17 00:00:00 2001 From: hakanoo Date: Wed, 23 Jun 2021 12:52:09 +0300 Subject: [PATCH 2/2] Update PublicApiApprovalTest.approve_public_api.approved.txt --- .../PublicApiApprovalTest.approve_public_api.approved.txt | 2 ++ 1 file changed, 2 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 2a2dc31ec..53cfd1a1c 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -657,6 +657,7 @@ namespace Humanizer { public static string ToOrdinalWords(this int number, System.Globalization.CultureInfo culture = null) { } public static string ToOrdinalWords(this int number, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture = null) { } + public static string ToTuple(this int number, System.Globalization.CultureInfo culture = null) { } public static string ToWords(this int number, System.Globalization.CultureInfo culture = null) { } public static string ToWords(this int number, bool addAnd, System.Globalization.CultureInfo culture = null) { } public static string ToWords(this int number, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture = null) { } @@ -1921,6 +1922,7 @@ namespace Humanizer.Localisation.NumberToWords string Convert(long number, Humanizer.GrammaticalGender gender, bool addAnd = True); string ConvertToOrdinal(int number); string ConvertToOrdinal(int number, Humanizer.GrammaticalGender gender); + string ConvertToTuple(int number); } } namespace Humanizer.Localisation.Ordinalizers