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

Added pt-BR localization for CollectionFormatter #387

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,4 +1,5 @@
###In Development
- [#387](https://github.com/MehdiK/Humanizer/pull/387): Added pt-BR localization for CollectionFormatter.
- [#381](https://github.com/MehdiK/Humanizer/pull/381): Fixes trailing question mark reported in #378.

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.33.7...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 @@ -105,6 +105,7 @@
<Compile Include="Localisation\pl\DateHumanizeTests.cs" />
<Compile Include="Localisation\pl\NumberToWordsTests.cs" />
<Compile Include="Localisation\pl\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\pt-BR\CollectionFormatterTests.cs" />
<Compile Include="Localisation\pt-BR\NumberToWordsTests.cs" />
<Compile Include="Localisation\pt-BR\OrdinalizeTests.cs" />
<Compile Include="Localisation\ro-Ro\TimeSpanHumanizerTests.cs" />
Expand Down
47 changes: 47 additions & 0 deletions src/Humanizer.Tests/Localisation/pt-BR/CollectionFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using Xunit;
using Xunit.Extensions;

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

[Fact]
public void OneItem()
{
var collection = new List<int>(new int[] { 1 });
string humanized = "1";
Assert.Equal(humanized, collection.Humanize());
}

[Fact]
public void TwoItems()
{
var collection = new List<int>(new int[] { 1, 2 });
string humanized = "1 e 2";
Assert.Equal(humanized, collection.Humanize());
}

[Fact]
public void MoreThanTwoItems()
{
var collection = new List<int>(new int[] { 1, 2, 3 });
string humanized = "1, 2 e 3";
Assert.Equal(humanized, collection.Humanize());
}

[Fact]
public void MoreThanTwoItemsWithCustomSeparator()
{
var collection = new List<int>(new int[] { 1, 2, 3 });
string humanized = "1, 2 ou 3";
Assert.Equal(humanized, collection.Humanize("ou"));
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/Configuration/CollectionFormatterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public CollectionFormatterRegistry()
{
Register("en", new EnglishCollectionFormatter());
Register("it", new ItalianCollectionFormatter());
Register("pt-BR", new BrazilianPortugueseCollectionFormatter());
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Localisation\CollectionFormatters\EnglishCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\ItalianCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\ICollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\BrazilianPortugueseCollectionFormatter.cs" />
<Compile Include="Localisation\Formatters\SerbianFormatter.cs" />
<Compile Include="Localisation\Formatters\SlovenianFormatter.cs" />
<Compile Include="Configuration\LocaliserRegistry.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Humanizer.Localisation.CollectionFormatters
{
internal class BrazilianPortugueseCollectionFormatter : DefaultCollectionFormatter
{
public BrazilianPortugueseCollectionFormatter()
{
DefaultSeparator = "e";
}

public override string Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter, String separator)
{
if (collection == null)
throw new ArgumentException("collection");

var enumerable = collection as T[] ?? collection.ToArray();

int count = enumerable.Count();

if (count == 0)
return "";

if (count == 1)
return objectFormatter(enumerable.First());

return String.Format("{0} {1} {2}",
String.Join(", ", enumerable.Take(count - 1).Select(objectFormatter)),
separator,
objectFormatter(enumerable.Skip(count - 1).First()));
}
}
}