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

Support for Serbian language, ToWords() #417

Merged
merged 2 commits into from
May 24, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@
<Compile Include="Localisation\sl\NumberToWordsTests.cs" />
<Compile Include="Localisation\sl\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sr-Latn\DateHumanizeTests.cs" />
<Compile Include="Localisation\sr-Latn\NumberToWordsTest.cs" />
<Compile Include="Localisation\sr-Latn\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sr\DateHumanizeTests.cs" />
<Compile Include="Localisation\sr\NumberToWordsTest.cs" />
<Compile Include="Localisation\sr\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sv\DateHumanizeTests.cs" />
<Compile Include="Localisation\sv\TimeSpanHumanizeTests.cs" />
Expand Down
55 changes: 55 additions & 0 deletions src/Humanizer.Tests/Localisation/sr-Latn/NumberToWordsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.sr_Latn
{
public class NumberToWordsTest : AmbientCulture
{
public NumberToWordsTest() : base("sr-Latn") { }

[Theory]
[InlineData(0, "nula")]
[InlineData(1, "jedan")]
[InlineData(2, "dva")]
[InlineData(3, "tri")]
[InlineData(4, "četiri")]
[InlineData(5, "pet")]
[InlineData(6, "šest")]
[InlineData(7, "sedam")]
[InlineData(8, "osam")]
[InlineData(9, "devet")]
[InlineData(10, "deset")]
[InlineData(20, "dvadeset")]
[InlineData(30, "trideset")]
[InlineData(40, "četrdeset")]
[InlineData(50, "petdeset")]
[InlineData(60, "šestdeset")]
[InlineData(70, "sedamdeset")]
[InlineData(80, "osamdeset")]
[InlineData(90, "devetdeset")]
[InlineData(100, "sto")]
[InlineData(200, "dvesto")]
[InlineData(1000, "hiljadu")]
[InlineData(10000, "deset hiljada")]
[InlineData(100000, "sto hiljada")]
[InlineData(1000000, "milion")]
[InlineData(10000000, "deset miliona")]
[InlineData(100000000, "sto miliona")]
[InlineData(1000000000, "milijarda")]
[InlineData(2000000000, "dve milijarde")]
[InlineData(15, "petnaest")]
[InlineData(43, "četrdeset tri")]
[InlineData(81, "osamdeset jedan")]
[InlineData(213, "dvesto trinaest")]
[InlineData(547, "petsto četrdeset sedam")]
public void ToWords(int number, string expected)
{
Assert.Equal(expected, number.ToWords());
}
}
}
55 changes: 55 additions & 0 deletions src/Humanizer.Tests/Localisation/sr/NumberToWordsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.sr
{
public class NumberToWordsTest : AmbientCulture
{
public NumberToWordsTest() : base("sr") { }

[Theory]
[InlineData(0, "нула")]
[InlineData(1, "један")]
[InlineData(2, "два")]
[InlineData(3, "три")]
[InlineData(4, "четири")]
[InlineData(5, "пет")]
[InlineData(6, "шест")]
[InlineData(7, "седам")]
[InlineData(8, "осам")]
[InlineData(9, "девет")]
[InlineData(10, "десет")]
[InlineData(20, "двадесет")]
[InlineData(30, "тридесет")]
[InlineData(40, "четрдесет")]
[InlineData(50, "петдесет")]
[InlineData(60, "шестдесет")]
[InlineData(70, "седамдесет")]
[InlineData(80, "осамдесет")]
[InlineData(90, "деветдесет")]
[InlineData(100, "сто")]
[InlineData(200, "двесто")]
[InlineData(1000, "хиљаду")]
[InlineData(10000, "десет хиљада")]
[InlineData(100000, "сто хиљада")]
[InlineData(1000000, "милион")]
[InlineData(10000000, "десет милиона")]
[InlineData(100000000, "сто милиона")]
[InlineData(1000000000, "милијарда")]
[InlineData(2000000000, "две милијарде")]
[InlineData(15, "петнаест")]
[InlineData(43, "четрдесет три")]
[InlineData(81, "осамдесет један")]
[InlineData(213, "двесто тринаест")]
[InlineData(547, "петсто четрдесет седам")]
public void ToWordsSr(int number, string expected)
{
Assert.Equal(expected, number.ToWords());
}
}
}
2 changes: 2 additions & 0 deletions src/Humanizer/Configuration/NumberToWordsConverterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal class NumberToWordsConverterRegistry : LocaliserRegistry<INumberToWords
Register("it", new ItalianNumberToWordsConverter());
Register("uz-Latn-UZ", new UzbekLatnNumberToWordConverter());
Register("uz-Cyrl-UZ", new UzbekCyrlNumberToWordConverter());
Register("sr", (culture) => new SerbianCyrlNumberToWordsConverter(culture));
Register("sr-Latn", (culture) => new SerbianNumberToWordsConverter(culture));
}
}
}
2 changes: 2 additions & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<Compile Include="Localisation\GrammaticalNumber\RussianGrammaticalNumberDetector.cs" />
<Compile Include="Localisation\NumberToWords\GenderedNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\GenderlessNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\SerbianCyrlNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\SerbianNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\SlovenianNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\DutchNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\DefaultNumberToWordsConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Humanizer.Localisation.NumberToWords
{
internal class SerbianCyrlNumberToWordsConverter : GenderlessNumberToWordsConverter
{
private static readonly string[] UnitsMap = { "нула", "један", "два", "три", "четири", "пет", "шест", "седам", "осам", "девет", "десет", "једанест", "дванаест", "тринаест", "четрнаест", "петнаест", "шестнаест", "седамнаест", "осамнаест", "деветнаест" };
private static readonly string[] TensMap = { "нула", "десет", "двадесет", "тридесет", "четрдесет", "петдесет", "шестдесет", "седамдесет", "осамдесет", "деветдесет" };

private readonly CultureInfo _culture;

public SerbianCyrlNumberToWordsConverter(CultureInfo culture)
{
_culture = culture;
}

public override string Convert(int number)
{
if (number == 0)
{
return "нула";
}

if (number < 0)
{
return string.Format("- {0}", Convert(-number));
}

var parts = new List<string>();
var billions = number / 1000000000;

if (billions > 0)
{
parts.Add(Part("милијарда", "две милијарде", "{0} милијарде", "{0} милијарда", billions));
number %= 1000000000;

if (number > 0)
{
parts.Add(" ");
}
}

var millions = number / 1000000;

if (millions > 0)
{
parts.Add(Part("милион", "два милиона", "{0} милиона", "{0} милиона", millions));
number %= 1000000;

if (number > 0)
{
parts.Add(" ");
}
}

var thousands = number / 1000;

if (thousands > 0)
{
parts.Add(Part("хиљаду", "две хиљаде", "{0} хиљаде", "{0} хиљада", thousands));
number %= 1000;

if (number > 0)
{
parts.Add(" ");
}
}

var hundreds = number / 100;

if (hundreds > 0)
{

parts.Add(Part("сто", "двесто", "{0}сто", "{0}сто", hundreds));
number %= 100;

if (number > 0)
{
parts.Add(" ");
}
}

if (number > 0)
{
if (number < 20)
{
parts.Add(UnitsMap[number]);
}
else
{
parts.Add(TensMap[number / 10]);

var units = number % 10;

if (units > 0)
{
parts.Add(string.Format(" {0}", UnitsMap[units]));
}
}
}

return string.Join("", parts);
}

public override string ConvertToOrdinal(int number)
{
//TODO: In progress
return number.ToString(_culture);
}

private string Part(string singular, string dual, string trialQuadral, string plural, int number)
{
switch (number)
{
case 1:
return singular;
case 2:
return dual;
case 3:
case 4:
return string.Format(trialQuadral, Convert(number));
default:
return string.Format(plural, Convert(number));
}
}
}
}
Loading