Skip to content

Commit

Permalink
Merge branch 'slovenian-NumberToWords'
Browse files Browse the repository at this point in the history
  • Loading branch information
MehdiK committed Apr 27, 2014
2 parents 1820cf9 + 37d2513 commit 52c7852
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<Compile Include="Localisation\pt-BR\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sk\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sl\DateHumanizeTests.cs" />
<Compile Include="Localisation\sl\NumberToWordsTests.cs" />
<Compile Include="Localisation\sl\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sr-Latn\DateHumanizeTests.cs" />
<Compile Include="Localisation\sr-Latn\TimeSpanHumanizeTests.cs" />
Expand Down
56 changes: 56 additions & 0 deletions src/Humanizer.Tests/Localisation/sl/NumberToWordsTests.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="GrammaticalGender.cs" />
<Compile Include="Localisation\GrammaticalNumber\RussianGrammaticalNumber.cs" />
<Compile Include="Localisation\GrammaticalNumber\RussianGrammaticalNumberDetector.cs" />
<Compile Include="Localisation\NumberToWords\SlovenianNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\DutchNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\DefaultNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\GermanNumberToWordsConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string>();

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));
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/NumberToWordsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static class NumberToWordsExtension
{"nl", () => new DutchNumberToWordsConverter()},
{"he", () => new HebrewNumberToWordsConverter()},
{"de", () => new GermanNumberToWordsConverter()},
{"sl", () => new SlovenianNumberToWordsConverter()}
};

/// <summary>
Expand Down

0 comments on commit 52c7852

Please sign in to comment.