diff --git a/release_notes.md b/release_notes.md index 1ef033e67..f98af024c 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,4 +1,5 @@ ###In Development + - [#188](https://github.com/Mehdik/Humanizer/pull/188): Added Spanish ToOrdinalWords translations - [#166](https://github.com/MehdiK/Humanizer/pull/166): Added Dutch (NL) Number to words and ordinals [Commits](https://github.com/MehdiK/Humanizer/compare/v1.21.15...master) diff --git a/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs b/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs index 017dcad73..2a77b1a8a 100644 --- a/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs +++ b/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs @@ -47,5 +47,16 @@ public void ToWordsSpanish(int number, string expected) { Assert.Equal(expected, number.ToWords()); } + + [Theory] + [InlineData(1, "primero", null)] + [InlineData(2, "segundo", GrammaticalGender.Masculine)] + [InlineData(2, "segunda", GrammaticalGender.Feminine)] + [InlineData(2, "segundo", GrammaticalGender.Neuter)] + [InlineData(11, "once", null)] + public void ToOrdinalWordsSpanish(int number, string words, GrammaticalGender gender) + { + Assert.Equal(words, number.ToOrdinalWords(gender)); + } } -} +} \ No newline at end of file diff --git a/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs index bed48c1d8..b3880e6bb 100644 --- a/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs @@ -9,6 +9,20 @@ internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter private static readonly string[] UnitsMap = { "cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciséis", "diecisiete", "dieciocho", "diecinueve" }; private static readonly string[] TensMap = { "cero", "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa" }; + private static readonly Dictionary Ordinals = new Dictionary + { + {1, "primero"}, + {2, "segundo"}, + {3, "tercero"}, + {4, "quarto"}, + {5, "quinto"}, + {6, "sexto"}, + {7, "séptimo"}, + {8, "octavo"}, + {9, "noveno"}, + {10, "décimo"} + }; + public override string Convert(int number) { if (number == 0) @@ -21,34 +35,34 @@ public override string Convert(int number) if ((number / 1000000000) > 0) { - parts.Add(number/1000000000 == 1 + parts.Add(number / 1000000000 == 1 ? string.Format("mil millones") - : string.Format("{0} mil millones", Convert(number/1000000000))); + : string.Format("{0} mil millones", Convert(number / 1000000000))); number %= 1000000000; } if ((number / 1000000) > 0) { - parts.Add(number/1000000 == 1 + parts.Add(number / 1000000 == 1 ? string.Format("millón") - : string.Format("{0} millones", Convert(number/1000000))); + : string.Format("{0} millones", Convert(number / 1000000))); number %= 1000000; } if ((number / 1000) > 0) { - parts.Add(number/1000 == 1 + parts.Add(number / 1000 == 1 ? string.Format("mil") - : string.Format("{0} mil", Convert(number/1000))); + : string.Format("{0} mil", Convert(number / 1000))); number %= 1000; } if ((number / 100) > 0) { - parts.Add(number == 100 ? string.Format("cien") : HundredsMap[(number/100)]); + parts.Add(number == 100 ? string.Format("cien") : HundredsMap[(number / 100)]); number %= 100; } @@ -56,8 +70,7 @@ public override string Convert(int number) { if (number < 20) parts.Add(UnitsMap[number]); - else if (number > 20 && number < 30) - { + else if (number > 20 && number < 30) { var lastPart = TensMap[number / 10]; if ((number % 10) > 0) lastPart += string.Format(" {0}", UnitsMap[number % 10]); @@ -77,9 +90,15 @@ public override string Convert(int number) return string.Join(" ", parts.ToArray()); } - public override string ConvertToOrdinal(int number) + public override string ConvertToOrdinal(int number, GrammaticalGender gender = GrammaticalGender.Masculine) { - throw new NotImplementedException(); + string towords; + if (!Ordinals.TryGetValue(number, out towords)) + towords = Convert(number); + if (gender == GrammaticalGender.Feminine) + towords = towords.TrimEnd('o') + "a"; + + return towords; } } } \ No newline at end of file