diff --git a/src/Humanizer/Transformer/ICulturedStringTransformer.cs b/src/Humanizer/Transformer/ICulturedStringTransformer.cs new file mode 100644 index 000000000..40763188f --- /dev/null +++ b/src/Humanizer/Transformer/ICulturedStringTransformer.cs @@ -0,0 +1,18 @@ +using System.Globalization; + +namespace Humanizer +{ + /// + /// Can transform a string with the given culture + /// + public interface ICulturedStringTransformer : IStringTransformer + { + /// + /// Transform the input + /// + /// String to be transformed + /// The culture + /// + string Transform(string input, CultureInfo culture); + } +} diff --git a/src/Humanizer/Transformer/To.cs b/src/Humanizer/Transformer/To.cs index ad24f88f6..b5631ab54 100644 --- a/src/Humanizer/Transformer/To.cs +++ b/src/Humanizer/Transformer/To.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Globalization; +using System.Linq; namespace Humanizer { @@ -18,13 +19,25 @@ public static string Transform(this string input, params IStringTransformer[] tr return transformers.Aggregate(input, (current, stringTransformer) => stringTransformer.Transform(current)); } + /// + /// Transforms a string using the provided transformers. Transformations are applied in the provided order. + /// + /// + /// + /// + /// + public static string Transform(this string input, CultureInfo culture, params ICulturedStringTransformer[] transformers) + { + return transformers.Aggregate(input, (current, stringTransformer) => stringTransformer.Transform(current, culture)); + } + /// /// Changes string to title case /// /// /// "INvalid caSEs arE corrected" -> "Invalid Cases Are Corrected" /// - public static IStringTransformer TitleCase + public static ICulturedStringTransformer TitleCase { get { @@ -38,7 +51,7 @@ public static IStringTransformer TitleCase /// /// "Sentence casing" -> "sentence casing" /// - public static IStringTransformer LowerCase + public static ICulturedStringTransformer LowerCase { get { @@ -52,7 +65,7 @@ public static IStringTransformer LowerCase /// /// "lower case statement" -> "LOWER CASE STATEMENT" /// - public static IStringTransformer UpperCase + public static ICulturedStringTransformer UpperCase { get { @@ -66,7 +79,7 @@ public static IStringTransformer UpperCase /// /// "lower case statement" -> "Lower case statement" /// - public static IStringTransformer SentenceCase + public static ICulturedStringTransformer SentenceCase { get { diff --git a/src/Humanizer/Transformer/ToLowerCase.cs b/src/Humanizer/Transformer/ToLowerCase.cs index c6ba8043f..c44605651 100644 --- a/src/Humanizer/Transformer/ToLowerCase.cs +++ b/src/Humanizer/Transformer/ToLowerCase.cs @@ -2,11 +2,18 @@ namespace Humanizer { - internal class ToLowerCase : IStringTransformer + internal class ToLowerCase : ICulturedStringTransformer { public string Transform(string input) { - return CultureInfo.CurrentCulture.TextInfo.ToLower(input); + return Transform(input, null); + } + + public string Transform(string input, CultureInfo culture) + { + culture ??= CultureInfo.CurrentCulture; + + return culture.TextInfo.ToLower(input); } } } \ No newline at end of file diff --git a/src/Humanizer/Transformer/ToSentenceCase.cs b/src/Humanizer/Transformer/ToSentenceCase.cs index 1794a518d..927962a50 100644 --- a/src/Humanizer/Transformer/ToSentenceCase.cs +++ b/src/Humanizer/Transformer/ToSentenceCase.cs @@ -1,15 +1,24 @@ +using System.Globalization; + namespace Humanizer { - internal class ToSentenceCase : IStringTransformer + internal class ToSentenceCase : ICulturedStringTransformer { public string Transform(string input) { + return Transform(input, null); + } + + public string Transform(string input, CultureInfo culture) + { + culture ??= CultureInfo.CurrentCulture; + if (input.Length >= 1) { - return string.Concat(input.Substring(0, 1).ToUpper(), input.Substring(1)); + return culture.TextInfo.ToUpper(input[0]) + input.Substring(1); } - return input.ToUpper(); + return culture.TextInfo.ToUpper(input); } } } \ No newline at end of file diff --git a/src/Humanizer/Transformer/ToTitleCase.cs b/src/Humanizer/Transformer/ToTitleCase.cs index 92111d4dc..f0d34d438 100644 --- a/src/Humanizer/Transformer/ToTitleCase.cs +++ b/src/Humanizer/Transformer/ToTitleCase.cs @@ -1,19 +1,27 @@ -using System.Linq; +using System.Globalization; +using System.Linq; using System.Text.RegularExpressions; namespace Humanizer { - internal class ToTitleCase : IStringTransformer + internal class ToTitleCase : ICulturedStringTransformer { public string Transform(string input) { + return Transform(input, null); + } + + public string Transform(string input, CultureInfo culture) + { + culture ??= CultureInfo.CurrentCulture; + var result = input; var matches = Regex.Matches(input, @"(\w|[^\u0000-\u007F])+'?\w*"); foreach (Match word in matches) { if (!AllCapitals(word.Value)) { - result = ReplaceWithTitleCase(word, result); + result = ReplaceWithTitleCase(word, result, culture); } } @@ -25,10 +33,10 @@ private static bool AllCapitals(string input) return input.ToCharArray().All(char.IsUpper); } - private static string ReplaceWithTitleCase(Match word, string source) + private static string ReplaceWithTitleCase(Match word, string source, CultureInfo culture) { var wordToConvert = word.Value; - var replacement = char.ToUpper(wordToConvert[0]) + wordToConvert.Remove(0, 1).ToLower(); + var replacement = culture.TextInfo.ToUpper(wordToConvert[0]) + culture.TextInfo.ToLower(wordToConvert.Remove(0, 1)); return source.Substring(0, word.Index) + replacement + source.Substring(word.Index + word.Length); } } diff --git a/src/Humanizer/Transformer/ToUpperCase.cs b/src/Humanizer/Transformer/ToUpperCase.cs index d904bfa27..339bb53bb 100644 --- a/src/Humanizer/Transformer/ToUpperCase.cs +++ b/src/Humanizer/Transformer/ToUpperCase.cs @@ -1,10 +1,19 @@ +using System.Globalization; + namespace Humanizer { - internal class ToUpperCase : IStringTransformer + internal class ToUpperCase : ICulturedStringTransformer { public string Transform(string input) { - return input.ToUpper(); + return Transform(input, null); + } + + public string Transform(string input, CultureInfo culture) + { + culture ??= CultureInfo.CurrentCulture; + + return culture.TextInfo.ToUpper(input); } } } \ No newline at end of file