[Run][Calculator Plugin] Add number formatting for different Locales (#9074)

* Add number formatting for different locales

* Remove conversionRequired check

* Add NumberTranslatorTests
This commit is contained in:
Pavel Zwerschke
2021-01-25 10:51:32 +01:00
committed by GitHub
parent a24ea2f3be
commit be02ee1a25
3 changed files with 135 additions and 11 deletions

View File

@@ -28,7 +28,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
throw new ArgumentNullException(paramName: nameof(query));
}
if (!CalculateHelper.InputValid(query.Search))
NumberTranslator translator = NumberTranslator.Create(CultureInfo.CurrentCulture, new CultureInfo("en-US"));
var input = translator.Translate(query.Search);
if (!CalculateHelper.InputValid(input))
{
return new List<Result>();
}
@@ -36,7 +39,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
try
{
// Using CurrentUICulture since this is user facing
var result = CalculateEngine.Interpret(query.Search, CultureInfo.CurrentUICulture);
var result = CalculateEngine.Interpret(input, CultureInfo.CurrentUICulture);
// This could happen for some incorrect queries, like pi(2)
if (result.Equals(default(CalculateResult)))

View File

@@ -29,8 +29,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
}
/// <summary>
/// Create a new <see cref="NumberTranslator"/> - returns null if no number conversion
/// is required between the cultures.
/// Create a new <see cref="NumberTranslator"/>.
/// </summary>
/// <param name="sourceCulture">source culture</param>
/// <param name="targetCulture">target culture</param>
@@ -44,15 +43,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
if (targetCulture == null)
{
throw new ArgumentNullException(paramName: nameof(sourceCulture));
throw new ArgumentNullException(paramName: nameof(targetCulture));
}
bool conversionRequired = sourceCulture.NumberFormat.NumberDecimalSeparator != targetCulture.NumberFormat.NumberDecimalSeparator
|| sourceCulture.NumberFormat.PercentGroupSeparator != targetCulture.NumberFormat.PercentGroupSeparator
|| sourceCulture.NumberFormat.NumberGroupSizes != targetCulture.NumberFormat.NumberGroupSizes;
return conversionRequired
? new NumberTranslator(sourceCulture, targetCulture)
: null;
return new NumberTranslator(sourceCulture, targetCulture);
}
/// <summary>