-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from DeanMilojevic/master
Support for Serbian language, ToWords()
- Loading branch information
Showing
7 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Humanizer.Tests/Localisation/sr-Latn/NumberToWordsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/Humanizer/Localisation/NumberToWords/SerbianCyrlNumberToWordsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.