From 1f44fe07bc10f25fee9457d1840f47414754515c Mon Sep 17 00:00:00 2001 From: Dmitry Gokun Date: Sun, 29 Jun 2014 20:52:52 +0300 Subject: [PATCH 1/2] string.FormatWith: added support for the explicit culture parameter. --- readme.md | 8 ++++++++ ...ApiApprovalTest.approve_public_api.approved.txt | 1 + src/Humanizer.Tests/StringExtensionsTests.cs | 12 +++++++++++- src/Humanizer/StringExentions.cs | 14 +++++++++++++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 1ffae5b1c..1e5aa7447 100644 --- a/readme.md +++ b/readme.md @@ -157,6 +157,14 @@ This is an extension method based on `String.Format`, so exact rules applies to If `format` is null, it'll throw `ArgumentNullException`. If passed a fewer number for arguments, it'll throw `String.FormatException` exception. +You also can specify the culture to use explicitly as the first parameter for the `FormatWith()` method: + +```c# +"{0:N2}".FormatWith(new CultureInfo("ru-RU"), 6666.66) => "6 666,66" +``` + +If a culture is not specified, current thread's current culture is used. + ###Humanize Enums Calling `ToString` directly on enum members usually results in less than ideal output for users. The solution to this is usually to use `DescriptionAttribute` data annotation and then read that at runtime to get a more friendly output. That is a great solution; but more often than not we only need to put some space between words of an enum member - which is what `String.Humanize()` does well. For an enum like: diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index adcf751d7..145de1445 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -443,6 +443,7 @@ public class StringDehumanizeExtensions public class StringExentions { public string FormatWith(string format, object[] args) { } + public string FormatWith(string format, System.IFormatProvider provider, object[] args) { } } public class StringHumanizeExtensions diff --git a/src/Humanizer.Tests/StringExtensionsTests.cs b/src/Humanizer.Tests/StringExtensionsTests.cs index 811cb9205..101073dd3 100644 --- a/src/Humanizer.Tests/StringExtensionsTests.cs +++ b/src/Humanizer.Tests/StringExtensionsTests.cs @@ -1,5 +1,7 @@ using System; +using System.Globalization; using Xunit; +using Xunit.Extensions; namespace Humanizer.Tests { @@ -32,5 +34,13 @@ public void FormatCannotBeNull() string format = null; Assert.Throws(() => format.FormatWith(1, 2)); } + + [Theory] + [InlineData("en-US", "6,666.66")] + [InlineData("ru-RU", "6 666,66")] + public void CanSpecifyCultureExplicitly(string culture, string expected) + { + Assert.Equal(expected, "{0:N2}".FormatWith(new CultureInfo(culture), 6666.66)); + } } -} +} \ No newline at end of file diff --git a/src/Humanizer/StringExentions.cs b/src/Humanizer/StringExentions.cs index 9b557d7e5..7b119f4bd 100644 --- a/src/Humanizer/StringExentions.cs +++ b/src/Humanizer/StringExentions.cs @@ -8,7 +8,7 @@ namespace Humanizer public static class StringExentions { /// - /// Extension method to format string with passed arguments + /// Extension method to format string with passed arguments. Current thread's current culture is used /// /// string format /// arguments @@ -17,5 +17,17 @@ public static string FormatWith(this string format, params object[] args) { return String.Format(format, args); } + + /// + /// Extension method to format string with passed arguments using specified format provider (i.e. CultureInfo) + /// + /// string format + /// An object that supplies culture-specific formatting information + /// arguments + /// + public static string FormatWith(this string format, IFormatProvider provider, params object[] args) + { + return String.Format(provider, format, args); + } } } From 1643fdce78c514f6e6fb05a83a99845cf0b687fb Mon Sep 17 00:00:00 2001 From: Dmitry Gokun Date: Sun, 29 Jun 2014 20:56:39 +0300 Subject: [PATCH 2/2] Updated release notes with pull request #307 --- release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/release_notes.md b/release_notes.md index 0ca3b37e9..2d0ccf186 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,6 +1,7 @@ ###In Development - [#306](https://github.com/MehdiK/Humanizer/pull/306): Added Singularize/Pluralize overload without using obsolete plurality enum - [#303](https://github.com/MehdiK/Humanizer/pull/303): Added support for all integer types in ByteSize extensions + - [#307](https://github.com/MehdiK/Humanizer/pull/307): string.FormatWith: added support for the explicit culture parameter. [Commits](https://github.com/MehdiK/Humanizer/compare/v1.27.0...master)