Fixed StringExtensions culture problem. Added CultureHelper.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent 5c59e9e57e
commit 764dc3790b

@ -0,0 +1,33 @@
using JetBrains.Annotations;
using Volo;
using Volo.Abp;
namespace System.Globalization
{
public static class CultureHelper
{
public static IDisposable Use([NotNull] string culture, string uiCulture = null)
{
Check.NotNull(culture, nameof(culture));
return Use(new CultureInfo(culture), uiCulture == null ? null : new CultureInfo(uiCulture));
}
public static IDisposable Use([NotNull] CultureInfo culture, CultureInfo uiCulture = null)
{
Check.NotNull(culture, nameof(culture));
var currentCulture = CultureInfo.CurrentCulture;
var currentUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentCulture = uiCulture ?? culture;
return new DisposeAction(() =>
{
CultureInfo.CurrentCulture = currentCulture;
CultureInfo.CurrentUICulture = currentUiCulture;
});
}
}
}

@ -6,8 +6,6 @@ using Volo.ExtensionMethods.Collections.Generic;
namespace Volo.ExtensionMethods
{
//TODO: Not working properly with cultures!
/// <summary>
/// Extension methods for String class.
/// </summary>
@ -16,15 +14,7 @@ namespace Volo.ExtensionMethods
/// <summary>
/// Adds a char to end of given string if it does not ends with the char.
/// </summary>
public static string EnsureEndsWith(this string str, char c)
{
return EnsureEndsWith(str, c, StringComparison.Ordinal);
}
/// <summary>
/// Adds a char to end of given string if it does not ends with the char.
/// </summary>
public static string EnsureEndsWith(this string str, char c, StringComparison comparisonType)
public static string EnsureEndsWith(this string str, char c, StringComparison comparisonType = StringComparison.Ordinal)
{
Check.NotNull(str, nameof(str));
@ -36,19 +26,10 @@ namespace Volo.ExtensionMethods
return str + c;
}
/// <summary>
/// Adds a char to beginning of given string if it does not starts with the char.
/// </summary>
public static string EnsureStartsWith(this string str, char c)
{
return EnsureStartsWith(str, c, StringComparison.Ordinal);
}
/// <summary>
/// Adds a char to beginning of given string if it does not starts with the char.
/// </summary>
public static string EnsureStartsWith(this string str, char c, StringComparison comparisonType)
public static string EnsureStartsWith(this string str, char c, StringComparison comparisonType = StringComparison.Ordinal)
{
Check.NotNull(str, nameof(str));
@ -239,8 +220,9 @@ namespace Volo.ExtensionMethods
/// Converts PascalCase string to camelCase string.
/// </summary>
/// <param name="str">String to convert</param>
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
/// <returns>camelCase of the string</returns>
public static string ToCamelCase(this string str)
public static string ToCamelCase(this string str, bool useCurrentCulture = false)
{
if (string.IsNullOrWhiteSpace(str))
{
@ -249,10 +231,10 @@ namespace Volo.ExtensionMethods
if (str.Length == 1)
{
return str.ToLower();
return useCurrentCulture ? str.ToLower() : str.ToLowerInvariant();
}
return char.ToLower(str[0]) + str.Substring(1);
return (useCurrentCulture ? char.ToLower(str[0]) : char.ToLowerInvariant(str[0])) + str.Substring(1);
}
/// <summary>
@ -260,14 +242,17 @@ namespace Volo.ExtensionMethods
/// Example: "ThisIsSampleSentence" is converted to "This is a sample sentence".
/// </summary>
/// <param name="str">String to convert.</param>
public static string ToSentenceCase(this string str)
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
public static string ToSentenceCase(this string str, bool useCurrentCulture = false)
{
if (string.IsNullOrWhiteSpace(str))
{
return str;
}
return Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLower(m.Value[1]));
return useCurrentCulture
? Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLower(m.Value[1]))
: Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLowerInvariant(m.Value[1]));
}
/// <summary>
@ -318,8 +303,9 @@ namespace Volo.ExtensionMethods
/// Converts camelCase string to PascalCase string.
/// </summary>
/// <param name="str">String to convert</param>
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
/// <returns>PascalCase of the string</returns>
public static string ToPascalCase(this string str)
public static string ToPascalCase(this string str, bool useCurrentCulture = false)
{
if (string.IsNullOrWhiteSpace(str))
{
@ -328,10 +314,10 @@ namespace Volo.ExtensionMethods
if (str.Length == 1)
{
return str.ToUpper();
return useCurrentCulture ? str.ToUpper() : str.ToUpperInvariant();
}
return char.ToUpper(str[0]) + str.Substring(1);
return (useCurrentCulture ? char.ToUpper(str[0]) : char.ToUpperInvariant(str[0])) + str.Substring(1);
}
/// <summary>

@ -1,16 +1,17 @@
using System.Globalization;
using System;
using System.Globalization;
using Shouldly;
using Xunit;
namespace Volo.ExtensionMethods
{
public class StringExtensions_Tests
public class StringExtensions_Tests : IDisposable
{
private readonly IDisposable _cultureScope;
public StringExtensions_Tests()
{
//TODO: Temporary workaround! See StringExtensions
CultureInfo.CurrentUICulture = new CultureInfo("en-US");
CultureInfo.CurrentCulture = new CultureInfo("en-US");
_cultureScope = CultureHelper.Use("en-US");
}
[Fact]
@ -21,11 +22,21 @@ namespace Volo.ExtensionMethods
"Test!".EnsureEndsWith('!').ShouldBe("Test!");
@"C:\test\folderName".EnsureEndsWith('\\').ShouldBe(@"C:\test\folderName\");
@"C:\test\folderName\".EnsureEndsWith('\\').ShouldBe(@"C:\test\folderName\");
"Sarı".EnsureEndsWith('ı').ShouldBe("Sarı");
//Case differences
"TurkeY".EnsureEndsWith('y').ShouldBe("TurkeYy");
}
[Fact]
public void EnsureEndsWith_CultureSpecific_Test()
{
using (CultureHelper.Use("tr-TR"))
{
"Kırmızı".EnsureEndsWith('I', StringComparison.CurrentCultureIgnoreCase).ShouldBe("Kırmızı");
}
}
[Fact]
public void EnsureStartsWith_Test()
{
@ -45,6 +56,15 @@ namespace Volo.ExtensionMethods
"istanbul".ToPascalCase().ShouldBe("Istanbul");
}
[Fact]
public void ToPascalCase_CurrentCulture_Test()
{
using (CultureHelper.Use("tr-TR"))
{
"istanbul".ToPascalCase(true).ShouldBe("İstanbul");
}
}
[Fact]
public void ToCamelCase_Test()
{
@ -173,5 +193,10 @@ namespace Volo.ExtensionMethods
MyValue1,
MyValue2
}
public void Dispose()
{
_cultureScope.Dispose();
}
}
}
Loading…
Cancel
Save