.netplural

Does .NET have a built-in pluralisation mechanism?


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?


Solution

  • 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