31 lines
1.1 KiB
C#
31 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using Jeffijoe.MessageFormat;
|
|
|
|
namespace WinWARP.Ui.Helpers;
|
|
|
|
/// <summary>
|
|
/// Provides methods to determine the plural category of a number based on the specified locale and whether it is an ordinal or cardinal number.
|
|
/// </summary>
|
|
public static class PluralCategory
|
|
{
|
|
private static readonly MessageFormatter Formatter = new();
|
|
|
|
private static readonly string CardinalPattern =
|
|
"{n, plural, zero{zero} one{one} two{two} few{few} many{many} other{other}}";
|
|
|
|
private static readonly string OrdinalPattern =
|
|
"{n, selectordinal, zero{zero} one{one} two{two} few{few} many{many} other{other}}";
|
|
|
|
public static string GetCategory(double number, CultureInfo locale, bool isOrdinal = false)
|
|
{
|
|
var pattern = isOrdinal ? OrdinalPattern : CardinalPattern;
|
|
var args = new Dictionary<string, object?> { { "n", number } };
|
|
|
|
// Formatter resolves the logic and returns the matched keyword block literal
|
|
return Formatter.FormatMessage(pattern, args, locale);
|
|
}
|
|
}
|