diff --git a/release_notes.md b/release_notes.md index 02757d80f..6345352e8 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/247): Added Slovenian number to words localisation [Commits](https://github.com/MehdiK/Humanizer/compare/v1.24.1...master) 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..743adf90f --- /dev/null +++ b/src/Humanizer/Localisation/NumberToWords/SlovenianNumberToWordsConverter.cs @@ -0,0 +1,92 @@ +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; + + if (number == 2) + return dual; + + 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()} }; ///