Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Slovenian number to words. #247

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -139,6 +139,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 @@ -66,6 +66,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,81 @@
using System.Collections.Generic;

namespace Humanizer.Localisation.NumberToWords {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format the code so the curlies are on new lines

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;

else if (number == 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for these else's here

return dual;

else 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