From 15c4e5d5d88199e67835aae460e85f31a4f37bd8 Mon Sep 17 00:00:00 2001 From: Marcin Nowacki Date: Thu, 10 Apr 2014 22:12:49 +0200 Subject: [PATCH 1/5] Added Polish translation for ToWords --- src/Humanizer.Tests/Humanizer.Tests.csproj | 1 + .../Localisation/pl/NumberToWordsTests.cs | 64 ++++++++++ src/Humanizer/Humanizer.csproj | 1 + .../PolishNumberToWordsConverter.cs | 115 ++++++++++++++++++ src/Humanizer/NumberToWordsExtension.cs | 7 +- 5 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 src/Humanizer.Tests/Localisation/pl/NumberToWordsTests.cs create mode 100644 src/Humanizer/Localisation/NumberToWords/PolishNumberToWordsConverter.cs diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index bdfac012a..51874e4eb 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -82,6 +82,7 @@ + diff --git a/src/Humanizer.Tests/Localisation/pl/NumberToWordsTests.cs b/src/Humanizer.Tests/Localisation/pl/NumberToWordsTests.cs new file mode 100644 index 000000000..d7527e05e --- /dev/null +++ b/src/Humanizer.Tests/Localisation/pl/NumberToWordsTests.cs @@ -0,0 +1,64 @@ +using Xunit; +using Xunit.Extensions; + +namespace Humanizer.Tests.Localisation.pl +{ + public class NumberToWordsTests : AmbientCulture + { + public NumberToWordsTests() : base("pl") { } + + [Theory] + [InlineData(0, "zero")] + [InlineData(1, "jeden")] + [InlineData(2, "dwa")] + [InlineData(3, "trzy")] + [InlineData(4, "cztery")] + [InlineData(5, "pięć")] + [InlineData(6, "sześć")] + [InlineData(7, "siedem")] + [InlineData(8, "osiem")] + [InlineData(9, "dziewięć")] + [InlineData(10, "dziesięć")] + [InlineData(11, "jedenaście")] + [InlineData(12, "dwanaście")] + [InlineData(13, "trzynaście")] + [InlineData(14, "czternaście")] + [InlineData(15, "piętnaście")] + [InlineData(16, "szesnaście")] + [InlineData(17, "siedemnaście")] + [InlineData(18, "osiemnaście")] + [InlineData(19, "dziewiętnaście")] + [InlineData(20, "dwadzieścia")] + [InlineData(30, "trzydzieści")] + [InlineData(40, "czterdzieści")] + [InlineData(50, "pięćdziesiąt")] + [InlineData(60, "sześćdziesiąt")] + [InlineData(70, "siedemdziesiąt")] + [InlineData(80, "osiemdziesiąt")] + [InlineData(90, "dziewięćdziesiąt")] + [InlineData(100, "sto")] + [InlineData(112, "sto dwanaście")] + [InlineData(128, "sto dwadzieścia osiem")] + [InlineData(1000, "tysiąc")] + [InlineData(2000, "dwa tysiące")] + [InlineData(5000, "pięć tysięcy")] + [InlineData(10000, "dziesięć tysięcy")] + [InlineData(20000, "dwadzieścia tysięcy")] + [InlineData(22000, "dwadzieścia dwa tysiące")] + [InlineData(25000, "dwadzieścia pięć tysięcy")] + [InlineData(100000, "sto tysięcy")] + [InlineData(500000, "pięćset tysięcy")] + [InlineData(1000000, "milion")] + [InlineData(2000000, "dwa miliony")] + [InlineData(5000000, "pięć milionów")] + [InlineData(1000000000, "miliard")] + [InlineData(2000000000, "dwa miliardy")] + [InlineData(1501001892, "miliard pięćset jeden milionów tysiąc osiemset dziewięćdziesiąt dwa")] + [InlineData(2147483647, "dwa miliardy sto czterdzieści siedem milionów czterysta osiemdziesiąt trzy tysiące sześćset czterdzieści siedem")] + [InlineData(-1501001892, "minus miliard pięćset jeden milionów tysiąc osiemset dziewięćdziesiąt dwa")] + public void ToWordsPolish(int number, string expected) + { + Assert.Equal(expected, number.ToWords()); + } + } +} diff --git a/src/Humanizer/Humanizer.csproj b/src/Humanizer/Humanizer.csproj index f2afacfe0..6e62714de 100644 --- a/src/Humanizer/Humanizer.csproj +++ b/src/Humanizer/Humanizer.csproj @@ -76,6 +76,7 @@ + diff --git a/src/Humanizer/Localisation/NumberToWords/PolishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/PolishNumberToWordsConverter.cs new file mode 100644 index 000000000..dc9e1018b --- /dev/null +++ b/src/Humanizer/Localisation/NumberToWords/PolishNumberToWordsConverter.cs @@ -0,0 +1,115 @@ +using System; +using System.Linq; +using System.Text; + +namespace Humanizer.Localisation.NumberToWords +{ + internal class PolishNumberToWordsConverter : DefaultNumberToWordsConverter + { + private enum Numeral + { + One = 1, + Thousand = 1000, + Million = 1000000,//10^6 + Miliard = 1000000000,//10^9 + } + + private static readonly string Negative = "minus"; + private static readonly string Zero = "zero"; + + private static string ConvertNumberUnderThousand(Numeral numeral, int number) + { + if (numeral != Numeral.One && number == 1) + return string.Empty; + + var result = new StringBuilder(); + + var hundreds = number / 100; + if (hundreds > 0) + { + var map = new[] { "", "sto", "dwieście", "trzysta", "czterysta", "pięćset", "sześćset", "siedemset", "osiemset", "dziewięćset" }; + result.AppendFormat(@"{0} ", map[hundreds]); + number = number % 100; + } + + var tens = number / 10; + if (tens > 1) + { + var map = new[] { "", "dziesięć", "dwadzieścia", "trzydzieści", "czterdzieści", "pięćdziesiąt", "sześćdziesiąt", "siedemdziesiąt", "osiemdziesiąt", "dziewięćdziesiąt" }; + result.AppendFormat(@"{0} ", map[tens]); + number = number % 10; + } + + if (number > 0) + { + var map = new[] { "zero", "jeden", "dwa", "trzy", "cztery", "pięć", "sześć", "siedem", "osiem", "dziewięć", "dziesięć", "jedenaście", "dwanaście", "trzynaście", "czternaście", "piętnaście", "szesnaście", "siedemnaście", "osiemnaście", "dziewiętnaście" }; + result.AppendFormat(@"{0} ", map[number]); + } + + return result.ToString(); + } + private static int GetMappingIndex(int number) + { + if (number == 1) + return 0; + + if (number > 1 && number < 5) + return 1;//denominator + + var tens = number / 10; + if (tens > 1) + { + var unity = number % 10; + if (unity > 1 && unity < 5) + return 1;//denominator + } + + return 2;//genitive + } + private static string GetSuffix(Numeral numeral, int num) + { + switch (numeral) + { + case Numeral.Miliard: + var miliard = new[] { "miliard", "miliardy", "miliardów" }; //one, denominator, genitive + return miliard[GetMappingIndex(num)]; + case Numeral.Million: + var million = new[] { "milion", "miliony", "milionów" }; //one, denominator, genitive + return million[GetMappingIndex(num)]; + case Numeral.Thousand: + var thousand = new[] { "tysiąc", "tysiące", "tysięcy" }; //one, denominator, genitive + return thousand[GetMappingIndex(num)]; + default: + return string.Empty; + } + } + + public override string Convert(int number) + { + if (number == 0) + return Zero; + + var result = new StringBuilder(); + + if (number < 0) + { + result.AppendFormat(@"{0} ", Negative); + number = Math.Abs(number); + } + + var numerals = ((Numeral[])Enum.GetValues(typeof(Numeral))).Reverse(); + foreach (var numeral in numerals) + { + var num = number / (int)numeral; + if (num > 0) + { + result.AppendFormat(@"{0}{1} ", ConvertNumberUnderThousand(numeral, num), GetSuffix(numeral, num)); + number %= (int)numeral; + } + } + + return result.ToString().Trim(); + } + + } +} diff --git a/src/Humanizer/NumberToWordsExtension.cs b/src/Humanizer/NumberToWordsExtension.cs index 3315f4142..05147a9fb 100644 --- a/src/Humanizer/NumberToWordsExtension.cs +++ b/src/Humanizer/NumberToWordsExtension.cs @@ -1,7 +1,7 @@ -using System; +using Humanizer.Localisation.NumberToWords; +using System; using System.Collections.Generic; using System.Globalization; -using Humanizer.Localisation.NumberToWords; namespace Humanizer { @@ -15,7 +15,8 @@ public static class NumberToWordsExtension { { "en", () => new EnglishNumberToWordsConverter() }, { "ar", () => new ArabicNumberToWordsConverter() }, - { "fa", () => new FarsiNumberToWordsConverter() } + { "fa", () => new FarsiNumberToWordsConverter() }, + { "pl", () => new PolishNumberToWordsConverter() } }; /// From e21c2383efb3228e846a1eb9e902fe5b5e8c7922 Mon Sep 17 00:00:00 2001 From: Marcin Nowacki Date: Thu, 10 Apr 2014 22:27:23 +0200 Subject: [PATCH 2/5] Added Polish localization test for Now and NoTime --- src/Humanizer.Tests/Localisation/pl/DateHumanizeTests.cs | 7 +++++++ .../Localisation/pl/TimeSpanHumanizeTests.cs | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Humanizer.Tests/Localisation/pl/DateHumanizeTests.cs b/src/Humanizer.Tests/Localisation/pl/DateHumanizeTests.cs index 0ca929fcb..0a9d09f37 100644 --- a/src/Humanizer.Tests/Localisation/pl/DateHumanizeTests.cs +++ b/src/Humanizer.Tests/Localisation/pl/DateHumanizeTests.cs @@ -1,4 +1,5 @@ using Humanizer.Localisation; +using Xunit; using Xunit.Extensions; namespace Humanizer.Tests.Localisation.pl @@ -164,5 +165,11 @@ public void YearsAgo(int years, string expected) { DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Past); } + + [Fact] + public void Now() + { + DateHumanize.Verify("teraz", 0, TimeUnit.Day, Tense.Past); + } } } diff --git a/src/Humanizer.Tests/Localisation/pl/TimeSpanHumanizeTests.cs b/src/Humanizer.Tests/Localisation/pl/TimeSpanHumanizeTests.cs index d535fe868..f26440b24 100644 --- a/src/Humanizer.Tests/Localisation/pl/TimeSpanHumanizeTests.cs +++ b/src/Humanizer.Tests/Localisation/pl/TimeSpanHumanizeTests.cs @@ -84,7 +84,13 @@ public void Days(int number, string expected) [InlineData(6, "6 tygodni")] public void Weeks(int number, string expected) { - Assert.Equal(expected, TimeSpan.FromDays(number*7).Humanize()); + Assert.Equal(expected, TimeSpan.FromDays(number * 7).Humanize()); + } + + [Fact] + public void NoTime() + { + Assert.Equal("brak czasu", TimeSpan.Zero.Humanize()); } } } From a3104f669faef2cc24c91268f223f5d5b4451f45 Mon Sep 17 00:00:00 2001 From: Marcin Nowacki Date: Thu, 10 Apr 2014 22:12:49 +0200 Subject: [PATCH 3/5] Added Polish translation for ToWords From aabf126cf951928e5b76570bdaf5b2fcded53832 Mon Sep 17 00:00:00 2001 From: Marcin Nowacki Date: Thu, 10 Apr 2014 22:27:23 +0200 Subject: [PATCH 4/5] Added Polish localization test for Now and NoTime From 7b14ed83d2d1310ec6be95902fe315876e2eb458 Mon Sep 17 00:00:00 2001 From: Marcin Nowacki Date: Fri, 11 Apr 2014 10:45:56 +0200 Subject: [PATCH 5/5] Added PR to the release notes --- release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/release_notes.md b/release_notes.md index 996175888..d342f8fa2 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,5 +1,6 @@ ###In Development - [#171](https://github.com/MehdiK/Humanizer/pull/171): T4-Template fix: Using EnglishNumberToWordsConverter instead of 'ToWords()' while dogfooding the template with the library. + - [#172](https://github.com/MehdiK/Humanizer/pull/172): Added Polish translation for ToWords [Commits](https://github.com/MehdiK/Humanizer/compare/v1.19.1...master)