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 OrdinalizeExtensions to better accommodate localisations. #217

Merged
merged 4 commits into from
Apr 15, 2014
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
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,22 @@ You can also pass a second argument, `ShowQuantityAs`, to `ToQuantity` to specif

You can also call `Ordinalize` on a numeric string and achieve the same result: `"21".Ordinalize()` => `"21st"`

`Ordinalize` also supports grammatical gender for both forms.
You can pass an argument to `Ordinalize` to specify which gender the number should be outputted in.
The possible values are `GrammaticalGender.Masculine`, `GrammaticalGender.Feminine` and `GrammaticalGender.Neuter`:

```C#
// for Brazilian Portuguese locale
1.ToOrdinalWords(GrammaticalGender.Masculine) => "1º"
1.ToOrdinalWords(GrammaticalGender.Feminine) => "1ª"
1.ToOrdinalWords(GrammaticalGender.Neuter) => "1º"
"2".ToOrdinalWords(GrammaticalGender.Masculine) => "2º"
"2".ToOrdinalWords(GrammaticalGender.Feminine) => "2ª"
"2".ToOrdinalWords(GrammaticalGender.Neuter) => "2º"
```

Obviously this only applies to some cultures. For others passing gender in or not passing at all doesn't make any difference in the result.
Copy link
Member

Choose a reason for hiding this comment

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

We need unit tests to verify this assumption.


####<a id="underscore">Underscore</a>
`Underscore` separates the input words with underscore; e.g. `"SomeTitle".Underscore()` => `"some_title"`

Expand Down
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
###In Development
- [#217](https://github.com/Mehdik/Humanizer/pull/217): Changed OrdinalizeExtensions to better accommodate localisations. Added pt-BR and Spanish Ordinalize localisation.

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.22.1...master)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ public enum OnNoMatch
public class OrdinalizeExtensions
{
public string Ordinalize(string numberString) { }
public string Ordinalize(string numberString, Humanizer.GrammaticalGender gender) { }
public string Ordinalize(int number) { }
public string Ordinalize(int number, Humanizer.GrammaticalGender gender) { }
}

public enum Plurality
Expand Down
2 changes: 2 additions & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="Localisation\cs\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\da\DateHumanizeTests.cs" />
<Compile Include="Localisation\da\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\es\OrdinalizeTests.cs" />
<Compile Include="Localisation\fr-BE\DateHumanizeTests.cs" />
<Compile Include="Localisation\fr-BE\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\fr\DateHumanizeTests.cs" />
Expand All @@ -103,6 +104,7 @@
<Compile Include="Localisation\pl\NumberToWordsTests.cs" />
<Compile Include="Localisation\pl\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\pt-BR\NumberToWordsTests.cs" />
<Compile Include="Localisation\pt-BR\OrdinalizeTests.cs" />
<Compile Include="Localisation\ru-RU\NumberToWordsTests.cs" />
<Compile Include="Localisation\ru-RU\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sk\DateHumanizeTests.cs" />
Expand Down
91 changes: 91 additions & 0 deletions src/Humanizer.Tests/Localisation/es/OrdinalizeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.es
{
public class OrdinalizeTests : AmbientCulture
{
public OrdinalizeTests()
: base("es-ES")
{
}

[Theory]
[InlineData("0", "0")]
[InlineData("1", "1º")]
[InlineData("2", "2º")]
[InlineData("3", "3º")]
[InlineData("4", "4º")]
[InlineData("5", "5º")]
[InlineData("6", "6º")]
[InlineData("23", "23º")]
[InlineData("100", "100º")]
[InlineData("101", "101º")]
[InlineData("102", "102º")]
[InlineData("103", "103º")]
[InlineData("1001", "1001º")]
public void OrdinalizeString(string number, string ordinalized)
Copy link
Member

Choose a reason for hiding this comment

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

Rename this to Ordinalize please

Never mind. I see the reason now.

{
Assert.Equal(number.Ordinalize(GrammaticalGender.Masculine), ordinalized);
}

[Theory]
[InlineData("0", "0")]
[InlineData("1", "1ª")]
[InlineData("2", "2ª")]
[InlineData("3", "3ª")]
[InlineData("4", "4ª")]
[InlineData("5", "5ª")]
[InlineData("6", "6ª")]
[InlineData("23", "23ª")]
[InlineData("100", "100ª")]
[InlineData("101", "101ª")]
[InlineData("102", "102ª")]
[InlineData("103", "103ª")]
[InlineData("1001", "1001ª")]
public void OrdinalizeStringFeminine(string number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Feminine), ordinalized);
}

[Theory]
[InlineData(0, "0")]
[InlineData(1, "1º")]
[InlineData(2, "2º")]
[InlineData(3, "3º")]
[InlineData(4, "4º")]
[InlineData(5, "5º")]
[InlineData(6, "6º")]
[InlineData(10, "10º")]
[InlineData(23, "23º")]
[InlineData(100, "100º")]
[InlineData(101, "101º")]
[InlineData(102, "102º")]
[InlineData(103, "103º")]
[InlineData(1001, "1001º")]
public void OrdinalizeNumber(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Masculine), ordinalized);
}

[Theory]
[InlineData(0, "0")]
[InlineData(1, "1ª")]
[InlineData(2, "2ª")]
[InlineData(3, "3ª")]
[InlineData(4, "4ª")]
[InlineData(5, "5ª")]
[InlineData(6, "6ª")]
[InlineData(10, "10ª")]
[InlineData(23, "23ª")]
[InlineData(100, "100ª")]
[InlineData(101, "101ª")]
[InlineData(102, "102ª")]
[InlineData(103, "103ª")]
[InlineData(1001, "1001ª")]
public void OrdinalizeNumberFeminine(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Feminine), ordinalized);
}
}
}
91 changes: 91 additions & 0 deletions src/Humanizer.Tests/Localisation/pt-BR/OrdinalizeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.ptBR
{
public class OrdinalizeTests : AmbientCulture
{
public OrdinalizeTests()
: base("pt-BR")
{
}

[Theory]
[InlineData("0", "0")]
[InlineData("1", "1º")]
[InlineData("2", "2º")]
[InlineData("3", "3º")]
[InlineData("4", "4º")]
[InlineData("5", "5º")]
[InlineData("6", "6º")]
[InlineData("23", "23º")]
[InlineData("100", "100º")]
[InlineData("101", "101º")]
[InlineData("102", "102º")]
[InlineData("103", "103º")]
[InlineData("1001", "1001º")]
public void OrdinalizeString(string number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Masculine), ordinalized);
}

[Theory]
[InlineData("0", "0")]
[InlineData("1", "1ª")]
[InlineData("2", "2ª")]
[InlineData("3", "3ª")]
[InlineData("4", "4ª")]
[InlineData("5", "5ª")]
[InlineData("6", "6ª")]
[InlineData("23", "23ª")]
[InlineData("100", "100ª")]
[InlineData("101", "101ª")]
[InlineData("102", "102ª")]
[InlineData("103", "103ª")]
[InlineData("1001", "1001ª")]
public void OrdinalizeStringFeminine(string number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Feminine), ordinalized);
}

[Theory]
[InlineData(0, "0")]
[InlineData(1, "1º")]
[InlineData(2, "2º")]
[InlineData(3, "3º")]
[InlineData(4, "4º")]
[InlineData(5, "5º")]
[InlineData(6, "6º")]
[InlineData(10, "10º")]
[InlineData(23, "23º")]
[InlineData(100, "100º")]
[InlineData(101, "101º")]
[InlineData(102, "102º")]
[InlineData(103, "103º")]
[InlineData(1001, "1001º")]
public void OrdinalizeNumber(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Masculine), ordinalized);
}

[Theory]
[InlineData(0, "0")]
[InlineData(1, "1ª")]
[InlineData(2, "2ª")]
[InlineData(3, "3ª")]
[InlineData(4, "4ª")]
[InlineData(5, "5ª")]
[InlineData(6, "6ª")]
[InlineData(10, "10ª")]
[InlineData(23, "23ª")]
[InlineData(100, "100ª")]
[InlineData(101, "101ª")]
[InlineData(102, "102ª")]
[InlineData(103, "103ª")]
[InlineData(1001, "1001ª")]
public void OrdinalizeNumberFeminine(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Feminine), ordinalized);
}
}
}
53 changes: 50 additions & 3 deletions src/Humanizer.Tests/OrdinalizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

namespace Humanizer.Tests
{
public class OrdinalizeTests
public class OrdinalizeTests : AmbientCulture
{
public OrdinalizeTests()
: base("en-US")
{
}

[Theory]
[InlineData("0", "0th")]
[InlineData("1", "1st")]
Expand Down Expand Up @@ -39,6 +44,7 @@ public void OrdinalizeString(string number, string ordinalized)
Assert.Equal(number.Ordinalize(), ordinalized);
}

[Theory]
[InlineData(0, "0th")]
[InlineData(1, "1st")]
[InlineData(2, "2nd")]
Expand Down Expand Up @@ -67,10 +73,51 @@ public void OrdinalizeString(string number, string ordinalized)
[InlineData(110, "110th")]
[InlineData(1000, "1000th")]
[InlineData(1001, "1001st")]
[Theory]
public void OrdanizeNumber(int number, string ordinalized)
public void OrdinalizeNumber(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(), ordinalized);
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(8)]
[InlineData(9)]
[InlineData(10)]
[InlineData(11)]
[InlineData(12)]
[InlineData(23)]
[InlineData(24)]
[InlineData(100)]
[InlineData(101)]
[InlineData(102)]
[InlineData(1001)]
public void OrdinalizeNumberGenderIndifferent(int number)
{
string masculineOrdinalized = number.Ordinalize(GrammaticalGender.Masculine);
string feminineOrdinalized = number.Ordinalize(GrammaticalGender.Feminine);
Assert.Equal(masculineOrdinalized, feminineOrdinalized);
}

[Theory]
[InlineData("0")]
[InlineData("1")]
[InlineData("8")]
[InlineData("9")]
[InlineData("10")]
[InlineData("11")]
[InlineData("12")]
[InlineData("23")]
[InlineData("24")]
[InlineData("100")]
[InlineData("101")]
[InlineData("102")]
[InlineData("1001")]
public void OrdinalizeStringGenderIndifferent(string number)
{
string masculineOrdinalized = number.Ordinalize(GrammaticalGender.Masculine);
string feminineOrdinalized = number.Ordinalize(GrammaticalGender.Feminine);
Assert.Equal(masculineOrdinalized, feminineOrdinalized);
}
}
}
4 changes: 4 additions & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
<Compile Include="Localisation\NumberToWords\PolishNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\RussianNumberToWordsConverter.cs" />
<Compile Include="Localisation\Formatters\HebrewFormatter.cs" />
<Compile Include="Localisation\Ordinalizer\SpanishOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizer\DefaultOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizer\EnglishOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizer\BrazilianPortugueseOrdinalizer.cs" />
<Compile Include="Localisation\Tense.cs" />
<Compile Include="Localisation\NumberToWords\SpanishNumberToWordsConverter.cs" />
<Compile Include="TimeSpanHumanizeExtensions.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Humanizer.Localisation.Ordinalizer
{
internal class BrazilianPortugueseOrdinalizer : DefaultOrdinalizer
{
public override string Convert(int number, string numberString)
{
return Convert(number, numberString, GrammaticalGender.Masculine);
}

public override string Convert(int number, string numberString, GrammaticalGender gender)
{
// N/A in Portuguese
if (number == 0)
return "0";

if (gender == GrammaticalGender.Feminine)
return numberString + "ª";

return numberString + "º";
}
}
}
15 changes: 15 additions & 0 deletions src/Humanizer/Localisation/Ordinalizer/DefaultOrdinalizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Humanizer.Localisation.Ordinalizer
{
internal class DefaultOrdinalizer
{
public virtual string Convert(int number, string numberString, GrammaticalGender gender)
{
return Convert(number, numberString);
}

public virtual string Convert(int number, string numberString)
{
return numberString;
}
}
}
Loading