Skip to content

Fixed StringFormatConverter ignoring Language parameter, cleanup #4764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions Microsoft.Toolkit.Uwp.UI/Converters/StringFormatConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Globalization;
using Windows.UI.Xaml.Data;

namespace Microsoft.Toolkit.Uwp.UI.Converters
Expand All @@ -18,29 +19,33 @@ public class StringFormatConverter : IValueConverter
/// <param name="value">Object to transform to string.</param>
/// <param name="targetType">The type of the target property, as a type reference</param>
/// <param name="parameter">An optional parameter to be used in the string.Format method.</param>
/// <param name="language">The language of the conversion (not used).</param>
/// <param name="language">The language of the conversion. If language is null or empty then <see cref="CultureInfo"/> will be used.</param>
/// <returns>Formatted string.</returns>
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
if (value is null)
{
return null;
}

if (parameter == null)
// Retrieve the format string and use it to format the value.
string formatString = parameter as string;
if (string.IsNullOrEmpty(formatString))
{
return value;
// If the format string is null or empty, simply call ToString()
// on the value.
return value.ToString();
}

try
{
return string.Format((string)parameter, value);
CultureInfo culture = string.IsNullOrWhiteSpace(language) ? CultureInfo.InvariantCulture : new CultureInfo(language);
return string.Format(culture, formatString, value);
}
catch
{
return value;
}

return value;
}

/// <summary>
Expand Down
41 changes: 23 additions & 18 deletions UnitTests/UnitTests.UWP/Converters/Test_StringFormatConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,63 @@
using Microsoft.Toolkit.Uwp.UI.Converters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Globalization;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace UnitTests.Converters
{
[TestClass]
public class Test_StringFormatConverter
{
private static readonly object NullString = null;
private static readonly object NotEmptyString = "Hello, world";
private static readonly DateTime Date = DateTime.Now;

[TestCategory("Converters")]
[TestMethod]
public void WhenValueIsNull_ThenReturnNull()
[DataRow(null)]
[DataRow("en-us")]
public void WhenValueIsNull_ThenReturnNull(string language)
{
var converter = new StringFormatConverter();
var result = converter.Convert(NullString, typeof(string), NullString, "en-us");
var result = converter.Convert(null, typeof(string), null, language);

Assert.IsNull(result);
}

[TestCategory("Converters")]
[TestMethod]
public void WhenValueExistsAndParameterIsNull_ThenReturnValue()
[DataRow(null)]
[DataRow("en-us")]
public void WhenValueExistsAndParameterIsNull_ThenReturnValue(string language)
{
var converter = new StringFormatConverter();
var result = converter.Convert(NotEmptyString, typeof(string), NullString, "en-us");
var result = converter.Convert(NotEmptyString, typeof(string), null, language);

Assert.AreEqual(NotEmptyString, result);
}

[TestCategory("Converters")]
[TestMethod]
public void WhenParameterIsTimeFormat_ThenReturnValueOfTimeFormat()
[DataRow(null)]
[DataRow("en-us")]
public void WhenParameterIsInvalidFormat_ThenReturnValue(string language)
{
var converter = new StringFormatConverter();
var result = converter.Convert(Date, typeof(string), "{0:HH:mm}", "en-us");
Assert.AreEqual(Date.ToString("HH:mm"), result);
}
var result = converter.Convert(Date, typeof(string), "{1:}", language);

[TestCategory("Converters")]
[TestMethod]
public void WhenParameterIsInvalidFormat_ThenReturnValue()
{
var converter = new StringFormatConverter();
var result = converter.Convert(Date, typeof(string), "{1:}", "en-us");
Assert.AreEqual(Date, result);
}

[TestCategory("Converters")]
[TestMethod]
public void WhenParameterIsNotAString_ThenReturnValue()
[DataRow(null)]
[DataRow("en-us")]
public void WhenParameterIsNotAString_ThenReturnValue(string language)
{
var converter = new StringFormatConverter();
var result = converter.Convert(NotEmptyString, typeof(string), 172, "en-us");
var result = converter.Convert(NotEmptyString, typeof(string), 172, language);

Assert.AreEqual(NotEmptyString, result);
}
}
Expand Down