From a6d0f96be76d9fa8fdd613643f5efd9bce2f78a8 Mon Sep 17 00:00:00 2001 From: Mahmoud Ali Date: Thu, 26 Feb 2015 00:27:20 -0400 Subject: [PATCH 1/2] Added pt-BR localization for CollectionFormatter --- src/Humanizer.Tests/Humanizer.Tests.csproj | 1 + .../pt-BR/CollectionFormatterTests.cs | 47 +++++++++++++++++++ .../CollectionFormatterRegistry.cs | 1 + src/Humanizer/Humanizer.csproj | 1 + .../BrazilianPortugueseCollectionFormatter.cs | 35 ++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 src/Humanizer.Tests/Localisation/pt-BR/CollectionFormatterTests.cs create mode 100644 src/Humanizer/Localisation/CollectionFormatters/BrazilianPortugueseCollectionFormatter.cs diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index c712a21ac..311a8e3cc 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -105,6 +105,7 @@ + diff --git a/src/Humanizer.Tests/Localisation/pt-BR/CollectionFormatterTests.cs b/src/Humanizer.Tests/Localisation/pt-BR/CollectionFormatterTests.cs new file mode 100644 index 000000000..901b390d2 --- /dev/null +++ b/src/Humanizer.Tests/Localisation/pt-BR/CollectionFormatterTests.cs @@ -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(new int[] { 1 }); + string humanized = "1"; + Assert.Equal(humanized, collection.Humanize()); + } + + [Fact] + public void TwoItems() + { + var collection = new List(new int[] { 1, 2 }); + string humanized = "1 e 2"; + Assert.Equal(humanized, collection.Humanize()); + } + + [Fact] + public void MoreThanTwoItems() + { + var collection = new List(new int[] { 1, 2, 3 }); + string humanized = "1, 2 e 3"; + Assert.Equal(humanized, collection.Humanize()); + } + + [Fact] + public void MoreThanTwoItemsWithCustomSeparator() + { + var collection = new List(new int[] { 1, 2, 3 }); + string humanized = "1, 2 ou 3"; + Assert.Equal(humanized, collection.Humanize("ou")); + } + } +} \ No newline at end of file diff --git a/src/Humanizer/Configuration/CollectionFormatterRegistry.cs b/src/Humanizer/Configuration/CollectionFormatterRegistry.cs index 21b2ca376..194a2fadf 100644 --- a/src/Humanizer/Configuration/CollectionFormatterRegistry.cs +++ b/src/Humanizer/Configuration/CollectionFormatterRegistry.cs @@ -9,6 +9,7 @@ public CollectionFormatterRegistry() { Register("en", new EnglishCollectionFormatter()); Register("it", new ItalianCollectionFormatter()); + Register("pt-BR", new BrazilianPortugueseCollectionFormatter()); } } } diff --git a/src/Humanizer/Humanizer.csproj b/src/Humanizer/Humanizer.csproj index 680d88bbd..c09d04e1e 100644 --- a/src/Humanizer/Humanizer.csproj +++ b/src/Humanizer/Humanizer.csproj @@ -56,6 +56,7 @@ + diff --git a/src/Humanizer/Localisation/CollectionFormatters/BrazilianPortugueseCollectionFormatter.cs b/src/Humanizer/Localisation/CollectionFormatters/BrazilianPortugueseCollectionFormatter.cs new file mode 100644 index 000000000..78760bff7 --- /dev/null +++ b/src/Humanizer/Localisation/CollectionFormatters/BrazilianPortugueseCollectionFormatter.cs @@ -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(IEnumerable collection, Func 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())); + } + } +} \ No newline at end of file From 74636f5161a3274527c08f4722a99ec0194b0e79 Mon Sep 17 00:00:00 2001 From: Mahmoud Ali Date: Thu, 26 Feb 2015 00:31:14 -0400 Subject: [PATCH 2/2] Added PR #387 in release notes --- release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/release_notes.md b/release_notes.md index e7cf714a0..cd4124a40 100644 --- a/release_notes.md +++ b/release_notes.md @@ -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)