From c22665fd2661bf202c09b34542af583c43949a90 Mon Sep 17 00:00:00 2001 From: Thomas Hunsaker Date: Sat, 12 Apr 2014 16:33:58 -0700 Subject: [PATCH 1/2] Updated Spanish ToOrdinalWords with gender --- release_notes.md | 1 + .../Localisation/es/NumberToWordsTests.cs | 21 ++++-- .../SpanishNumberToWordsConverter.cs | 68 +++++++++++-------- 3 files changed, 53 insertions(+), 37 deletions(-) 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..fc7b45c1e 100644 --- a/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs +++ b/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs @@ -1,10 +1,8 @@ using Xunit; using Xunit.Extensions; -namespace Humanizer.Tests.Localisation.es -{ - public class NumberToWordsTests : AmbientCulture - { +namespace Humanizer.Tests.Localisation.es { + public class NumberToWordsTests : AmbientCulture { public NumberToWordsTests() : base("es-ES") { } [Theory] @@ -43,9 +41,18 @@ public NumberToWordsTests() : base("es-ES") { } [InlineData(1999, "mil novecientos noventa y nueve")] [InlineData(2014, "dos mil catorce")] [InlineData(2048, "dos mil cuarenta y ocho")] - public void ToWordsSpanish(int number, string expected) - { + 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..ce03b7bad 100644 --- a/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs @@ -1,16 +1,27 @@ using System; using System.Collections.Generic; -namespace Humanizer.Localisation.NumberToWords -{ - internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter - { +namespace Humanizer.Localisation.NumberToWords { + internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter { private static readonly string[] HundredsMap = { "cero", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos", "setecientos", "ochocientos", "novecientos" }; 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" }; - public override string Convert(int number) + 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) return "cero"; @@ -19,53 +30,45 @@ public override string Convert(int number) var parts = new List(); - if ((number / 1000000000) > 0) - { - parts.Add(number/1000000000 == 1 + if ((number / 1000000000) > 0) { + 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 + if ((number / 1000000) > 0) { + 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 + if ((number / 1000) > 0) { + 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)]); + if ((number / 100) > 0) { + parts.Add(number == 100 ? string.Format("cien") : HundredsMap[(number / 100)]); number %= 100; } - if (number > 0) - { + if (number > 0) { 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]); parts.Add(lastPart); - } - else - { + } else { var lastPart = TensMap[number / 10]; if ((number % 10) > 0) lastPart += string.Format(" y {0}", UnitsMap[number % 10]); @@ -77,9 +80,14 @@ public override string Convert(int number) return string.Join(" ", parts.ToArray()); } - public override string ConvertToOrdinal(int number) - { - throw new NotImplementedException(); + public override string ConvertToOrdinal(int number, GrammaticalGender gender = GrammaticalGender.Masculine) { + 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 From 7b8eb6d2700663a78cfca29984a3ade033bd9a35 Mon Sep 17 00:00:00 2001 From: Thomas Hunsaker Date: Sat, 12 Apr 2014 16:39:35 -0700 Subject: [PATCH 2/2] Fixed curly brace formatting... --- .../Localisation/es/NumberToWordsTests.cs | 12 ++++--- .../SpanishNumberToWordsConverter.cs | 31 +++++++++++++------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs b/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs index fc7b45c1e..2a77b1a8a 100644 --- a/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs +++ b/src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs @@ -1,8 +1,10 @@ using Xunit; using Xunit.Extensions; -namespace Humanizer.Tests.Localisation.es { - public class NumberToWordsTests : AmbientCulture { +namespace Humanizer.Tests.Localisation.es +{ + public class NumberToWordsTests : AmbientCulture + { public NumberToWordsTests() : base("es-ES") { } [Theory] @@ -41,7 +43,8 @@ public NumberToWordsTests() : base("es-ES") { } [InlineData(1999, "mil novecientos noventa y nueve")] [InlineData(2014, "dos mil catorce")] [InlineData(2048, "dos mil cuarenta y ocho")] - public void ToWordsSpanish(int number, string expected) { + public void ToWordsSpanish(int number, string expected) + { Assert.Equal(expected, number.ToWords()); } @@ -51,7 +54,8 @@ public void ToWordsSpanish(int number, string expected) { [InlineData(2, "segunda", GrammaticalGender.Feminine)] [InlineData(2, "segundo", GrammaticalGender.Neuter)] [InlineData(11, "once", null)] - public void ToOrdinalWordsSpanish(int number, string words, GrammaticalGender gender) { + public void ToOrdinalWordsSpanish(int number, string words, GrammaticalGender gender) + { Assert.Equal(words, number.ToOrdinalWords(gender)); } } diff --git a/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs index ce03b7bad..b3880e6bb 100644 --- a/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; -namespace Humanizer.Localisation.NumberToWords { - internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter { +namespace Humanizer.Localisation.NumberToWords +{ + internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter + { private static readonly string[] HundredsMap = { "cero", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos", "setecientos", "ochocientos", "novecientos" }; 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" }; @@ -21,7 +23,8 @@ internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter { {10, "décimo"} }; - public override string Convert(int number) { + public override string Convert(int number) + { if (number == 0) return "cero"; @@ -30,7 +33,8 @@ public override string Convert(int number) { var parts = new List(); - if ((number / 1000000000) > 0) { + if ((number / 1000000000) > 0) + { parts.Add(number / 1000000000 == 1 ? string.Format("mil millones") : string.Format("{0} mil millones", Convert(number / 1000000000))); @@ -38,7 +42,8 @@ public override string Convert(int number) { number %= 1000000000; } - if ((number / 1000000) > 0) { + if ((number / 1000000) > 0) + { parts.Add(number / 1000000 == 1 ? string.Format("millón") : string.Format("{0} millones", Convert(number / 1000000))); @@ -46,7 +51,8 @@ public override string Convert(int number) { number %= 1000000; } - if ((number / 1000) > 0) { + if ((number / 1000) > 0) + { parts.Add(number / 1000 == 1 ? string.Format("mil") : string.Format("{0} mil", Convert(number / 1000))); @@ -54,12 +60,14 @@ public override string Convert(int number) { number %= 1000; } - if ((number / 100) > 0) { + if ((number / 100) > 0) + { parts.Add(number == 100 ? string.Format("cien") : HundredsMap[(number / 100)]); number %= 100; } - if (number > 0) { + if (number > 0) + { if (number < 20) parts.Add(UnitsMap[number]); else if (number > 20 && number < 30) { @@ -68,7 +76,9 @@ public override string Convert(int number) { lastPart += string.Format(" {0}", UnitsMap[number % 10]); parts.Add(lastPart); - } else { + } + else + { var lastPart = TensMap[number / 10]; if ((number % 10) > 0) lastPart += string.Format(" y {0}", UnitsMap[number % 10]); @@ -80,7 +90,8 @@ public override string Convert(int number) { return string.Join(" ", parts.ToArray()); } - public override string ConvertToOrdinal(int number, GrammaticalGender gender = GrammaticalGender.Masculine) { + public override string ConvertToOrdinal(int number, GrammaticalGender gender = GrammaticalGender.Masculine) + { string towords; if (!Ordinals.TryGetValue(number, out towords)) towords = Convert(number);