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 german to words localization #241

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
###In Development
- [#236](https://github.com/Mehdik/Humanizer/pull/236): Added Turkish localisation
- [#239](https://github.com/Mehdik/Humanizer/pull/239): Added Serbian localisation
- [#241](https://github.com/Mehdik/Humanizer/pull/241): Added German ToWords 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 @@ -92,6 +92,7 @@
<Compile Include="Localisation\cs\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\da\DateHumanizeTests.cs" />
<Compile Include="Localisation\da\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\de\NumberToWordsTests.cs" />
<Compile Include="Localisation\es\OrdinalizeTests.cs" />
<Compile Include="Localisation\fr-BE\DateHumanizeTests.cs" />
<Compile Include="Localisation\fr-BE\TimeSpanHumanizeTests.cs" />
Expand Down
56 changes: 56 additions & 0 deletions src/Humanizer.Tests/Localisation/de/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.de
{
public class NumberToWordsTests : AmbientCulture
{
public NumberToWordsTests() : base("de-DE") { }

[Theory]
[InlineData(0, "null")]
[InlineData(1, "eins")]
[InlineData(2, "zwei")]
[InlineData(3, "drei")]
[InlineData(4, "vier")]
[InlineData(5, "fünf")]
[InlineData(6, "sechs")]
[InlineData(7, "sieben")]
[InlineData(8, "acht")]
[InlineData(9, "neun")]
[InlineData(10, "zehn")]
[InlineData(20, "zwanzig")]
[InlineData(30, "dreißig")]
[InlineData(40, "vierzig")]
[InlineData(50, "fünfzig")]
[InlineData(60, "sechzig")]
[InlineData(70, "siebzig")]
[InlineData(80, "achtzig")]
[InlineData(90, "neunzig")]
[InlineData(100, "einhundert")]
[InlineData(200, "zweihundert")]
[InlineData(1000, "eintausend")]
[InlineData(10000, "zehntausend")]
[InlineData(100000, "einhunderttausend")]
[InlineData(1000000, "eine Million")]
[InlineData(10000000, "zehn Millionen")]
[InlineData(100000000, "einhundert Millionen")]
[InlineData(1000000000, "eine Milliarde")]
[InlineData(2000000000, "zwei Milliarden")]
[InlineData(122, "einhundertzweiundzwanzig")]
[InlineData(3501, "dreitausendfünfhunderteins")]
[InlineData(111, "einhundertelf")]
[InlineData(1112, "eintausendeinhundertzwölf")]
[InlineData(11213, "elftausendzweihundertdreizehn")]
[InlineData(121314, "einhunderteinundzwanzigtausenddreihundertvierzehn")]
[InlineData(2132415, "zwei Millionen einhundertzweiunddreißigtausendvierhundertfünfzehn")]
[InlineData(12345516, "zwölf Millionen dreihundertfünfundvierzigtausendfünfhundertsechzehn")]
[InlineData(751633617, "siebenhunderteinundfünfzig Millionen sechshundertdreiunddreißigtausendsechshundertsiebzehn")]
[InlineData(1111111118, "eine Milliarde einhundertelf Millionen einhundertelftausendeinhundertachtzehn")]
[InlineData(-751633619, "minus siebenhunderteinundfünfzig Millionen sechshundertdreiunddreißigtausendsechshundertneunzehn")]
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 @@ -67,6 +67,7 @@
<Compile Include="Localisation\GrammaticalNumber\RussianGrammaticalNumberDetector.cs" />
<Compile Include="Localisation\NumberToWords\DutchNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\DefaultNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\GermanNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\FarsiNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\ArabicNumberToWordsConverter.cs" />
<Compile Include="Bytes\ByteSize.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
{
internal class GermanNumberToWordsConverter : DefaultNumberToWordsConverter
{
private static readonly string[] UnitsMap = { "null", "ein", "zwei", "drei", "vier", "f�nf", "sechs", "sieben", "acht", "neun", "zehn", "elf", "zw�lf", "dreizehn", "vierzehn", "f�nfzehn", "sechzehn", "siebzehn", "achtzehn", "neunzehn" };
private static readonly string[] TensMap = { "null", "zehn", "zwanzig", "drei�ig", "vierzig", "f�nfzig", "sechzig", "siebzig", "achtzig", "neunzig" };

public override string Convert(int number)
{
if (number == 0)
return "null";

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("{0} Milliarden", "eine Milliarde", billions));
number %= 1000000000;
if (number > 0)
parts.Add(" ");
}

var millions = number / 1000000;
if (millions > 0)
{
parts.Add(Part("{0} Millionen", "eine Million", millions));
number %= 1000000;
if (number > 0)
parts.Add(" ");
}

var thousands = number / 1000;
if (thousands > 0)
{
parts.Add(Part("{0}tausend", "eintausend", thousands));
number %= 1000;
}

var hundreds = number / 100;
if (hundreds > 0)
{
parts.Add(Part("{0}hundert", "einhundert", hundreds));
number %= 100;
}

if (number > 0)
{
if (number < 20)
{
if (number > 1)
parts.Add(UnitsMap[number]);
else
parts.Add("eins");
}
else
{
var units = number % 10;
if (units > 0)
parts.Add(string.Format("{0}und", UnitsMap[units]));

parts.Add(TensMap[number / 10]);
}
}

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

private string Part(string pluralFormat, string singular, int number)
{
if (number == 1)
return singular;
return string.Format(pluralFormat, Convert(number));
}
}
}
3 changes: 2 additions & 1 deletion src/Humanizer/NumberToWordsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static class NumberToWordsExtension
{"ru", () => new RussianNumberToWordsConverter()},
{"fr", () => new FrenchNumberToWordsConverter()},
{"nl", () => new DutchNumberToWordsConverter()},
{"he", () => new HebrewNumberToWordsConverter()}
{"he", () => new HebrewNumberToWordsConverter()},
{"de", () => new GermanNumberToWordsConverter()},
};

/// <summary>
Expand Down