From 74b1f2e6f7394a0af0118e89789cc71c0e9e4c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Sat, 26 Apr 2014 15:45:10 +0200 Subject: [PATCH 1/6] Add Slovenian number to words. --- src/Humanizer.Tests/Humanizer.Tests.csproj | 1 + .../Localisation/sl/NumberToWordsTests.cs | 56 +++++++++++++ src/Humanizer/Humanizer.csproj | 1 + .../SlovenianNumberToWordsConverter.cs | 81 +++++++++++++++++++ src/Humanizer/NumberToWordsExtension.cs | 1 + 5 files changed, 140 insertions(+) create mode 100644 src/Humanizer.Tests/Localisation/sl/NumberToWordsTests.cs create mode 100644 src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index aa08c8c17..8c307a8a4 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -139,6 +139,7 @@ + diff --git a/src/Humanizer.Tests/Localisation/sl/NumberToWordsTests.cs b/src/Humanizer.Tests/Localisation/sl/NumberToWordsTests.cs new file mode 100644 index 000000000..bb9a2bb41 --- /dev/null +++ b/src/Humanizer.Tests/Localisation/sl/NumberToWordsTests.cs @@ -0,0 +1,56 @@ +using Xunit; +using Xunit.Extensions; + +namespace Humanizer.Tests.Localisation.sl +{ + public class NumberToWordsTests : AmbientCulture + { + public NumberToWordsTests() : base("sl-SI") { } + + [Theory] + [InlineData(0, "nič")] + [InlineData(1, "ena")] + [InlineData(2, "dva")] + [InlineData(3, "tri")] + [InlineData(4, "štiri")] + [InlineData(5, "pet")] + [InlineData(6, "šest")] + [InlineData(7, "sedem")] + [InlineData(8, "osem")] + [InlineData(9, "devet")] + [InlineData(10, "deset")] + [InlineData(20, "dvajset")] + [InlineData(30, "trideset")] + [InlineData(40, "štirideset")] + [InlineData(50, "petdeset")] + [InlineData(60, "šestdeset")] + [InlineData(70, "sedemdeset")] + [InlineData(80, "osemdeset")] + [InlineData(90, "devetdeset")] + [InlineData(100, "sto")] + [InlineData(200, "dvesto")] + [InlineData(1000, "tisoč")] + [InlineData(10000, "deset tisoč")] + [InlineData(100000, "sto tisoč")] + [InlineData(1000000, "milijon")] + [InlineData(10000000, "deset milijonov")] + [InlineData(100000000, "sto milijonov")] + [InlineData(1000000000, "milijarda")] + [InlineData(2000000000, "dve milijardi")] + [InlineData(122, "sto dvaindvajset")] + [InlineData(3501, "tri tisoč petsto ena")] + [InlineData(111, "sto enajst")] + [InlineData(1112, "tisoč sto dvanajst")] + [InlineData(11213, "enajst tisoč dvesto trinajst")] + [InlineData(121314, "sto enaindvajset tisoč tristo štirinajst")] + [InlineData(2132415, "dva milijona sto dvaintrideset tisoč štiristo petnajst")] + [InlineData(12345516, "dvanajst milijonov tristo petinštirideset tisoč petsto šestnajst")] + [InlineData(751633617, "sedemsto enainpetdeset milijonov šeststo triintrideset tisoč šeststo sedemnajst")] + [InlineData(1111111118, "milijarda sto enajst milijonov sto enajst tisoč sto osemnajst")] + [InlineData(-751633619, "minus sedemsto enainpetdeset milijonov šeststo triintrideset tisoč šeststo devetnajst")] + public void ToWords(int number, string expected) + { + Assert.Equal(expected, number.ToWords()); + } + } +} diff --git a/src/Humanizer/Humanizer.csproj b/src/Humanizer/Humanizer.csproj index c36a3239f..387b97437 100644 --- a/src/Humanizer/Humanizer.csproj +++ b/src/Humanizer/Humanizer.csproj @@ -66,6 +66,7 @@ + diff --git a/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs new file mode 100644 index 000000000..8dc51203b --- /dev/null +++ b/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs @@ -0,0 +1,81 @@ +using System.Collections.Generic; + +namespace Humanizer.Localisation.NumberToWords { + internal class SlovenianNumberToWordsConverter : DefaultNumberToWordsConverter { + private static readonly string[] UnitsMap = { "ni", "ena", "dva", "tri", "tiri", "pet", "est", "sedem", "osem", "devet", "deset", "enajst", "dvanajst", "trinajst", "tirinajst", "petnajst", "estnajst", "sedemnajst", "osemnajst", "devetnajst" }; + private static readonly string[] TensMap = { "ni", "deset", "dvajset", "trideset", "tirideset", "petdeset", "estdeset", "sedemdeset", "osemdeset", "devetdeset" }; + + public override string Convert(int number) { + if (number == 0) + return "ni"; + + if (number < 0) + return string.Format("minus {0}", Convert(-number)); + + var parts = new List(); + + var billions = number / 1000000000; + if (billions > 0) { + parts.Add(Part("milijarda", "dve milijardi", "{0} milijarde", "{0} milijard", billions)); + number %= 1000000000; + if (number > 0) + parts.Add(" "); + } + + var millions = number / 1000000; + if (millions > 0) { + parts.Add(Part("milijon", "dva milijona", "{0} milijone", "{0} milijonov", millions)); + number %= 1000000; + if (number > 0) + parts.Add(" "); + } + + var thousands = number / 1000; + if (thousands > 0) { + parts.Add(Part("tiso", "dva tiso", "{0} tiso", "{0} tiso", thousands)); + number %= 1000; + if (number > 0) + parts.Add(" "); + } + + var hundreds = number / 100; + if (hundreds > 0) { + parts.Add(Part("sto", "dvesto", "{0}sto", "{0}sto", hundreds)); + number %= 100; + if (number > 0) + parts.Add(" "); + } + + if (number > 0) { + if (number < 20) { + if (number > 1) + parts.Add(UnitsMap[number]); + else + parts.Add("ena"); + } + else { + var units = number % 10; + if (units > 0) + parts.Add(string.Format("{0}in", UnitsMap[units])); + + parts.Add(TensMap[number / 10]); + } + } + + return string.Join("", parts); + } + + private string Part(string singular, string dual, string trialQuadral, string plural, int number) { + if (number == 1) + return singular; + + else if (number == 2) + return dual; + + else if (number == 3 || number == 4) + return string.Format(trialQuadral, Convert(number)); + + return string.Format(plural, Convert(number)); + } + } +} \ No newline at end of file diff --git a/src/Humanizer/NumberToWordsExtension.cs b/src/Humanizer/NumberToWordsExtension.cs index ba2a5de2a..f0065a491 100644 --- a/src/Humanizer/NumberToWordsExtension.cs +++ b/src/Humanizer/NumberToWordsExtension.cs @@ -24,6 +24,7 @@ public static class NumberToWordsExtension {"nl", () => new DutchNumberToWordsConverter()}, {"he", () => new HebrewNumberToWordsConverter()}, {"de", () => new GermanNumberToWordsConverter()}, + {"sl", () => new SlovenianNumberToWordsConverter()} }; /// From 8b620a816b499e655818b69a8469cd22b708188a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Sat, 26 Apr 2014 16:44:40 +0200 Subject: [PATCH 2/6] Add #247 pr to release notes. --- release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/release_notes.md b/release_notes.md index 02757d80f..d7dfd40e7 100644 --- a/release_notes.md +++ b/release_notes.md @@ -3,6 +3,7 @@ - [#239](https://github.com/Mehdik/Humanizer/pull/239): Added Serbian localisation - [#241](https://github.com/Mehdik/Humanizer/pull/241): Added German ToWords localisation - [#244](https://github.com/MehdiK/Humanizer/pull/244): Added Slovenian localisation + - [#247](https://github.com/MehdiK/Humanizer/pull/244): Added Slovenian number to words localisation [Commits](https://github.com/MehdiK/Humanizer/compare/v1.24.1...master) From 05a887cea5b196d0df2cf6f371923220925102dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Sat, 26 Apr 2014 16:46:49 +0200 Subject: [PATCH 3/6] Fixed the pr number. --- release_notes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release_notes.md b/release_notes.md index d7dfd40e7..54a760007 100644 --- a/release_notes.md +++ b/release_notes.md @@ -3,7 +3,7 @@ - [#239](https://github.com/Mehdik/Humanizer/pull/239): Added Serbian localisation - [#241](https://github.com/Mehdik/Humanizer/pull/241): Added German ToWords localisation - [#244](https://github.com/MehdiK/Humanizer/pull/244): Added Slovenian localisation - - [#247](https://github.com/MehdiK/Humanizer/pull/244): Added Slovenian number to words localisation + - [#247](https://github.com/MehdiK/Humanizer/pull/24): Added Slovenian number to words localisation [Commits](https://github.com/MehdiK/Humanizer/compare/v1.24.1...master) From 982e2e61fb2ea644a77f13b34a2d7e7d7618732c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Sat, 26 Apr 2014 16:51:30 +0200 Subject: [PATCH 4/6] Fixed release notes. --- release_notes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release_notes.md b/release_notes.md index 54a760007..6345352e8 100644 --- a/release_notes.md +++ b/release_notes.md @@ -3,7 +3,7 @@ - [#239](https://github.com/Mehdik/Humanizer/pull/239): Added Serbian localisation - [#241](https://github.com/Mehdik/Humanizer/pull/241): Added German ToWords localisation - [#244](https://github.com/MehdiK/Humanizer/pull/244): Added Slovenian localisation - - [#247](https://github.com/MehdiK/Humanizer/pull/24): Added Slovenian number to words localisation + - [#247](https://github.com/MehdiK/Humanizer/pull/247): Added Slovenian number to words localisation [Commits](https://github.com/MehdiK/Humanizer/compare/v1.24.1...master) From e3d548bf6708f22a13e5273cbb6c2fc54560cfc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Sat, 26 Apr 2014 17:39:51 +0200 Subject: [PATCH 5/6] Tidy up: fixed curly braces and removed unneeded else statements. --- .../SlovenianNumberToWordsConverter.cs | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs index 8dc51203b..4a25ae1aa 100644 --- a/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs @@ -1,11 +1,14 @@ using System.Collections.Generic; -namespace Humanizer.Localisation.NumberToWords { - internal class SlovenianNumberToWordsConverter : DefaultNumberToWordsConverter { - private static readonly string[] UnitsMap = { "ni", "ena", "dva", "tri", "tiri", "pet", "est", "sedem", "osem", "devet", "deset", "enajst", "dvanajst", "trinajst", "tirinajst", "petnajst", "estnajst", "sedemnajst", "osemnajst", "devetnajst" }; - private static readonly string[] TensMap = { "ni", "deset", "dvajset", "trideset", "tirideset", "petdeset", "estdeset", "sedemdeset", "osemdeset", "devetdeset" }; +namespace Humanizer.Localisation.NumberToWords +{ + internal class SlovenianNumberToWordsConverter : DefaultNumberToWordsConverter + { + private static readonly string[] UnitsMap = {"ni", "ena", "dva", "tri", "tiri", "pet", "est", "sedem", "osem", "devet", "deset", "enajst", "dvanajst", "trinajst", "tirinajst", "petnajst", "estnajst", "sedemnajst", "osemnajst", "devetnajst"}; + private static readonly string[] TensMap = {"ni", "deset", "dvajset", "trideset", "tirideset", "petdeset", "estdeset", "sedemdeset", "osemdeset", "devetdeset"}; - public override string Convert(int number) { + public override string Convert(int number) + { if (number == 0) return "ni"; @@ -15,7 +18,8 @@ public override string Convert(int number) { var parts = new List(); var billions = number / 1000000000; - if (billions > 0) { + if (billions > 0) + { parts.Add(Part("milijarda", "dve milijardi", "{0} milijarde", "{0} milijard", billions)); number %= 1000000000; if (number > 0) @@ -23,7 +27,8 @@ public override string Convert(int number) { } var millions = number / 1000000; - if (millions > 0) { + if (millions > 0) + { parts.Add(Part("milijon", "dva milijona", "{0} milijone", "{0} milijonov", millions)); number %= 1000000; if (number > 0) @@ -31,7 +36,8 @@ public override string Convert(int number) { } var thousands = number / 1000; - if (thousands > 0) { + if (thousands > 0) + { parts.Add(Part("tiso", "dva tiso", "{0} tiso", "{0} tiso", thousands)); number %= 1000; if (number > 0) @@ -39,21 +45,25 @@ public override string Convert(int number) { } var hundreds = number / 100; - if (hundreds > 0) { + if (hundreds > 0) + { parts.Add(Part("sto", "dvesto", "{0}sto", "{0}sto", hundreds)); number %= 100; if (number > 0) parts.Add(" "); } - if (number > 0) { - if (number < 20) { + if (number > 0) + { + if (number < 20) + { if (number > 1) parts.Add(UnitsMap[number]); else parts.Add("ena"); } - else { + else + { var units = number % 10; if (units > 0) parts.Add(string.Format("{0}in", UnitsMap[units])); @@ -65,14 +75,15 @@ public override string Convert(int number) { return string.Join("", parts); } - private string Part(string singular, string dual, string trialQuadral, string plural, int number) { + private string Part(string singular, string dual, string trialQuadral, string plural, int number) + { if (number == 1) return singular; - else if (number == 2) + if (number == 2) return dual; - else if (number == 3 || number == 4) + if (number == 3 || number == 4) return string.Format(trialQuadral, Convert(number)); return string.Format(plural, Convert(number)); From 9e9d196f0d3991e778e5d5f589d7447a24b0a0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Sun, 27 Apr 2014 00:42:15 +0200 Subject: [PATCH 6/6] Changed encoding for NumberToWords file. --- .../NumberToWords/SlovenianNumberToWordsConverter.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs index 4a25ae1aa..743adf90f 100644 --- a/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs @@ -1,16 +1,16 @@ -using System.Collections.Generic; +using System.Collections.Generic; namespace Humanizer.Localisation.NumberToWords { internal class SlovenianNumberToWordsConverter : DefaultNumberToWordsConverter { - private static readonly string[] UnitsMap = {"ni", "ena", "dva", "tri", "tiri", "pet", "est", "sedem", "osem", "devet", "deset", "enajst", "dvanajst", "trinajst", "tirinajst", "petnajst", "estnajst", "sedemnajst", "osemnajst", "devetnajst"}; - private static readonly string[] TensMap = {"ni", "deset", "dvajset", "trideset", "tirideset", "petdeset", "estdeset", "sedemdeset", "osemdeset", "devetdeset"}; + private static readonly string[] UnitsMap = {"nič", "ena", "dva", "tri", "štiri", "pet", "šest", "sedem", "osem", "devet", "deset", "enajst", "dvanajst", "trinajst", "štirinajst", "petnajst", "šestnajst", "sedemnajst", "osemnajst", "devetnajst"}; + private static readonly string[] TensMap = {"nič", "deset", "dvajset", "trideset", "štirideset", "petdeset", "šestdeset", "sedemdeset", "osemdeset", "devetdeset"}; public override string Convert(int number) { if (number == 0) - return "ni"; + return "nič"; if (number < 0) return string.Format("minus {0}", Convert(-number)); @@ -38,7 +38,7 @@ public override string Convert(int number) var thousands = number / 1000; if (thousands > 0) { - parts.Add(Part("tiso", "dva tiso", "{0} tiso", "{0} tiso", thousands)); + parts.Add(Part("tisoč", "dva tisoč", "{0} tisoč", "{0} tisoč", thousands)); number %= 1000; if (number > 0) parts.Add(" ");