-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Feature: Plural and singular translation without complex implementation #15323
Comments
Sample of personal useStringExtensions.cs// Copyright (c) 2024 Laštůvka Lukáš
// Licensed under the Apache-2.0 license. See the LICENSE.
using System.Text.RegularExpressions;
namespace ProjectName.Extensions;
public static partial class StringExtensions
{
// Default resource manager
private static readonly ResourceManager _resourceManager = new("ProjectName.Strings.Resources", typeof(Program).Assembly);
// Default pattern for plural strings
private const string _regexPattern = @"\{\s*(?'index'\d+)\s*,\s*plural\s*,\s*=\s*(?'num'\d+)\s*\{\s*(?'std'[^{]+)\s*\}\s*(?:(one|few|many|other)\s*\{\s*([^{}]+)\s*\})?\s*(?:(one|few|many|other)\s*\{\s*([^{}]+)\s*\})?\s*(?:(one|few|many|other)\s*\{\s*([^{}]+)\s*\})?\s*(?:(one|few|many|other)\s*\{\s*([^{}]+)\s*\})?\s*\}";
// Default string to replace with amount
private const string _replaceString = "#";
// Default regex object for pattern
[GeneratedRegex(_regexPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace)]
private static partial Regex ResourceRegex();
private static readonly Regex _regex = ResourceRegex();
/// <summary>
/// Tests if a resource with the specified key exists in the resource manager with the specified culture.
/// </summary>
/// <param name="resKey">The key of the resource to look for.</param>
/// <param name="culture">The culture to use when looking for the resource.</param>
/// <param name="value">The value of the resource, if it exists.</param>
/// <returns>True if the resource exists, false otherwise.</returns>
private static bool ExistsLocalizedResource(this string resKey, CultureInfo culture, out string value)
{
try
{
var keyValue = _resourceManager.GetResourceSet(culture, true, true)?.GetString(resKey);
if (keyValue != null)
{
value = keyValue;
return true;
}
}
catch { }
value = string.Empty;
return false;
}
/// <summary>
/// Gets the plural category for the specified number.
/// </summary>
/// <param name="amount">The number for which to get the plural category.</param>
/// <returns>The plural category for the specified number.</returns>
private static string GetPluralCategory(int amount)
{
if (amount == 1)
{
return "one";
}
else if (amount % 10 >= 2 && amount % 10 <= 4 && (amount % 100 < 10 || amount % 100 >= 20))
{
return "few";
}
else if ((amount % 10 == 0 || (amount % 10 >= 5 && amount % 10 <= 9) || (amount % 100 >= 11 && amount % 100 <= 19)))
{
return "many";
}
else
{
return "other";
}
}
/// <summary>
/// Tests if a resource with the specified key exists in the resource manager with the current UI culture.
/// </summary>
/// <param name="resKey">The key of the resource to look for.</param>
/// <returns>True if the resource exists, false otherwise.</returns>
public static bool ExistsLocalizedResource(this string resKey) => ExistsLocalizedResource(resKey, CultureInfo.CurrentUICulture, out var _);
/// <summary>
/// Gets the localized string for the specified resource key from the resource manager with the specified culture.
/// </summary>
/// <param name="resKey">The key of the resource to look for.</param>
/// <param name="culture">The culture to use when looking for the resource.</param>
/// <returns>The localized string for the resource, or the resource key if the resource could not be found.</returns>
public static string GetLocalizedResource(this string resKey, CultureInfo culture) => ExistsLocalizedResource(resKey, culture, out string value) ? value : resKey;
/// <summary>
/// Gets the localized string for the specified resource key from the resource manager with the current UI culture.
/// </summary>
/// <param name="resKey">The key of the resource to look for.</param>
/// <returns>The localized string for the resource, or the resource key if the resource could not be found.</returns>
public static string GetLocalizedResource(this string resKey) => GetLocalizedResource(resKey, CultureInfo.CurrentUICulture);
/// <summary>
/// Gets the plural localized resource for the specified resource key and culture, using the specified amounts.
/// </summary>
/// <param name="resKey">The key of the resource to look for.</param>
/// <param name="culture">The culture to use when looking for the resource.</param>
/// <param name="amounts">The amounts to use for pluralization.</param>
/// <returns>The pluralized localized resource for the specified key and culture.</returns>
public static string GetPluralLocalizedResource(this string resKey, CultureInfo culture, params int[] amounts)
{
var res = GetLocalizedResource(resKey, culture);
var matches = _regex.Matches(res);
if (matches.Count == 0)
{
return res;
}
foreach (var match in matches.Cast<Match>())
{
var index = int.Parse(match.Groups["index"].Value);
var num = int.Parse(match.Groups["num"].Value);
if (amounts[index] == num)
{
var std = match.Groups["std"].Value;
res = res.ReplaceFirst(match.Groups[0].Value, std.ReplaceFirst(_replaceString, amounts[index].ToString()));
continue;
}
for (int i = 1; i < match.Groups.Count; i += 2)
{
var groupName = match.Groups[i].Value;
var groupText = match.Groups[i + 1].Value;
if (groupName == GetPluralCategory(amounts[index]) || groupName == "other")
{
res = res.ReplaceFirst(match.Groups[0].Value, groupText.ReplaceFirst(_replaceString, amounts[index].ToString()).TrimEnd());
break;
}
}
}
return res;
}
/// <summary>
/// Gets the plural localized resource for the specified resource key and culture, using the specified amounts with the current UI culture.
/// </summary>
/// <param name="resKey">The key of the resource to look for.</param>
/// <param name="amounts">The amounts to use for pluralization.</param>
/// <returns>The pluralized localized resource for the specified key and culture.</returns>
public static string GetPluralLocalizedResource(this string resKey, params int[] amounts) => GetPluralLocalizedResource(resKey, CultureInfo.CurrentUICulture, amounts);
/// <summary>
/// Replaces the first occurrence of a specified string with another specified string in a given string.
/// </summary>
/// <param name="str">The given string.</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The new string to replace the old string.</param>
/// <returns>A string that is identical to the given string except that the first occurrence of oldValue is replaced with newValue.</returns>
public static string ReplaceFirst(this string str, string oldValue, string newValue)
{
int position = str.IndexOf(oldValue);
if (position < 0)
{
return str;
}
str = string.Concat(str.AsSpan(0, position), newValue, str.AsSpan(position + oldValue.Length));
return str;
}
} Example using in code// Copyright (c) 2024 Laštůvka Lukáš
// Licensed under the Apache-2.0 license. See the LICENSE.
// Test New Version
CultureInfo.CurrentUICulture = new("en-US");
string[] testStrings = ["CreateShortcutDescription", "UnpinFolderFromSidebarDescription"];
foreach (string testString in testStrings)
{
Console.WriteLine($"KEY: {testString}");
Console.WriteLine($"Source: \"{testString.GetLocalizedResource()}\"");
Console.WriteLine($"\nResult:\n");
for (int i = 1; i <= 10; i++)
{
Console.WriteLine($"NUM:{i}\t->\t{testString.GetPluralLocalizedResource(i)}");
}
Console.WriteLine();
}
string[] testStrings2 = ["ConflictingItemsDialogSubtitleMultipleConflictsMultipleNonConflicts", "CopyItemsDialogSubtitleMultiple"];
foreach (string testString in testStrings2)
{
Console.WriteLine($"KEY: {testString}");
Console.WriteLine($"Source: \"{testString.GetLocalizedResource()}\"");
Console.WriteLine($"\nResult:\n");
for (int i = 0; i <= 10; i++)
{
Console.WriteLine($"NUM:{i}\t->\t{testString.GetPluralLocalizedResource(i,0)}");
}
Console.WriteLine();
}
Console.ReadKey(); |
Testing on real data
SamplesSample A
Sample B
Sample C
Sample D
|
@Jay-o-Way do you have any input? |
Hm, looks sweet! But not really my expertise... Would be nice as a nuget package? 😉
|
is/are can also:
Test regex101 online work |
MessageFormat
Install NuGet
Demo code// Copyright (c) 2024 Laštůvka Lukáš
// Licensed under the Apache-2.0 license. See the LICENSE.
// Test With MessageFormat
using Jeffijoe.MessageFormat;
CultureInfo.CurrentUICulture = new("en-US");
var mf = new MessageFormatter(useCache: true, locale: CultureInfo.CurrentUICulture.Name);
string[] testStrings = ["CreateShortcutDescription", "UnpinFolderFromSidebarDescription"];
foreach (string testString in testStrings)
{
Console.WriteLine($"KEY: {testString}");
Console.WriteLine($"Source: \"{testString.GetLocalizedResource()}\"");
Console.WriteLine($"\nResult:\n");
for (int i = 1; i <= 10; i++)
{
Console.WriteLine($"NUM:{i}\t->\t{mf.FormatMessage(testString.GetLocalizedResource(), new Dictionary<string, object?> { { "0", i } })}");
}
Console.WriteLine();
}
string[] testStrings2 = ["ConflictingItemsDialogSubtitleMultipleConflictsMultipleNonConflicts", "CopyItemsDialogSubtitleMultiple", "FilesReady"];
foreach (string testString in testStrings2)
{
Console.WriteLine($"KEY: {testString}");
Console.WriteLine($"Source: \"{testString.GetLocalizedResource()}\"");
Console.WriteLine($"\nResult:\n");
for (int i = 0; i <= 10; i++)
{
Console.WriteLine($"NUM:{i}\t->\t{mf.FormatMessage(testString.GetLocalizedResource(), new Dictionary<string, object?> { { "0", i },{ "1", 0} })}");
}
Console.WriteLine();
}
Console.ReadKey(); Results
|
This has low priority comparing to other urgent issues but definitely worth trying and if someone gonna make a PR for this we are happy to help. |
Personally, I try to find a compromise between speed, compatibility and simplicity. Personally, I am creating my own new project and trying to implement this feature. |
The main question about this implementation is if it will cause confusion. This is something we need translators to provide input on as they are the ones putting the time and effort into localizing Files. |
We can also start with a couple strings as a test and see how it goes. |
@XTorLukas do you want to open a PR using this method for a couple strings so we can get a feel for it? |
@yaira2 why not, let's see if the translators know their stuff |
I don't think we need string extension for this as Crowdin seems to support ICU message syntax. The issue that should be fixed here is to have this syntax in en-US resources. Btw, it looks like XCode has a brilliant feature for this type of thing called Grammatical Agreement: you can put |
@0x5bfa we already agreed to try this approach but if you have a simpler solution, please share the details here so we can track in one place. |
@XTorLukas already posted it as a first solution to this, as a second solution to this they suggested to have an extension class. This issue is too crowded to list up. |
Picked some up. Files/src/Files.App/Strings/en-US/Resources.resw Lines 387 to 410 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 429 to 440 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 450 to 452 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 471 to 476 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 1236 to 1244 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 1248 to 1256 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 1260 to 1277 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 1290 to 1295 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 1383 to 1388 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 1626 to 1628 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 2376 to 2405 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 2412 to 2423 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 2445 to 2459 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 2472 to 2480 in af0ab75
Files/src/Files.App/Strings/en-US/Resources.resw Lines 2493 to 2516 in af0ab75
|
@0x5bfa can you open a PR for one of them and we can see how it goes (the Status Bar is a good one to start with)? I'd like to keep this issue open as a backup plan. |
I recommend using the MessageFormat solution, I'm trying it out, and it's versatile and can handle text formats like '{0}' or '{num}' |
@0x5bfa is already busy with other tasks so if you can help that would be great! |
@yaira2 But I don't know the syntax of your code yet, so if a foundation is established, I could start helping. |
Each area is different but I think we can start with the Status Bar. |
I wrote a prototype of a possible implementation and tested this part for my region where more than two possible values are used: <data name="ItemSelected.Text" xml:space="preserve">
<value>položka vybrána</value>
</data>
<data name="ItemsSelected.Text" xml:space="preserve">
<value>položky vybrány</value>
</data> Change to only one pluralKey with prefix <data name="pItemsSelected.Text" xml:space="preserve">
<value>{0, plural, one {# položka vybrána} few {# položky vybrány} other {# položek vybráno}}</value>
</data> Maybe I'll create a PR for my solution |
Looks good to me |
Looks absolutely great. We can reduce a number of string resources. |
I'll start working on finding all the text strings that will need to be reworked. I won't be opening any more PRs, just in the private branch for now, or if necessary I'll create a PR as a draft I'll start with these: #15323 (comment)
|
Merging with #15503 |
What feature or improvement do you think would benefit Files?
I've got an idea to enhance the efficiency of implementing translation for both plural and singular words in Files.
There's also the option to incorporate prefixes and postfixes.
Sample of examples
Examples of display and use in crowdin
Test RegExr online work
Crowdin ICU message implementation (doc)
The text was updated successfully, but these errors were encountered: