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

Changed the NumberToWords Converter to find the right converter by the language Name as well #174

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
###In Development
- [#171](https://github.com/MehdiK/Humanizer/pull/171): T4-Template fix: Using EnglishNumberToWordsConverter instead of 'ToWords()' while dogfooding the template with the library.
- [#174](https://github.com/MehdiK/Humanizer/pull/174): Changed the NumberToWords Converter to find the right converter by the language Name as well.
- [#165](https://github.com/MehdiK/Humanizer/pull/165): Added precision based `DateTime.Humanize` strategy
- [#155](https://github.com/MehdiK/Humanizer/pull/155): French and Belgian French localisation

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 @@ -85,6 +85,7 @@
<Compile Include="Localisation\fr\DateHumanizeTests.cs" />
<Compile Include="Localisation\fr\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\nb-NO\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\NumerToWordsFactoryTests.cs" />
<Compile Include="Localisation\pl\DateHumanizeTests.cs" />
<Compile Include="Localisation\pl\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\ru-RU\TimeSpanHumanizeTests.cs" />
Expand Down
49 changes: 49 additions & 0 deletions src/Humanizer.Tests/Localisation/NumerToWordsFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation
{
public class NumerToWordsFactoryTests
Copy link
Member

Choose a reason for hiding this comment

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

Please add some tests to verify the logic works for pt-PT and pt-BR.

{
[Theory]
[InlineData("1000000000", 1000000000)]
public void CanGetTwoLetterISOLanguageSpecificFactory(string notExpected, int number)
{
using (new AmbientCulture("ar"))
{
string result = number.ToWords();
Assert.NotEqual(notExpected, result);
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Please remove redundant spaces.

[Theory]
[InlineData("1000000000", 1000000000)]
public void CanGetRFCStandardLanguageSpecificFactory(string notExpected, int number)
{
using (new AmbientCulture("pt-BR"))
{
string result = number.ToWords();
Assert.NotEqual(notExpected, result);
}
}

[Theory]
[InlineData(1000000000)]
public void CanGetCorrectRFCStandardLanguageSpecificFactory(int number)
{
string resultPtBR;
using (new AmbientCulture("pt-BR"))
{
resultPtBR = number.ToWords();
}

string resultPtPT;
using (new AmbientCulture("pt-PT"))
{
resultPtPT = number.ToWords();
}

Assert.NotEqual(resultPtBR, resultPtPT);
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<AssemblyOriginatorKeyFile>Humanizer.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="Localisation\NumberToWords\BrazilianPortugueseNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\DefaultNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\FarsiNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\ArabicNumberToWordsConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Humanizer.Localisation.NumberToWords
{
internal class BrazilianPortugueseNumberToWordsConverter : DefaultNumberToWordsConverter
{
public override string Convert(int number)
{
return "not implemented";
Copy link
Member

Choose a reason for hiding this comment

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

This will upset Brazilian users I believe. We need to implement this first.

}
}
}
7 changes: 6 additions & 1 deletion src/Humanizer/NumberToWordsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static class NumberToWordsExtension
{
{ "en", () => new EnglishNumberToWordsConverter() },
{ "ar", () => new ArabicNumberToWordsConverter() },
{ "fa", () => new FarsiNumberToWordsConverter() }
{ "fa", () => new FarsiNumberToWordsConverter() },
{ "pt-BR", () => new BrazilianPortugueseNumberToWordsConverter() },
};

/// <summary>
Expand All @@ -33,6 +34,10 @@ private static INumberToWordsConverter Converter
get
{
Func<INumberToWordsConverter> converterFactory;

if (ConverterFactories.TryGetValue(CultureInfo.CurrentUICulture.Name, out converterFactory))
return converterFactory();

if (ConverterFactories.TryGetValue(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, out converterFactory))
return converterFactory();

Expand Down