|
| 1 | +// Copyright (c) 2024 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using Jeffijoe.MessageFormat; |
| 5 | +using Microsoft.Windows.ApplicationModel.Resources; |
| 6 | +using Windows.Globalization; |
| 7 | +using System.Globalization; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | + |
| 10 | +namespace Files.App.Extensions |
| 11 | +{ |
| 12 | + public static class MessageFormatExtensions |
| 13 | + { |
| 14 | + // Resource map for accessing localized strings |
| 15 | + private static readonly ResourceMap resourcesTree = new ResourceManager().MainResourceMap.TryGetSubtree("Resources"); |
| 16 | + |
| 17 | + // CultureInfo based on the application's primary language override |
| 18 | + private static readonly CultureInfo locale = new(ApplicationLanguages.PrimaryLanguageOverride); |
| 19 | + |
| 20 | + // Message formatter with caching enabled, using the current UI culture's two-letter ISO language name |
| 21 | + private static readonly MessageFormatter formatter = new(useCache: true, locale: locale.TwoLetterISOLanguageName); |
| 22 | + |
| 23 | + // Extension method to create a dictionary for format pairs with a string key |
| 24 | + public static IReadOnlyDictionary<string, object?> ToFormatPairs(this string key, object value) => new Dictionary<string, object?> { [key] = value }; |
| 25 | + |
| 26 | + // Extension method to create a dictionary for format pairs with an integer key |
| 27 | + public static IReadOnlyDictionary<string, object?> ToFormatPairs(this int key, object value) => new Dictionary<string, object?> { [key.ToString()] = value }; |
| 28 | + |
| 29 | + // Retrieves a localized resource string, formatting it with the provided pairs |
| 30 | + public static string GetLocalizedFormatResource(this string resourceKey, IReadOnlyDictionary<string, object?> pairs) |
| 31 | + { |
| 32 | + var value = resourcesTree?.TryGetValue(resourceKey)?.ValueAsString; |
| 33 | + |
| 34 | + if (value is null) |
| 35 | + return string.Empty; |
| 36 | + |
| 37 | + try |
| 38 | + { |
| 39 | + value = formatter.FormatMessage(value, pairs); |
| 40 | + } |
| 41 | + catch |
| 42 | + { |
| 43 | + value = string.Empty; |
| 44 | + App.Logger.LogWarning($"Formatter could not get a valid result value for: '{resourceKey}'"); |
| 45 | + } |
| 46 | + |
| 47 | + return value; |
| 48 | + } |
| 49 | + |
| 50 | + // Overloaded method to accept multiple dictionaries of pairs and merge them |
| 51 | + public static string GetLocalizedFormatResource(this string resourceKey, params IReadOnlyDictionary<string, object?>[] pairs) |
| 52 | + { |
| 53 | + var mergedPairs = pairs.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value); |
| 54 | + return GetLocalizedFormatResource(resourceKey, mergedPairs); |
| 55 | + } |
| 56 | + |
| 57 | + // Overloaded method to accept multiple values and convert them to a dictionary with their indices as keys |
| 58 | + public static string GetLocalizedFormatResource(this string resourceKey, params object[] values) |
| 59 | + { |
| 60 | + var pairs = values.Select((value, index) => new KeyValuePair<string, object?>(index.ToString(), value)) |
| 61 | + .ToDictionary(pair => pair.Key, pair => pair.Value); |
| 62 | + return GetLocalizedFormatResource(resourceKey, pairs); |
| 63 | + } |
| 64 | + |
| 65 | + //TODO: Could replace `GetLocalizedResource()` in the future |
| 66 | + public static string GetLocalizedFormatResource(this string resourceKey) => GetLocalizedFormatResource(resourceKey, new Dictionary<string, object?>()); |
| 67 | + } |
| 68 | +} |
0 commit comments