-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Code Quality: Adding the possibility of plural translation using ICU format #15433
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
yaira2
merged 9 commits into
files-community:main
from
XTorLukas:xtorlukas/CQ-AddMessageFormat
May 21, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
97e33c8
Code Quality: Adding `MessageFormatExtensions`
XTorLukas 7fcc929
Code Quality: Adding test text strings
XTorLukas b46a4be
Code Quality: Adding a method only for localizing the value of the te…
XTorLukas b137095
Code Quality: Adding logging to help find incorrect values
XTorLukas 589c3dc
Fix: Removal of unnecessary texts and suffix correction
XTorLukas 970ab27
Fix: ICU plurals string keys start with lowercase `p`
XTorLukas 7171317
Fix: Update key values and add comments for plural type keys
XTorLukas 0e7668e
Merge branch 'main' into xtorlukas/CQ-AddMessageFormat
yaira2 0bdb970
Update Resources.resw
yaira2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2024 Files Community | ||
// Licensed under the MIT License. See the LICENSE. | ||
|
||
using Jeffijoe.MessageFormat; | ||
using Microsoft.Windows.ApplicationModel.Resources; | ||
using Windows.Globalization; | ||
using System.Globalization; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Files.App.Extensions | ||
{ | ||
public static class MessageFormatExtensions | ||
{ | ||
// Resource map for accessing localized strings | ||
private static readonly ResourceMap resourcesTree = new ResourceManager().MainResourceMap.TryGetSubtree("Resources"); | ||
|
||
// CultureInfo based on the application's primary language override | ||
private static readonly CultureInfo locale = new(ApplicationLanguages.PrimaryLanguageOverride); | ||
|
||
// Message formatter with caching enabled, using the current UI culture's two-letter ISO language name | ||
private static readonly MessageFormatter formatter = new(useCache: true, locale: locale.TwoLetterISOLanguageName); | ||
|
||
// Extension method to create a dictionary for format pairs with a string key | ||
public static IReadOnlyDictionary<string, object?> ToFormatPairs(this string key, object value) => new Dictionary<string, object?> { [key] = value }; | ||
|
||
// Extension method to create a dictionary for format pairs with an integer key | ||
public static IReadOnlyDictionary<string, object?> ToFormatPairs(this int key, object value) => new Dictionary<string, object?> { [key.ToString()] = value }; | ||
|
||
// Retrieves a localized resource string, formatting it with the provided pairs | ||
public static string GetLocalizedFormatResource(this string resourceKey, IReadOnlyDictionary<string, object?> pairs) | ||
{ | ||
var value = resourcesTree?.TryGetValue(resourceKey)?.ValueAsString; | ||
|
||
if (value is null) | ||
return string.Empty; | ||
|
||
try | ||
{ | ||
value = formatter.FormatMessage(value, pairs); | ||
} | ||
catch | ||
{ | ||
value = string.Empty; | ||
App.Logger.LogWarning($"Formatter could not get a valid result value for: '{resourceKey}'"); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
// Overloaded method to accept multiple dictionaries of pairs and merge them | ||
public static string GetLocalizedFormatResource(this string resourceKey, params IReadOnlyDictionary<string, object?>[] pairs) | ||
{ | ||
var mergedPairs = pairs.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value); | ||
return GetLocalizedFormatResource(resourceKey, mergedPairs); | ||
} | ||
|
||
// Overloaded method to accept multiple values and convert them to a dictionary with their indices as keys | ||
public static string GetLocalizedFormatResource(this string resourceKey, params object[] values) | ||
{ | ||
var pairs = values.Select((value, index) => new KeyValuePair<string, object?>(index.ToString(), value)) | ||
.ToDictionary(pair => pair.Key, pair => pair.Value); | ||
return GetLocalizedFormatResource(resourceKey, pairs); | ||
} | ||
|
||
//TODO: Could replace `GetLocalizedResource()` in the future | ||
public static string GetLocalizedFormatResource(this string resourceKey) => GetLocalizedFormatResource(resourceKey, new Dictionary<string, object?>()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.