0

In order to properly handle pluralisation, Qt has QObject::tr(), gettext has ngettext, Rails has a good i18n API, Cocoa has .stringsdict files. All those systems take in account the actual number (quantity) to determine the appropriate plural form for a given language.

I know about Humanizer, NGettext and PluralNet but I'm looking for a built-in equivalent in .NET. Does it exist or am I out of luck?

phuzi
  • 12,078
  • 3
  • 26
  • 50
0xced
  • 25,219
  • 10
  • 103
  • 255
  • This looks promising but I've not used it: https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.design.pluralizationservices?view=netframework-4.7.2 – Joe Sewell Mar 21 '19 at 15:57
  • Unfortunately, PluralizationService is only supported for the English language (it throws `NotImplementedException` for other languages). You can also see in its API that it was not designed with other languages in mind since it is only about singular/plural and [many languages have more than those two plural forms](http://cldr.unicode.org/index/cldr-spec/plural-rules). – 0xced Mar 25 '19 at 07:33

1 Answers1

1

This is not exactly a built-in solution, but there's a Visual Studio extension, ReswPlus (on Visual Studio Marketplace), that leverages PluralNet and produces methods taking a number as argument to automatically choose the correct plural form for a given localization.

Here's an excerpt from the README about pluralization:

The resources:

| Key               | Value            | Comment           |
|-------------------|------------------|-------------------|
| MinutesLeft_One   | {0} minute left  | #ReswPlusTyped[Q] |
| MinutesLeft_Other | {0} minutes left |                   |

Will automatically generate the following code:

#region MinutesLeft
/// <summary>
///   Get the pluralized version of the string similar to: {0} minute left
/// </summary>
public static string MinutesLeft(double number)
{
    return Huyn.PluralNet.ResourceLoaderExtension.GetPlural(_resourceLoader, "MinutesLeft", (decimal)number);
}
/// <summary>
///   Format the string similar to: {0} minute left
/// </summary>
public static string MinutesLeft_Format(double pluralCount)
{
    return string.Format(MinutesLeft(pluralCount), pluralCount);
}
#endregion
MRD79
  • 26
  • 2