Skip to content

Commit 9ecec6f

Browse files
XTorLukas0x5bfa
andauthored
Code Quality: Convert Multiple Localized Strings to ICU Message Format | Add custom number format (#15517)
Co-authored-by: 0x5BFA <62196528+0x5bfa@users.noreply.github.com>
1 parent 8697522 commit 9ecec6f

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Files.App/Extensions/MessageFormatExtensions.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,33 @@ public static class MessageFormatExtensions
2525
/// </summary>
2626
private static readonly CultureInfo _locale = new(AppLanguageHelper.PreferredLanguage.Code);
2727

28+
/// <summary>
29+
/// Gets custom value formatters for the message formatter.
30+
/// This class is used to customize the formatting of specific value types.
31+
/// </summary>
32+
private static readonly CustomValueFormatters _customFormatter = new()
33+
{
34+
// Custom formatting for number values.
35+
Number = (CultureInfo _, object? value, string? style, out string? formatted) =>
36+
{
37+
if (style is not null && style == string.Empty)
38+
{
39+
// Format the number '{0, number}'
40+
formatted = string.Format($"{{0:#,##0}}", value);
41+
return true;
42+
}
43+
44+
formatted = null;
45+
return false;
46+
}
47+
};
48+
2849
/// <summary>
2950
/// Message formatter with caching enabled, using the current UI culture's two-letter ISO language name.
30-
/// It is initialized with the options to use cache and the two-letter ISO language name of the current UI culture.
51+
/// It is initialized with the options to use cache and the two-letter ISO language name of the current UI culture,
52+
/// and a custom value formatter for number values.
3153
/// </summary>
32-
private static readonly MessageFormatter _formatter = new(useCache: true, locale: _locale.TwoLetterISOLanguageName);
54+
private static readonly MessageFormatter _formatter = new(useCache: true, locale: _locale.TwoLetterISOLanguageName, customValueFormatter: _customFormatter);
3355

3456
/// <summary>
3557
/// Creates a dictionary for format pairs with a string key.

0 commit comments

Comments
 (0)