diff --git a/src/Volo.Abp/System/Globalization/CultureHelper.cs b/src/Volo.Abp/System/Globalization/CultureHelper.cs
new file mode 100644
index 0000000000..dec84f768e
--- /dev/null
+++ b/src/Volo.Abp/System/Globalization/CultureHelper.cs
@@ -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;
+ });
+ }
+ }
+}
diff --git a/src/Volo.ExtensionMethods/Volo/ExtensionMethods/StringExtensions.cs b/src/Volo.ExtensionMethods/Volo/ExtensionMethods/StringExtensions.cs
index 7c0faf9da6..a95f31963a 100644
--- a/src/Volo.ExtensionMethods/Volo/ExtensionMethods/StringExtensions.cs
+++ b/src/Volo.ExtensionMethods/Volo/ExtensionMethods/StringExtensions.cs
@@ -6,8 +6,6 @@ using Volo.ExtensionMethods.Collections.Generic;
namespace Volo.ExtensionMethods
{
- //TODO: Not working properly with cultures!
-
///
/// Extension methods for String class.
///
@@ -16,15 +14,7 @@ namespace Volo.ExtensionMethods
///
/// Adds a char to end of given string if it does not ends with the char.
///
- public static string EnsureEndsWith(this string str, char c)
- {
- return EnsureEndsWith(str, c, StringComparison.Ordinal);
- }
-
- ///
- /// Adds a char to end of given string if it does not ends with the char.
- ///
- 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;
}
-
- ///
- /// Adds a char to beginning of given string if it does not starts with the char.
- ///
- public static string EnsureStartsWith(this string str, char c)
- {
- return EnsureStartsWith(str, c, StringComparison.Ordinal);
- }
-
///
/// Adds a char to beginning of given string if it does not starts with the char.
///
- 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.
///
/// String to convert
+ /// set true to use current culture. Otherwise, invariant culture will be used.
/// camelCase of the string
- 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);
}
///
@@ -260,14 +242,17 @@ namespace Volo.ExtensionMethods
/// Example: "ThisIsSampleSentence" is converted to "This is a sample sentence".
///
/// String to convert.
- public static string ToSentenceCase(this string str)
+ /// set true to use current culture. Otherwise, invariant culture will be used.
+ 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]));
}
///
@@ -318,8 +303,9 @@ namespace Volo.ExtensionMethods
/// Converts camelCase string to PascalCase string.
///
/// String to convert
+ /// set true to use current culture. Otherwise, invariant culture will be used.
/// PascalCase of the string
- 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);
}
///
diff --git a/test/Volo.ExtensionMethods.Tests/Volo/ExtensionMethods/StringExtensions_Tests.cs b/test/Volo.ExtensionMethods.Tests/Volo/ExtensionMethods/StringExtensions_Tests.cs
index 109c00e718..c2d8d302fe 100644
--- a/test/Volo.ExtensionMethods.Tests/Volo/ExtensionMethods/StringExtensions_Tests.cs
+++ b/test/Volo.ExtensionMethods.Tests/Volo/ExtensionMethods/StringExtensions_Tests.cs
@@ -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();
+ }
}
}
\ No newline at end of file